栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

go操作ElasticSearch7.3.2

go操作ElasticSearch7.3.2

ElasticSearch的安装以及基本操作见这篇文章:ElasticSearch.Net 7.3.2 的学习

引用go的es包
go get github.com/olivere/elastic/v7

注意这里使用的是olivere/elastic的v7版本的包。
es提供的官方的go包不好用,由于本文章使用的ES7.3.2所以后面要加上v7,不然默认引用的v6的包!!!

初始化client
type BatDataInfo struct {
	BatId       int       `json:"batid"`
	BatCode     string    `json:"batcode"`
	StationName string    `josn:"stationname"`
	ClientName  string    `josn:"clientname"`
	Cycle       int       `josn:"cycle"`
	Step        int       `josn:"step"`
	ValueType   int       `josn:"valuetype"`
	Value       string    `josn:"value"`
	UploadTime  time.Time `josn:"uploadtime"`
}

func NewESClient() *elastic.Client {
	client, err := elastic.NewClient(elastic.SetSniff(false), elastic.SetURL("http://localhost:9200"))

	if err != nil {
		panic(err)
	}
	return client
}
Terms查询

这里引用了go-linq包,使得在go中操作可遍历对象像C#使用linq一样:

go get github.com/ahmetb/go-linq
func queryDetailDataByIds(batIds []int) {
	client := NewESClient()

	boolQuery := elastic.NewBoolQuery()

	objs := make([]interface{}, 0)
	linq.From(batIds).Select(func(i interface{}) interface{} { return i }).ToSlice(&objs)

	termsQuery := elastic.NewTermsQuery("batid", objs...)
	boolQuery.Must(termsQuery)

	result, err := client.Search().Index("batdatainfo").Pretty(true).Query(boolQuery).Do(context.Background())

	if err != nil {
		panic(err)
	}

	fmt.Printf("took time:%d msn", result.TookInMillis)
	models := make([]BatDataInfo, 0)

	var ttyp BatDataInfo
	for _, item := range result.Each(reflect.TypeOf(ttyp)) {
		t := item.(BatDataInfo)
		models = append(models, t)
	}

	if result.Hits.TotalHits.Value > 0 {
		fmt.Printf("Found count:%dn", result.Hits.TotalHits.Value)
	} else {
		fmt.Print("Found no batdatainfosn")
	}

	fmt.Println(models)
}
多条件的Terms查询
func queryDetailData(batIds []int, stationnames []string, valuetypes []int, startTime time.Time, endTime time.Time) ([]BatDataInfo, error) {
	queries := make([]elastic.Query, 0)
	if len(batIds) > 0 {
		objs := make([]interface{}, 0)
		linq.From(batIds).Select(func(i interface{}) interface{} { return i }).ToSlice(&objs)

		queries = append(queries, elastic.NewTermsQuery("batid", objs...))
	}

	if len(stationnames) > 0 {
		objs := make([]interface{}, 0)
		linq.From(batIds).Select(func(i interface{}) interface{} { return i }).ToSlice(&objs)

		queries = append(queries, elastic.NewTermsQuery("stationname", objs...))
	}

	if len(valuetypes) > 0 {
		objs := make([]interface{}, 0)
		linq.From(valuetypes).Select(func(i interface{}) interface{} { return i }).ToSlice(&objs)

		queries = append(queries, elastic.NewTermsQuery("valuetype", objs...))
	}

	if !startTime.IsZero() {
		queries = append(queries, elastic.NewRangeQuery("uploadtime").Gte(startTime))
	}

	if !endTime.IsZero() {
		queries = append(queries, elastic.NewRangeQuery("uploadtime").Lte(endTime))
	}

	boolQuery := elastic.NewBoolQuery()
	boolQuery.Must(queries...)

	client := NewESClient()
	searchResult, err := client.Search().Size(1000000).Index("batdatainfo").Query(boolQuery).Do(context.TODO())

	if err != nil {
		return nil, err
	}

	models := make([]BatDataInfo, 0)
	for _, item := range searchResult.Each(reflect.TypeOf(BatDataInfo{})) {
		t := item.(BatDataInfo)
		models = append(models, t)
	}
	return models, nil
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/775479.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号