ElasticSearch query DSL ~ compound query

ElasticSearch query DSL ~ compound query

ElasticSearch compound query classification

  1. bool query
    POST _search
    {
          
          
      "query": {
          
          
        "bool" : {
          
          
          "must" : {
          
          },
          "filter": {
          
          },
          "must_not" : {
          
          },
          "should" : {
          
          },
          "minimum_should_match" : 1,
          "boost" : 1.0
        }
      }
    }
    

    Constructed by one or more Boolean clauses, each of which has a typed appearance. The appearance types are:

    name desc
    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 context of the filter , 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 context of the filter , which means that the scoring is ignored, and the clause is considered to be used for caching. Because the scoring is ignored, 0the scores for all documents will be returned.
    minimum_should_match You can use this minimum_should_matchparameter to specify the number or percentage of clauses that the shouldreturned document must match.
    If the boolquery contains at least one shouldclause, but there is no mustor filterclause, the default value 1. Otherwise, the default value is 0.

    The boolquery uses a "better match" approach, so the scores of each match mustor shouldclause will be added together to provide _scorethe final result for each document.

    Other parameters

    name dsec
    _name Named query (one more "matched_queries" in the result): https://www.elastic.co/guide/en/elasticsearch/reference/7.x/query-dsl-bool-query.html
  2. boosting query
    GET /_search
    {
          
          
      "query": {
          
          
        "boosting": {
          
          
          "positive": {
          
          },
          "negative": {
          
          },
          "negative_boost": 0.5
        }
      }
    }
    

    Returns positivethat match the query document, while reducing the negative documents that match the query relevance score .

    You can use boostingqueries to downgrade certain documents without having to exclude them from the search results.

    name dsec
    positive Query that documents must match (required)
    negative Query to reduce document relevance (distinguishable from filtering) (required)
    negative_boost A floating point number between 0 and 1.0, used to reduce the relevance score of documents matching negative queries. (Required)

    Match the intersection of positive and negative documents at the same time, multiply the score in positive by negative_boost

  3. constant_score query
    GET /_search
    {
          
          
      "query": {
          
          
        "constant_score": {
          
          
          "filter": {
          
          },
          "boost": 1.2
        }
      }
    }
    

    Pack the filter query, and the scores of the queried documents are the scores specified by boost

    name dsec
    filter Filter query (required)
    boost Relative score, the default value of floating point number is 1.0 (optional)
  4. dis_max query
    GET /_search
    {
          
          
      "query": {
          
          
        "dis_max": {
          
          
          "queries": [
            {
          
           "term": {
          
           "title": "Quick pets" } },
            {
          
           "term": {
          
           "body": "Quick pets" } }
          ],
          "tie_breaker": 0.7
        }
      }
    }
    

    Return any document that matches any query as the result, but only the score with the best match is returned as the scoring result of the query

    name dsec
    queries Contains one or more query clauses. The returned documents must match one or more of these queries. If a text
    file matching multiple queries, Elasticsearch will use the highest relevance score. (Query object array) (required)
    tie_breaker Floating point number 0~1.0, a document matches multiple clauses at the same time, select the score of the clause with the highest score, and then
    multiply the scores of all other matching clauses by the sum of tie_breaker and add the highest score (optional default value 0.0)
  5. function_score query
    GET /_search
    {
          
          
      "query": {
          
          
        "function_score": {
          
          
          "query": {
          
           "match_all": {
          
          } },
          "boost": "5", 
          "functions": [
            {
          
          
              "filter": {
          
           "match": {
          
           "test": "bar" } },
              "random_score": {
          
          }, 
              "weight": 23
            },
            {
          
          
              "filter": {
          
           "match": {
          
           "test": "cat" } },
              "weight": 42
            }
          ],
          "max_boost": 42,
          "score_mode": "max",
          "boost_mode": "multiply",
          "min_score": 42
        }
      }
    }
    

    Customize the scoring function, and perform further scoring on the original scoring result, that is, to modify the original scoring result through the specified rules and the final scoring result. For example, if you search a blog by keywords, after the blog gets the score of the document through the keyword, you need to further modify the score of the document by the number of likes.

    name dsec
    boost_mode Single custom scoring function, modify the calculation method of the score (multiply, replace, sum, avg, max, min)
    score_mode Multiple custom scoring functions, modify the calculation method of scores (multiply, replace, sum, avg, max, min)
    max_boost Upper limit of score
    min_score Lower limit of score
    functions 打分函数(weight,random_score,field_value_factor,decay_function,script_score)

​ Reference address: https://www.elastic.co/guide/en/elasticsearch/reference/7.x/compound-queries.html, https://segmentfault.com/a/1190000037700644

Guess you like

Origin blog.csdn.net/weixin_41237676/article/details/113261601