How do I group documents in elasticsearch by a single field? -


if have bunch of documents in elaticsearch want returned grouped 1 field of document, how do it? need consistently return fixed number of results (using set maxresults)

for example if have bunch of documents each document representing person , fields of document containing attributes of person. let's each person has city field in document. query elasticsearch in way return 50 results grouped city. 50 results want know how possible return 50 cities mapped people in cities.

i found implementation in:

http://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html

but want apply pagination these results well. dont see setoffset , setlimit possibility in es. ideas?

how possible return 50 cities mapped people in cities.

query looking looks 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                     }                 }             }         }     } }' 

in elastica, equivalent query created 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)         ) ); 

if want paginate results, change arguments passed setfrom , setsize.


Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -