如何在Elasticsearch Golang客户端中检索整个搜索匹配来源?

时间:2019-06-12 15:50:37

标签: elasticsearch go

我有一个包含太多字段的索引,例如一个文档有6个字段,而另一个有不同数量的字段,总共有近千个不同的字段。

i遵循此https://github.com/olivere/elastic/wiki/Search,它工作正常,但我无法创建所有字段的结构并对此执行json.Unmarshal(*hit.Source, &t)。 有什么办法可以在没有字段struct的情况下检索整个源。

我使用Elasticsearch版本-7和olivre elasticsearch golang库。

1 个答案:

答案 0 :(得分:0)

如果我理解正确,则您不想解组json结果-您只想访问该值。

搜索操作返回SearchHit结果docs here

有一个字段

type SearchHit struct {
    // trimmed
    Source         json.RawMessage                `json:"_source,omitempty"`         

}

json.RawMessage只是[]bytedocs here

因此,可以将其作为字符串获取,

s := string(hit.Source)