1

What is the difference between the Query Context and the Filter Context in the Elastic Search in Query DSL.

My Understanding is Query Context- How well the document matches the query parameters.

Ex:

    { "match": { "title":   "Search"        }}

If I am searching for the documents with title 'Search' then if I contains two documents

      i)title:"Search"    
      ii)title:"Search 123"

Then first document is a perfect match and document two is a semi-match. Then the first document is given in the first place and the second document given the second place. Is my understanding correct?

Filter Context:
Ex:

{ "term":  { "status": "published" }}

If I am searching for the documents with status 'published' then if I contains two documents

      i)status:"published"    
      ii)status:"published 123"

Then the first document is perfect so it is returned and the second match is not a perfect match so it is not returned. Is my understanding correct?

Devid Farinelli
  • 7,514
  • 9
  • 42
  • 73
Jack Daniel
  • 2,527
  • 3
  • 31
  • 52
  • 1
    This thread should help: http://stackoverflow.com/questions/14595988/queries-vs-filters – Val Jul 21 '16 at 07:16
  • Is my understanding correct then? – Jack Daniel Jul 21 '16 at 07:26
  • 1
    Filters can be used to select document based on a Yes/No answer (and there's no scoring), whereas queries are mainly used for full-text search (and scoring). The main idea is to **filter** documents as much as possible so that **queries** can be executed on as least documents as possible. So your understanding is correct. – Val Jul 21 '16 at 07:48
  • No your are not correct, if the standard analyser is used for the status field, the second document also matches. There are multiple differences between query and filter context, but the analysing side is not different from the index perspective. It is from the query perspective. It would be different if you were searching for Published and published. – Jettro Coenradie Jul 21 '16 at 07:49
  • The thing is that filter answers the question with YES or NO. Query tells you "how much". Filter will return for you results with score 1.0, when Query will give you some number which you can debug with _explain API – ruhungry Jul 21 '16 at 08:53

2 Answers2

4

Basically in Query context, the elastic search scans all the documents and tries to find out how well the documents match the query, means the score will will be calculated for each documents. Where as in filter context,it will just checks whether the documents matches the query or not i.e, only yes or no will be returned. The filter queries does not contribute to the score of the document.

Next coming to the difference between the match and term queries , if you mapped a field to keyword then that field will be not analysed and its inverted index contains the whole term as it is, i.e is if status is mapped to keyword then if you insert "published 123" in status field , then its inverted index contains ["published 123"] and if status is mapped to text then while inserting data to status filed it is analysed for ex: if you insert "published 123" then its inverted index will be ["published","123"]. So whenever you use term query for keyword fields the query string will not be analysed and it tries to find exact term in the inverted index and if you use match query it analyses the query string and it returns all the doc's that contain the one of the analysed string of query in it's inverted index

Siddu
  • 156
  • 7
0

Your understanding about the difference between term and match queries is correct at the most basic level but like Jettro commented in the filter query you mentioned both the documents will be selected. When doing a term query it really depends what kind of analyzer you are using and how that affects the terms that are stored in inverted index that lucene uses. To quote an example from the Elasticsearch: Th Definitive Guide "if you were to index ["Foo","Bar"] into an exact value not_analyzed field, or Foo Bar into an analyzed field with the whitespace analyzer, both would result in having the two terms Foo and Bar in the inverted index."

Now under the hood the term query will search all the terms in the inverted index for your query term and even if one of them matches it will be returned as a result. So in the first case there is only "published" in the inverted index but in the second case too there are both terms "published" and "123", so both documents will be returned as matches.

It also is important to remember that the term query looks in the inverted index for the exact term only; it won’t match any variants like "Published" or "publisheD" with "published".