执行聚合时如何获取搜索命中结果?
How to get search hits results when executing aggregation?
如 ElasticSearch 文档中所述:
In Elasticsearch, you have the ability to execute searches returning hits and at the same time return aggregated results separate from the hits all in one response. This is very powerful and efficient in the sense that you can run queries and multiple aggregations and get the results back of both (or either) operations in one shot avoiding network roundtrips using a concise and simplified API.
我想在查询聚合时执行返回命中的搜索。但我不确定如何才能实现上述目标?
我正在使用以下查询:
curl -XPOST 'localhost:9200/employee/_search?pretty' -d '
{
"size": 0,
"aggs": {
"group_by_domain": {
"terms": {
"field": "domain"
}
}
}
}'
这是我得到的结果,
{
"took" : 92,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 1000,
"max_score" : 0.0,
"hits" : [ ]
},
"aggregations" : {
"group_by_domain" : {
"doc_count_error_upper_bound" : 5,
"sum_other_doc_count" : 744,
"buckets" : [ {
"key" : "finance",
"doc_count" : 30
}]
}
}
}
正如我们所见,hits 数组是空的。我不确定如何获得这些命中数组。有什么建议吗?
命中为空,因为您在指定时将返回查询的大小设置为 0:
"size": 0,
您可以完全删除大小,在这种情况下,您将获得 10 次默认匹配,或者您可以设置您想要的大小,例如,如果您指定 100,您将获得 100 次匹配作为响应。这与搜索结果有关。
现在,如果您还想在聚合中获得结果,您可以使用 Top Hits Aggregation。
如 ElasticSearch 文档中所述:
In Elasticsearch, you have the ability to execute searches returning hits and at the same time return aggregated results separate from the hits all in one response. This is very powerful and efficient in the sense that you can run queries and multiple aggregations and get the results back of both (or either) operations in one shot avoiding network roundtrips using a concise and simplified API.
我想在查询聚合时执行返回命中的搜索。但我不确定如何才能实现上述目标?
我正在使用以下查询:
curl -XPOST 'localhost:9200/employee/_search?pretty' -d '
{
"size": 0,
"aggs": {
"group_by_domain": {
"terms": {
"field": "domain"
}
}
}
}'
这是我得到的结果,
{
"took" : 92,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 1000,
"max_score" : 0.0,
"hits" : [ ]
},
"aggregations" : {
"group_by_domain" : {
"doc_count_error_upper_bound" : 5,
"sum_other_doc_count" : 744,
"buckets" : [ {
"key" : "finance",
"doc_count" : 30
}]
}
}
}
正如我们所见,hits 数组是空的。我不确定如何获得这些命中数组。有什么建议吗?
命中为空,因为您在指定时将返回查询的大小设置为 0:
"size": 0,
您可以完全删除大小,在这种情况下,您将获得 10 次默认匹配,或者您可以设置您想要的大小,例如,如果您指定 100,您将获得 100 次匹配作为响应。这与搜索结果有关。
现在,如果您还想在聚合中获得结果,您可以使用 Top Hits Aggregation。