I'm writing an API for aggregate some data in Elasticsearch, one of them is counting the documents. I'm using the terms filter and the terms aggreagtions with a lot of ID to get them. The problem is, Elasticsearch seems to not return a bucket with a term which is not exist in the query above. Is there any simple and efficient way to do that?

I've tried to follow this and it kinda solve the problem but it's not optimized by using a lot of term query. My API has to query with at least 50 IDs and using that much of term query seems not to be a good idea

For example, I used this to aggregate data:

{
    "size": 0,
    "aggs":{
      "matching_values_field1": {
         "filter": { 
            "terms" : { "myfield" : ["1", "2", "3","100"]}
         },
         "aggs": {
            "myfield" : {
                "terms" : { 
                    "field" : "myfield",
                    "min_doc_count": 0
                }                
            }
         }
      }
   }    
}

and the response just return the didn't include the 100 bucket

{
  "aggregations": {
      "matching_values_1": {
         "doc_count": 11,
         "myfield": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
               {
                  "key": 2,
                  "doc_count": 6
               },
               {
                  "key": 1,
                  "doc_count": 3
               },
               {
                  "key": 3,
                  "doc_count": 2
               },
               {
                  "key": 0,
                  "doc_count": 0
               },
               {
                  "key": 0.5,
                  "doc_count": 0
               },
               {
                  "key": 4,
                  "doc_count": 0
               },
               {
                  "key": 5,
                  "doc_count": 0
               },
               {
                  "key": 6,
                  "doc_count": 0
               },
               {
                  "key": 14,
                  "doc_count": 0
               }
            ]
         }
      }
   }
}

Thanks for your help!

0 Answers