[ES from entry to actual combat] 14. Full-text search-ElasticSearch-advanced-bool compound query

Continue to section 13

6), bool [Compound query]

bool is used to do compound queries:

It is important to understand that compound statements can be combined with any other query statements, including compound statements.
This means that compound statements can be nested within each other and can express very complex logic.

  • must: Must meet all the conditions listed in must
GET /bank/_search
{
    
    
  "query": {
    
    
    "bool": {
    
    
      "must": [
        {
    
    
          "match": {
    
    
            "gender": "F"
          }
        },
        {
    
    
          "match": {
    
    
            "address": "Mill"
          }
        }
      ]
    }
  }
}

Insert picture description here

  • must_not: clause (query) must not appear in matching documents
GET /bank/_search
{
    
    
  "query": {
    
    
    "bool": {
    
    
      "must": [
        {
    
    
          "match": {
    
    
            "gender": "F"
          }
        },
        {
    
    
          "match": {
    
    
            "address": "Mill"
          }
        }
      ],
      "must_not": [
        {
    
    "match": {
    
    
          "age": 30
        }}
      ]
    }
  }
}

Insert picture description here

  • should: The clause (query) should appear in the matching document. (Should means it’s best to have it, it’s okay to have none)
GET /bank/_search
{
    
    
  "query": {
    
    
    "bool": {
    
    
      "must": [
        {
    
    
          "match": {
    
    
            "gender": "M"
          }
        },
        {
    
    
          "match": {
    
    
            "address": "Mill"
          }
        }
      ],
      "must_not": [
        {
    
    "match": {
    
    
          "age": 30
        }}
      ],
      "should": [
        {
    
    "match": {
    
    
          "lastname": "Holland"
        }}
      ]
    }
  }
}

Insert picture description here

Boolean query

Queries that match documents that match Boolean combinations of other queries. Boolean queries are mapped to Lucene BooleanQuery. It is constructed using one or more Boolean clauses, and each clause has an occurrence of a type. The type of occurrence is:

occur description
must The clause (query) must appear in the matching document and will contribute to the score.
filter The clause (query) must appear in the matching document. But unlike mustscores of the query will be ignored. The Filter clause is executed in the filter context , which means that scoring is ignored and the clause is considered for caching.
should The clause (query) should appear in the matching document.
must_not The clause (query) must not appear in matching documents. The clause is executed in the filter context , which means that the scoring is ignored and the clause is considered to be used for caching. Since scoring is ignored, 0the scores for all documents will be returned.

Reference document-query-dsl-bool-query

The next section we look at filterthe results of the filter.

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/106346318