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
}
*/
}