如何将Elasticsearch中的文档按单个字段分组? [英] How do I group documents in elasticsearch by a single field?
问题描述
如果我在elaticsearch中有一堆文件想要按文件的一个字段分组归还,我该怎么做?我还需要它来一致地返回固定数量的结果(使用set maxresults)
If I have a bunch of documents in elaticsearch that I want to be returned grouped by the one field of the document, how do I do it? Also I need it to consistently return a fixed number of results (using set maxresults)
例如,如果我有一堆文件,则每个文件代表一个人,而文件中包含该人属性的字段. 假设每个人在文档中都有一个城市字段. 我想以某种方式查询Elasticsearch,该方式将返回按城市分组的50个结果.通过50个结果,我想知道如何能够返回映射到这些城市中所有人的50个城市.
For example if I have a bunch of documents each document representing a person and fields of the document containing attributes of the person. Let's say each person has a city field in the document. I would like to query Elasticsearch in a way that will return 50 results that are grouped by city. By 50 results I want to know how it is possible to return 50 cities mapped to all the people in those cities.
我在以下位置找到了一个实现:
I found an implementation in:
但是我也想对这些结果应用分页.我在ES中看不到setOffset和setLimit的可能性.想法?
But i want to apply pagination to these results as well. I dont see a setOffset and setLimit possibility in ES. Ideas?
推荐答案
如何能够返回映射到这些城市中所有人的50个城市.
how it is possible to return 50 cities mapped to all the people in those cities.
查询所要查找的内容如下:
Query you are looking for looks like this:
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中,可以通过以下方式创建等效查询:
In Elastica, equivalent query could be created this way:
$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
和setSize
的参数.
If you want to paginate results, just change arguments passed into setFrom
and setSize
.
这篇关于如何将Elasticsearch中的文档按单个字段分组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!