go调用olivere包操作elasticsearch获取原始数据

go调用olivere包操作elasticsearch获取原始数据

go调用olivere包操作elasticsearch获取原始数据

核心代码如下:

//这里是查询条件

boolQ := elastic.NewBoolQuery()
//boolQ.Filter(elastic.NewRangeQuery("saveTime").Gte(begin))

res,err := this.deviceDBClient.Search("索引名称").
		Size(10000).
		Query(boolQ).
		//Sort("saveTime",true).	//排序
		Do(context.Background())

	if err != nil{
		fmt.Println("读取失败",err)
	}
//开始读取原始数据	
for _,val := range res.Hits.Hits{
		t := DevicePo.JieShouPo{}	//数据的主体对象
		err := json.Unmarshal(val.Source,&t)
 		fmt.Println(val.Index,val.Id,t.SaveTime,err)
 		/*
 			val中包含了所有的原始数据,包括索引名称,索引类型,数据ID
 			如下为原始内容
 			// SearchHit is a single hit.
			type SearchHit struct {
				Score          *float64                       `json:"_score,omitempty"`   // computed score
				Index          string                         `json:"_index,omitempty"`   // index name
				Type           string                         `json:"_type,omitempty"`    // type meta field
				Id             string                         `json:"_id,omitempty"`      // external or internal
				Uid            string                         `json:"_uid,omitempty"`     // uid meta field (see MapperService.java for all meta fields)
				Routing        string                         `json:"_routing,omitempty"` // routing meta field
				Parent         string                         `json:"_parent,omitempty"`  // parent meta field
				Version        *int64                         `json:"_version,omitempty"` // version number, when Version is set to true in SearchService
				SeqNo          *int64                         `json:"_seq_no"`
				PrimaryTerm    *int64                         `json:"_primary_term"`
				Sort           []interface{}                  `json:"sort,omitempty"`            // sort information
				Highlight      SearchHitHighlight             `json:"highlight,omitempty"`       // highlighter information
				Source         json.RawMessage                `json:"_source,omitempty"`         // stored document source
				Fields         SearchHitFields                `json:"fields,omitempty"`          // returned (stored) fields
				Explanation    *SearchExplanation             `json:"_explanation,omitempty"`    // explains how the score was computed
				MatchedQueries []string                       `json:"matched_queries,omitempty"` // matched queries
				InnerHits      map[string]*SearchHitInnerHits `json:"inner_hits,omitempty"`      // inner hits with ES >= 1.5.0
				Nested         *NestedHit                     `json:"_nested,omitempty"`         // for nested inner hits
				Shard          string                         `json:"_shard,omitempty"`          // used e.g. in Search Explain
				Node           string                         `json:"_node,omitempty"`           // used e.g. in Search Explain
			
				// HighlightFields
				// SortValues
				// MatchedFilters
			}
		*/
	}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值