[ES from entry to actual combat] 15. Full text search-ElasticSearch-advanced-filter

Continue from section 14

7), filter [Result filtering]

Not all queries need to generate scores, especially those documents that are only used for "fitering" (filtering). In order not to calculate the score, Elasticsearch will automatically check the scene and optimize the execution of the query.
The query specified under the filter element has no effect on the score-the score is returned as 0. The score is only affected by the specified query.
To mustquery as an example:

GET /bank/_search
{
    
    
  "query": {
    
    
    "bool": {
    
    
      "must": [
        {
    
    "range": {
    
    
          "age": {
    
    
            "gte": 18,
            "lte": 30
          }
        }}
      ]
    }
  }
}

Insert picture description here

Use filterinstead of mustqueries should be noted that the use of filtercheck out the results and mustcheck out the result is the same, only difference is that there is no correlation score:

GET /bank/_search
{
    
    
  "query": {
    
    
    "bool": {
    
    
      "filter": {
    
    
        "range": {
    
    
          "age": {
    
    
            "gte": 18,
            "lte": 30
          }
        }
      }
    }
  }
}

Insert picture description here

So we shouldcan add after filteradding a filter:

GET /bank/_search
{
    
    
  "query": {
    
    
    "bool": {
    
    
      "must": [
        {
    
    
          "match": {
    
    
            "gender": "M"
          }
        },
        {
    
    
          "match": {
    
    
            "address": "Mill"
          }
        }
      ],
      "must_not": [
        {
    
    "match": {
    
    
          "age": 30
        }}
      ],
      "should": [
        {
    
    "match": {
    
    
          "lastname": "Holland"
        }}
      ],
      "filter": {
    
    
        "range": {
    
    
          "age": {
    
    
            "gte": 18,
            "lte": 30
          }
        }
      }
    }
  }
}

Insert picture description here

Reference document-query-dsl


reference:

Elasticsearch Reference

elastic

Getting started with the full-text search engine Elasticsearch

Guess you like

Origin blog.csdn.net/runewbie/article/details/106365550