Customize query


(Remco Zwaan) #1

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


(Zachary Tong) #2

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.


(Remco Zwaan) #3

Hi Zachary,

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

ciao..remco


(Zachary Tong) #4

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

{
  "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.


(Remco Zwaan) #5

thanks man!


(system) #6

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