ES---golang

栏目: Go · 发布时间: 5年前

内容简介: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 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 {

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

}


以上所述就是小编给大家介绍的《ES---golang》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

产品心经

产品心经

闫荣 / 机械工业出版社 / 2014-9-30 / 59

产品经理如何才能迅速地、全方位地提升自己的能力,从而打造出让用户尖叫并疯狂爱上的产品?有没有捷径?从成功的、有经验的产品经理的实践真知和智慧中学习是一个很好的途径!本书就是一位拥有近10年产品经验的资深产品经理的实践真知和智慧的结晶,从产品经理核心素养、产品认知、战略与规划、精益开发、需求分析与管理、用户体验、精细运营7大方面,系统梳理了能全面、迅速提升产品经理能力,从而打造出让用户尖叫的产品的5......一起来看看 《产品心经》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

在线进制转换器
在线进制转换器

各进制数互转换器

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具