内容简介:ElasticSearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。1.查询DSL(query DSL)和过滤DSL(filter DSL)package elastic
ElasticSearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。
1.查询DSL(query DSL)和过滤DSL(filter DSL)
package elastic
import (
"boss/base"
"boss/models"
"context"
"fmt"
"gopkg.in/olivere/elastic.v5"
"log"
"os"
"reflect"
"strconv"
"strings"
)
var client *elastic.Client
var host = "http://127.0.0.1:9200/"
type CmsAudiosets struct {
Name stringjson:"name"
PublishName string
json:"publishname"
Summary string
json:"summary"
Detail string
json:"detail"
Cid int64
json:"cid"
Type int
json:"type"
ImgUrl string
json:"imgurl"
Frequency string
json:"frequency"
LogoImg string
json:"logoimg"
AuthorName string
json:"authorname"
Sort int64
json:"sort"
}
//初始化
func init() {
errorlog := log.New(os.Stdout, "APP", log.LstdFlags)
var err error
client, err = elastic.NewClient(elastic.SetErrorLog(errorlog), elastic.SetURL(host))
if err != nil {
base.LOG_INFO("client err:", err)
panic(err)
}
info, code, err := client.Ping(host).Do(context.Background())
if err != nil {
base.LOG_INFO("client err:", err)
panic(err)
}
base.LOG_INFO("client:", code, info.Version.Number)
//本地测试使用
create()
}
//创建
func create() {
var dao models.DetailDao
//获取除电台外数据
maps_ic := dao.GetCMSAudioAllExcept4()
if maps_ic != nil {
maps := maps_ic.(*models.MapsModel)
maps)
content_index := 0
for index := int64(0); index < maps.Counts; index++ {
gcontent := new(CmsAudiosets)
gcontent.Name = maps.GetString("name", index)
gcontent.PublishName = maps.GetString("publishName", index)
gcontent.Summary = maps.GetString("summary", index)
gcontent.Cid = maps.GetInt64("cid", index)
gcontent.Type = maps.GetInt("type", index)
gcontent.ImgUrl = maps.GetString("imgurl", index)
//TODO 作者
content_index++
put1, err := client.Index().Index("cmsaudio").Type("cmsaudiosets").
Id(strconv.Itoa(content_index)).BodyJson(gcontent).Do(context.Background())
if err != nil {
panic(err)
}
fmt.Printf("Indexed tweet %s to index s%s, type %s\n", put1.Id, put1.Index, put1.Type)
}
}
//电台
maps_ir := dao.GetAllFmRadio()
if maps_ic != nil {
models.MapsModel)//base.LOG_INFO("maps:", *maps)
content_index := 0
for index := int64(0); index < maps.Counts; index++ {
gcontent := new(CmsAudiosets)
gcontent.Name = maps.GetString("id", index)
gcontent.Cid = maps.GetInt64("cid", index)
gcontent.Type = 4
gcontent.ImgUrl = maps.GetString("imgurl", index)
gcontent.LogoImg = maps.GetString("logoimg", index)
gcontent.Frequency = maps.GetString("frequency", index)
content_index++
put1, err := client.Index().Index("cmsaudio").Type("cmsaudiosets").
Id(strconv.Itoa(content_index)).BodyJson(gcontent).Do(context.Background())
if err != nil {
panic(err)
}
fmt.Printf("Indexed tweet %s to index s%s, type %s\n", put1.Id, put1.Index, put1.Type)
}
}
}
type DetailAssociateContent struct {
Id stringjson:"id"
Name string
json:"name"
}
//搜索联想词4028
func AssociatWord(keyword string) ([]DetailAssociateContent, int, error) {
var res *elastic.SearchResult
var err error
//取所有 TODO context时间
res, err = client.Search("cmsaudio").Type("cmsaudiosets").Do(context.Background())
boolSearch := elastic.NewBoolQuery().Filter(elastic.NewQueryStringQuery(keyword))
res, err = client.Search("cmsaudio").Type("cmsaudiosets").Query(boolSearch).Do(context.Background())
var estyp CmsAudiosets
var content = make([]DetailAssociateContent, 0)
index := 0
for _, item := range res.Each(reflect.TypeOf(estyp)) { //从搜索结果中取数据的方法
t := item.(CmsAudiosets)
if strings.Index(t.Name, keyword) != -1 {
content = append(content, DetailAssociateContent{Id: strconv.Itoa(index), Name: t.Name})
index++ if index >= 20 { return content, index, err } } if strings.Index(t.Summary, keyword) != -1 { content = append(content, DetailAssociateContent{Id: strconv.Itoa(index), Name: t.Summary}) index++ if index >= 20 { return content, index, err } } if strings.Index(t.Frequency, keyword) != -1 { content = append(content, DetailAssociateContent{Id: strconv.Itoa(index), Name: t.Frequency}) index++ if index >= 20 { return content, index, err } } if strings.Index(t.PublishName, keyword) != -1 { content = append(content, DetailAssociateContent{Id: strconv.Itoa(index), Name: t.PublishName}) index++ if index >= 20 { return content, index, err } } if strings.Index(t.AuthorName, keyword) != -1 { content = append(content, DetailAssociateContent{Id: strconv.Itoa(index), Name: t.AuthorName}) index++ if index >= 20 { return content, index, err } } if strings.Index(t.Detail, keyword) != -1 { content = append(content, DetailAssociateContent{Id: strconv.Itoa(index), Name: t.Detail}) index++ if index >= 20 { return content, index, err } } } return content, index, err
}
type DetailSearchContent struct {
Name stringjson:"name"
Summary string
json:"summary"
Cid int64
json:"cid"
Type int
json:"type"
ImgUrl string
json:"imgurl"
Frequency string
json:"frequency"
LogoImg string
json:"logoimg"
}
func EsSearch(keyword string, typ string) ([]DetailSearchContent, int, error) {
var res *elastic.SearchResult
var content = make([]DetailSearchContent, 0)
index := 0
var estyp CmsAudiosets
var err error
//取所有
res, err = client.Search("cmsaudio").Type("cmsaudiosets").Do(context.Background())
if typ == "0" {
boolSearch := elastic.NewBoolQuery().Filter(elastic.NewQueryStringQuery(keyword))
res, err = client.Search("cmsaudio").Type("cmsaudiosets").Query(boolSearch).Do(context.Background())
} else {
typint, er := strconv.Atoi(typ)
if er != nil {
return content, index, err
}
boolSearch := elastic.NewBoolQuery().Filter(elastic.NewQueryStringQuery(keyword)).Filter(elastic.NewTermQuery("type", typint))
res, err = client.Search("cmsaudio").Type("cmsaudiosets").Query(boolSearch).Do(context.Background())
}
for _, item := range res.Each(reflect.TypeOf(estyp)) {
t := item.(CmsAudiosets)
content = append(content, DetailSearchContent{
Name: t.Name,
Summary: t.Summary,
Cid: t.Cid,
Type: t.Type,
ImgUrl: t.ImgUrl,
Frequency: t.Frequency,
LogoImg: t.LogoImg,
})
if index >= 20 {
return content, index, err
}
index++
}
return content, index, err
}
以上所述就是小编给大家介绍的《ES---golang》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Introduction to Linear Optimization
Dimitris Bertsimas、John N. Tsitsiklis / Athena Scientific / 1997-02-01 / USD 89.00
"The true merit of this book, however, lies in its pedagogical qualities which are so impressive..." "Throughout the book, the authors make serious efforts to give geometric and intuitive explanations......一起来看看 《Introduction to Linear Optimization》 这本书的介绍吧!