# ElasticSearch - date range condition should match EXACTLY one item from date range array

**URL:** https://discuss.elastic.co/t/elasticsearch-date-range-condition-should-match-exactly-one-item-from-date-range-array/53071
**Category:** Elasticsearch
**Created:** [June 16, 2016, 11:19pm UTC](https://discuss.elastic.co/t/elasticsearch-date-range-condition-should-match-exactly-one-item-from-date-range-array/53071 "2016-06-16T23:19:05Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![pungent](https://avatars.discourse-cdn.com/v4/letter/p/a698b9/32.png) [@pungent](https://discuss.elastic.co/u/pungent)
#### Post date: [June 16, 2016, 11:19pm UTC](https://discuss.elastic.co/t/elasticsearch-date-range-condition-should-match-exactly-one-item-from-date-range-array/53071/1 "2016-06-16T23:19:05Z")

</div>

I am planning to store million of airbnb type apartments availabilty in elasticsearch .  
Where `availabilty` is an array that contains `nested` objects (`availability` type is `nested`).  
And each of those objects have date range, in which that apartment is available.

```
    apartments = [
  { 
    "_id": "kjty873yhekrg789e7r0n87e",
    "first_available_date": "2016-06-21",
    "availability": [
      {
        "start": "2016-06-21",
        "end": "2016-08-01"
      },
      {
        "start": "2016-08-20",
        "end": "2016-08-28"
      },
      {
        "start": "2016-10-03",
        "end": "2016-11-02"
      },
      { //This means it is available only for one day.
        "start": "2016-11-13",
        "end": "2016-11-13"
      },
      { 
        "start": "2016-11-28",
        "end": "2017-01-14"
      } 
    ],
    "apartment_metadata1": 56456,
    "apartment_metadata2": 8989,
    "status": "active"
  },
  { 
    "_id": "hgk87783iii86937jh",
    "first_available_date": "2016-06-09",
    "availability": [
      {
        "start": "2016-06-09",
        "end": "2016-07-02"
      },
      {
        "start": "2016-07-21",
        "end": "2016-12-19"
      },
      {
        "start": "2016-12-12",
        "end": "2017-07-02"
      }
    ],
    "apartment_metadata1": 23534,
    "apartment_metadata2": 24377,
    "status": "active"
  }
]

```

I would want to search apartments those are available for a specific date range (say `2016-08-20 to 2016-12-12`). And that  
range should fall inside one of the availability date ranges of various apartments.

So I want to write a query, something like:

```
{
  "query": {
    "bool": {
      "must": [
        {
          "range": { "first_available_date": {"lte": "2016-08-20"} },
          "match": { "status": "active" }
        }
      ]
      },
      "filter": [
        {
          "range": 
            {
              "apartments.availability.start": {"gte": "2016-08-20"}, 
              "apartments.availability.end": {"lte": "2016-12-12"} 
            }
        }
     ]
    }
  }
}

```

And above query will return me both apartments (with MULTIPLE `availability` objects matching the condition),  
and that is incorrect, it should only return document with `_id: hgk87783iii86937jh` as there is EXACTLY one `availability` object matches the creiteria and that is `{"start": "2016-07-21", "end": "2016-12-19"}`. So in order to have correct result, the condition should be - there should be EXACTLY one `availability` object in apartment doc  
that should match the condition. So how to enforce that there should be EXACTLY one match in the above query? Second question - is my query even correct?

---

<div class="post-metadata">

### Author: ![Glen\_Smith](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/glen_smith/32/111656_2.png) [@Glen\_Smith](https://discuss.elastic.co/u/Glen_Smith)
#### Post date: [June 17, 2016, 12:18am UTC](https://discuss.elastic.co/t/elasticsearch-date-range-condition-should-match-exactly-one-item-from-date-range-array/53071/2 "2016-06-17T00:18:38Z")

</div>

[See if this clears up the issue](https://www.elastic.co/guide/en/elasticsearch/guide/current/nested-objects.html) [1]

[1] [https://www.elastic.co/guide/en/elasticsearch/guide/current/nested-objects.html](https://www.elastic.co/guide/en/elasticsearch/guide/current/nested-objects.html)

---

<div class="post-metadata">

### Author: ![pungent](https://avatars.discourse-cdn.com/v4/letter/p/a698b9/32.png) [@pungent](https://discuss.elastic.co/u/pungent)
#### Post date: [June 17, 2016, 2:28am UTC](https://discuss.elastic.co/t/elasticsearch-date-range-condition-should-match-exactly-one-item-from-date-range-array/53071/3 "2016-06-17T02:28:05Z")

</div>

@Glen_Smith thanks for the link. But there I did not find anything that explain, how to find those documents where EXACTLY one nested object match my filter

---

<div class="post-metadata">

### Author: ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)
#### Post date: [June 17, 2016, 7:28am UTC](https://discuss.elastic.co/t/elasticsearch-date-range-condition-should-match-exactly-one-item-from-date-range-array/53071/4 "2016-06-17T07:28:42Z")

</div>

Hey,

this looks rather like a duplicate of [How to store disjoint date ranges and then search for a specific range](https://discuss.elastic.co/t/how-to-store-disjoint-date-ranges-and-then-search-for-a-specific-range/52935/2)

--Alex

---

<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: [July 5, 2017, 10:42pm UTC](https://discuss.elastic.co/t/elasticsearch-date-range-condition-should-match-exactly-one-item-from-date-range-array/53071/5 "2017-07-05T22:42:53Z")

</div>


