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 string json:"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)
//base.LOG_INFO("maps:", 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 {
maps := maps_ir.(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 string json:"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 string json:"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
}
有疑问加站长微信联系(非本文作者)