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》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

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

GWT in Action

GWT in Action

Robert Hanson、Adam Tacy / Manning Publications / 2007-06-05 / USD 49.99

This book will show Java developers how to use the Google Web Toolkit (GWT) to rapidly create rich web-based applications using their existing skills. It will cover the full development cycle, from ......一起来看看 《GWT in Action》 这本书的介绍吧!

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试