# Elastic Search query to get the list of documents with some minimum occurrence of a property

**URL:** <https://discuss.elastic.co/t/elastic-search-query-to-get-the-list-of-documents-with-some-minimum-occurrence-of-a-property/284476>\
**Category:** Elasticsearch\
**Created:** [September 17, 2021, 10:50am UTC](https://discuss.elastic.co/t/elastic-search-query-to-get-the-list-of-documents-with-some-minimum-occurrence-of-a-property/284476 "2021-09-17T10:50:27Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![pranay\_jain](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pranay_jain/32/92747_2.png) [@pranay\_jain](https://discuss.elastic.co/u/pranay_jain)\
**Post date:** [September 17, 2021, 10:50am UTC](https://discuss.elastic.co/t/elastic-search-query-to-get-the-list-of-documents-with-some-minimum-occurrence-of-a-property/284476/1 "2021-09-17T10:50:27Z")

</div>

I have a index with documents like this

```auto
[
 {
   "customer_id" : "123",
   "country": "USA",
   "department": "IT",
   "creation_date" : "2021-06-23"
   ...
 },
 {
   "customer_id" : "123",
   "country": "USA",
   "department": "IT",
   "creation_date" : "2021-06-24"
   ...
 },
 {
   "customer_id" : "345",
   "country": "USA",
   "department": "IT",
   "creation_date" : "2021-06-25"
   ...
 }
]

```

I want to get the list of all documents from specific country e.g USA, between a give time range with at least 2 occurrences of same customer\_id. With the above data, it should return

```auto
[
 {
   "customer_id" : "123",
   "country": "USA",
   "department": "IT",
   "creation_date" : "2021-06-24"
   ...
 }
]

```

Now, I tried the below ES query

```auto
POST /index_name/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "creation_timestamp": {
              "gte": "2021-06-23",
              "lte": "2021-08-23"
            }
          }
        },
        {
          "match": {
            "country": "USA"
          }
        }
      ]
    }
  },
  "aggs": {
    "customer_agg": {
      "terms": {
        "field": "customer_id",
        "min_doc_count": 2
      }
    }
  }
}

```

The above query returns following result

```auto
"hits" : {
    "total" : {
      "value" : 10000,
      "relation" : "gte"
    },
    "max_score" : 1.5587491,
    "hits" : [...]
    ]
  },
  "aggregations" : {
    "person_agg" : {
      "doc_count_error_upper_bound" : 1,
      "sum_other_doc_count" : 1,
      "buckets" : [
        {
          "key" : "customer_id",
          "doc_count" : 2
        }
      ]
    }
  }

```

I don't need the list of buckets in response, but only the list of documents satisfying the condition. How can I achieve it? The documents in the hits section in the response doesn't match with the data in the aggregation bucket.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [October 15, 2021, 10:50am UTC](https://discuss.elastic.co/t/elastic-search-query-to-get-the-list-of-documents-with-some-minimum-occurrence-of-a-property/284476/2 "2021-10-15T10:50:45Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
