3

I have a query that counts the number of images per user:

GET images/_search
{
 "query": {
    "bool": {
      "must": [
      {
        "term": {
           "appID.raw": "myApp"
      }
    }
  ]
 }
},
"size": 0,
"aggs": {
  "perDeviceAggregation": {
    "terms": {
       "field": "deviceID"    
    }
  }
 }
}

It basically works fine, but I would like to exclude all aggregation results for users that have less than 200 images. How can I tweak the query above to achieve this?

Thanks.

1
  • could you post your mapping and couple of sample documents? in which field image is stored? Oct 13, 2016 at 19:10

2 Answers 2

8

You can achieve this by using a Minimum Document Count option.

"aggs": {
      "perDeviceAggregation": {
         "terms": {
            "field": "deviceID",
            "min_doc_count": 200
         }
      }
   }
3

Add a filter aggregation to your terms aggregation with the query clause. Filter Aggregations

You can modify your above query to look like this.

{
    "query": {
        "bool": {
            "must": [
                {
                    "term": {
                        "appID.raw": "myApp"
                    }
                }
            ]
        }
    },
    "size": 0,
    "aggs": {
        "filtered_users_with_images_count": {
            "filter": {
                "term": {
                    "count": 200
                }
            },
            "aggs": {
                "perDeviceAggregation": {
                    "terms": {
                        "field": "deviceID"
                    }
                }
            }
        }
    }
}

You can modify the filter inside filtered_users_with_images_count to match documents with images greater than 200.

Please also consider to post your data mappings along with query to support your questions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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