I'm using Olivere v7.
I'm trying to Merge _id & _source (from the ElasticSearch call, which returns a JSON response) into a re-ordered struct, and deliver results in that format.
This is what my actual code is:
elasticsearch, err := //actual request to ES using olivere fmt.Println(elasticsearch.Hits.Hits) and it successfully makes a call and prints this:
{ "_score": 11.019884, "_index": "test", "_type": "_doc", "_id": "20", "_seq_no": null, "_primary_term": null, "_source": { "name": "Michael J.", "age": "22" } }, { "_score": 11.019884, "_index": "test", "_type": "_doc", "_id": "21", "_seq_no": null, "_primary_term": null, "_source": { "name": "Michael Jae.", "age": "18" } }, { "_score": 11.019884, "_index": "test", "_type": "_doc", "_id": "52", "_seq_no": null, "_primary_term": null, "_source": { "name": "Michael Jay.", "age": "69" } }, { "_score": 11.019884, "_index": "test", "_type": "_doc", "_id": "33", "_seq_no": null, "_primary_term": null, "_source": { "name": "Michael Jo.", "age": "25" } } Instead, i would like to only print "_id", "name", "age"
So the desired output based on the same query above would be:
{ "id": "20", "name": "Michael J.", "age": "22" }, { "id": "21", "name": "Michael Jae.", "age": "18" }, { "id": "52", "name": "Michael Jay.", "age": "69" }, { "id": "33", "name": "Michael Jo.", "age": "25" } I've written this code:
elasticsearch, err := //actual request to ES using olivere type StructuredResponse struct { ID string `json:"id"` Email string `json:"email"` Name string `json:"name"` } var SR []StructuredResponse for _, hit := range elasticsearch.Hits.Hits { source, err := json.Marshal(hit.Source) if err != nil { panic(err) } var SR2 StructuredResponse err = json.Unmarshal(source, &SR2) if err != nil { panic(err) } SR = append(SR, SR2) } fmt.Println(SR) But i'm lost, im not sure what the next step would be or what to do from here.
Update:
elasticsearch.Hits.Hits points to this struct within Olivere's package:
// 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 map[string]interface{} `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 }
IDin the resulting elasticsearch output_id? Then it should bejson:"_id". But that will not work, as _id is one level up to the rest of the data. Also, why are you marshalling it back to json? Isn't the ID already available on thehitdirectly?