go *** 作ElasticSearch7.3.2

go *** 作ElasticSearch7.3.2,第1张

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 ms\n", 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:%d\n", result.Hits.TotalHits.Value)
	} else {
		fmt.Print("Found no batdatainfos\n")
	}

	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
}

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/994988.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存