The difference between Query and Filter in Elasticsearch DSL

Elasticsearch supports many query methods. In addition to querying through the TCP protocol through port 9300 (default), the other is DSL, which writes the request in JSON and then performs related queries.

A DSL example

GET _search
{
  "query": { 
    "bool": { 
      "must": [
        { "match": { "name":   "Jim" }}, 
        { "match": { "city": "Guangzhou" }}  
      ],
      "filter": [ 
        { "term":  { "weight": "60" }}, 
        { "range": { "age": { "gte": "18"}}} It is the execution environment when using query to query, such as when using search.1. Query context:query in the Query query context and Filter filter In the context, the operations performed are different:Query and FilterThis query is used in conjunction with other leaf queries or compound queries to logically form more complex queries, such as boolas , range, etcCompound query clausesThis query can be used alone to query a specific value for a specific field, such as match, termLeaf query Clause:The DSL in Elasticsearch is mainly composed of two parts:Query types}  }    }
      ]




















In the query context, the query will answer the question - " Does this document match this query, and is it highly relevant? "
The data indexed in ES will store a _score score, and the higher the score, the better the match. Even if Lucene uses an inverted index, it still takes a certain amount of time to calculate the score for a search.

2. Filter context: The execution environment when the filter parameter is used, such as using Must_not in a bool query or filter
in the filter context, the query will answer the question - " Does this document match? "
It does not calculate any Score, and will not care about the returned sorting problem, so the efficiency will be higher.
In addition, when filters are often used, ES will automatically cache the content of the filter, which will improve a lot of performance for queries.

All in all:
1 Query context: The query operation will not only perform a query, but also calculate a score to determine the relevance;

2 Filter context: The query operation only determines whether the query conditions are met, and does not calculate the score, and the query results can be cached.

Therefore, according to the actual requirements, whether it is necessary to obtain scores, consider performance factors, and choose different query clauses.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326228367&siteId=291194637