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 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
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值