# Customize query

**URL:** <https://discuss.elastic.co/t/customize-query/132340>\
**Category:** Elasticsearch\
**Created:** [May 17, 2018, 1:19pm UTC](https://discuss.elastic.co/t/customize-query/132340 "2018-05-17T13:19:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![remco\_zwaan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/remco_zwaan/32/129991_2.png) [@remco\_zwaan](https://discuss.elastic.co/u/remco_zwaan)\
**Post date:** [May 17, 2018, 1:20pm UTC](https://discuss.elastic.co/t/customize-query/132340/1 "2018-05-17T13:20:00Z")

</div>

Hi ,

User search for availabilty 2 - 5 june . They want to stay for 3 nights. This is my query

`curl http://localhost:9200/lodgings_development/_search?pretty -H 'Content-Type: application/json' -d '{"aggs":{"beds":{"filter":{"bool":{"must":[{"term":{"available_on":"2018-06-02"}},{"term":{"available_on":"2018-06-03"}},{"term":{"available_on":"2018-06-04"}},{"term":{"available_on":"2018-06-05"}}]}},"aggs":{"beds":{"terms":{"field":"beds","size":1000}}}},"baths":{"filter":{"bool":{"must":[{"term":{"available_on":"2018-06-02"}},{"term":{"available_on":"2018-06-03"}},{"term":{"available_on":"2018-06-04"}},{"term":{"available_on":"2018-06-05"}}]}},"aggs":{"baths":{"terms":{"field":"baths","size":1000}}}}},"query":{"bool":{"must":{"match_all":{}},"filter":[{"term":{"available_on":"2018-06-02"}},{"term":{"available_on":"2018-06-03"}},{"term":{"available_on":"2018-06-04"}},{"term":{"available_on":"2018-06-05"}}]}},"timeout":"11s","_source":false,"size":10,"from":0}'`

result are ok. User gets all lodges as result that are available for 3 nights.

Question: How can i adjust the query..when user search for 3 nights, we show also lodges in result page that are available for 2 nights. ?.

So this condfition...user search based on input calendar minus -1 day. How can i arrange this in the query ? (example. User search for 6 nights ..show also lodges that are available only for 5 nigths, User search for 10 nights ..show also lodges that are available only for 9 nigths, ect ect)

thanks ..remco

---

<div class="post-metadata">

**Author:** ![polyfractal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polyfractal/32/48162_2.png) [@polyfractal](https://discuss.elastic.co/u/polyfractal)\
**Post date:** [May 17, 2018, 2:13pm UTC](https://discuss.elastic.co/t/customize-query/132340/2 "2018-05-17T14:13:50Z")

</div>

The easiest way is to take the "optional" day and move it out of the `filter` clause. Instead, place it in the `should` clause of the boolean. The date will no longer be required, but will match if it exists, so your query will return both `n` nights and `n-1` nights.

Note: I would avoid using all those `filter` aggregations. They are fairly slow, since internally we have to check the filter on each document. You could instead use a single `date_histogram` aggregation to partition the documents by day. It will be much faster.

---

<div class="post-metadata">

**Author:** ![remco\_zwaan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/remco_zwaan/32/129991_2.png) [@remco\_zwaan](https://discuss.elastic.co/u/remco_zwaan)\
**Post date:** [May 17, 2018, 2:42pm UTC](https://discuss.elastic.co/t/customize-query/132340/3 "2018-05-17T14:42:25Z")

</div>

Hi Zachary,

thanks....would be very helpful if have a example 😉 I just learning ES for a few weeks now.

ciao..remco

---

<div class="post-metadata">

**Author:** ![polyfractal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polyfractal/32/48162_2.png) [@polyfractal](https://discuss.elastic.co/u/polyfractal)\
**Post date:** [May 17, 2018, 3:36pm UTC](https://discuss.elastic.co/t/customize-query/132340/4 "2018-05-17T15:36:41Z")

</div>

Sure 🙂 Looking closer at your request, I'd probably do it like this:

```auto
{
  "aggs":{
    "beds":{
      "terms":{
        "field":"beds",
        "size":1000
      }
    },
    "baths":{
      "terms":{
        "field":"baths",
        "size":1000
      }
    }
  },
  "query":{
    "bool":{
      "must":{
        "filter":[
          {
            "range":{
              "available_on": {
                "gte": "2018-06-02",
                "lte": "2018-06-04"
              }
            }
          }
        ],
        "should": [
          {
            "range":{
              "available_on": {
                "gte": "2018-06-05",
                "lte": "2018-06-05"
            }
          }
        }
      ]
    }
}
  },
  "timeout":"11s",
  "_source":false,
  "size":10,
  "from":0
}

```

It replaces the `term` aggregations with `range` query, which will be faster on date fields than individual `term` queries. Inside the `filter` there is one range with the "mandatory" full range, and inside the `should` we put the optional day.

For the aggs, you don't need any of the filters at all. The only documents that are aggregated are those that match the query, so the docs in the aggs will be "pre-filtered" already.

---

<div class="post-metadata">

**Author:** ![remco\_zwaan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/remco_zwaan/32/129991_2.png) [@remco\_zwaan](https://discuss.elastic.co/u/remco_zwaan)\
**Post date:** [May 18, 2018, 1:34pm UTC](https://discuss.elastic.co/t/customize-query/132340/5 "2018-05-18T13:34:12Z")

</div>

thanks man!

---

<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:** [June 15, 2018, 1:34pm UTC](https://discuss.elastic.co/t/customize-query/132340/6 "2018-06-15T13:34:24Z")

</div>

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