如果我在elaticsearch中有一堆文档,我想按文档的一个字段分组返回,我该怎么做?我还需要它始终返回固定数量的结果(使用 set maxresults)
例如,如果我有一堆文档,每个文档代表一个人,并且文档的字段包含该人的属性。
假设每个人在文档中都有一个城市字段。
我想以返回 50 个按城市分组的结果的方式查询 Elasticsearch。通过 50 个结果,我想知道如何返回映射到这些城市中所有人的 50 个城市。
我在以下位置找到了一个实现:
http://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html http://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html
但我也想对这些结果应用分页。我在 ES 中没有看到设置 Offset 和设置 Limit 的可能性。有想法吗?
如何返回 50 个城市的地图并显示这些城市中的所有人。
您正在寻找的查询如下所示:
curl -XGET 'http://localhost:9200/users/user/_search?pretty' -d '{
"aggs": {
"users-by-city": {
"terms": {
"field": "city",
"size": 50
},
"aggs": {
"top_tag_hits": {
"top_hits": {
"from": 0,
"size": 9000
}
}
}
}
}
}'
在 Elastica 中,可以通过以下方式创建等效查询:
$query = new Elastica\Query();
$qb = new Elastica\QueryBuilder();
$query->addAggregation(
$qb->aggregation()->terms('users-by-city')
->setField('city')
->setSize(50)
->addAggregation(
$qb->aggregation()->top_hits('top-hits-in-city')
->setFrom(0)
->setSize(9000)
)
);
如果你想对结果进行分页,只需更改传递给的参数setFrom
and setSize
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)