我正在使用 go-elasticsearch,它是弹性的官方包。这是我的弹性响应:
{
"took": 12,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 0,
"relation": "eq"
},
"max_score": null,
"hits": [
]
},
"suggest": {
"compeletion-suggest": [
{
"text": "txt",
"offset": 0,
"length": 3,
"options": [
{
"text": "sometext_result1",
"_index": "myindex",
"_type": "_doc",
"_id": "id1",
"_score": 2.0,
"_source": {
"content_completion": {
"input": [
"sometext_result1"
],
"weight": 2
}
}
},
{
"text": "sometext_result2",
"_index": "myindex",
"_type": "_doc",
"_id": "id2",
"_score": 1.0,
"_source": {
"content_completion": {
"input": [
"sometext_result2"
],
"weight": 1
}
}
},
]
}
}
我想迭代“选项”,我已经尝试过这个:
var (
r map[string]interface{}
)
es, err := elasticsearch.NewDefaultClient()
var buf bytes.Buffer
query := map[string]interface{}{
"suggest": map[string]interface{}{
"compeletion-suggest": map[string]interface{}{
"prefix": "txt",
"completion": map[string]interface{}{
"field": "content_completion",
},
},
},
}
if err := json.NewEncoder(&buf).Encode(query); err != nil {
log.Fatalf("Error encoding query: %s", err)
}
res, err = es.Search(
es.Search.WithContext(context.Background()),
es.Search.WithIndex("myindex"),
es.Search.WithBody(&buf),
es.Search.WithTrackTotalHits(true),
es.Search.WithPretty(),
)
我是 Go 新手。我需要知道如何访问弹性响应字段。例如,如何读取选项的 _id 字段?
长风秋雁
相关分类