Warm tip: This article is reproduced from stackoverflow.com, please click
elasticsearch

In which order elasticsearch filters applied?

发布于 2020-04-08 09:33:45

Currently I am using elasticsearch in my rails application. My concern is how filter works in query is there any priority or ranking that this filter will apply first and other one in last OR it applied from top to bottom OR bottom to top :-

I have an example query with filter's below :-

{
  "query": {
    "constant_score": {
      "filter": {
        "bool": {
          "must": [
            {
              "match_all": {

              }
            }
          ],
          "filter": [
            {
              "term": {
                "status": true
              }
            },
            {
              "geo_bounding_box": {
                "location": {
                  "top_left": {
                    "lat": 40.8888,
                    "lon": -73.888888
                  },
                  "bottom_right": {
                    "lat": 41.8888,
                    "lon": -74.88888
                  }
                }
              }
            },
            {
              "range": {
                "get_booking_cut_off_time": {
                  "gte": 5.46
                }
              }
            },
            {
              "terms": {
                "show_to_list_val": [
                  "Both",
                  "Direct"
                ]
              }
            },
            {
              "range": {
                "min_days": {
                  "lte": 1
                }
              }
            },
            {
              "terms": {
                "midoffice_master_id": [
                  10,
                  14
                ]
              }
            }
          ]
        }
      }
    }
  },
  "_source": {
    "includes": [
      "name",
      "code"
    ]
  },
  "size": 20,
  "from": 0
} 

So in above example I have term filter, geo_bounding_box filter and range filter. I want to know that which filter should be apply first or last when query hits the elasticsearch api ??

Any help would be appreciable... :)

Questioner
code_aks
Viewed
74
apt-get_install_skill 2020-02-01 17:37

Take a look at this very detailed blog post from Elastic about the inner workings of query- and filter-execution:

https://www.elastic.co/de/blog/elasticsearch-query-execution-order

In the Conclusion-section at the end they state (quote):

Q: Does the order in which I put my queries/filters in the query DSL matter?

A: No, because they will be automatically reordered anyway based on their respective costs and match costs.

Hope this helps!