Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get buckets count in elasticsearch aggregations

I am using elasticsearch to search a database with a lot of duplicates. I am using field colapse and it works, however it returns the amount of hits (including duplicates) and not the amount of buckets.

"aggs": {
        "uniques": {
            "terms": {
                "field": "guid"
            },
            "aggs": {
                "jobs": { "top_hits": { "_source": "title", "size": 1 }}  
            }
        }
    }

I can count the buckets by making another request using cardinality (but it only returns count, not the documents):

{
    "aggs" : {
        "uniques" : {
            "cardinality" : {
                "field" : "guid"
            }
        }
    }
}

Is there a way to return both requests (buckets + total bucket count) in one search?

Thanks

like image 241
peixotorms Avatar asked Jun 18 '15 13:06

peixotorms


1 Answers

You can combine both of these aggregations into 1 request.

{
   "aggs" : {
    "uniques" : {
        "cardinality" : {
            "field" : "guid"
        }
    },
    "uniquesTerms": {
        "terms": {
            "field": "guid"
        },
        "aggs": {
            "jobs": { "top_hits": { "_source": "title", "size": 1 }}  
        }
    }
}
like image 145
Roman Avatar answered Sep 21 '22 10:09

Roman