13

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

1 Answer 1

25

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 }}  
        }
    }
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.