Elasticsearch 查询权重(qbit)

前言

  • 本文对 Elasticsearch 7.17 适用

提升字段的权重

multi_match

  • 默认 type 为 best_fields

    GET /_search
    {
    "query": {
      "multi_match": {
        "query": "this is a test",
        "fields": ["subject^3", "message"]
      }
    }
    }
  • most_fields

    GET /_search
    {
    "query": {
      "multi_match": {
        "query": "this is a test",
        "type": "most_fields",
        "fields": ["subject^3", "message"]
      }
    }
    }
    // 等价于
    GET /_search
    {
    "query": {
      "bool": {
        "should": [
          {
            "match": { "subject": { "query": "this is a test", "boost": 3 }}
          },
          {
            "macth": { "message": "this is a test" }
          }
        ]
      }
    }
    }

query_string

GET /_search
{
  "query": {
    "query_string": {
      "fields": ["content", "name^5"],
      "query": "this AND that OR thus"
    }
  }
}

simple_query_string

GET /_search
{
  "query": {
    "simple_query_string": {
      "query": """"fried eggs" +(eggplant | potato) -frittata""",
      "fields": ["title^5", "body"],
      "default_operator": "and"
    }
  }
}

提升子句的权重

  • bool 查询

    GET /_search
    {
    "query": {
      "bool": {
        "should": [
          {
            "match": { "subject": { "query": "this is a test", "boost": 3 }}
          },
          {
            "macth": { "message": "this is a test" }
          }
        ]
      }
    }
    }

提升索引的权重

  • indices_boost

    GET /_search
    {
      "indices_boost" : [
          { "alias1" : 1.4 },
          { "index*" : 1.3 }
      ]
    }

Function score query

  • ElasticSearch 相关性打分机制
  • 年份越大,分值越高

    GET /_search
    {
    "query": {
      "function_score": {
        "field_value_factor": {
          "field": "year",
          "factor": 1.2,
          "modifier": "sqrt",
          "missing": 1
        }
      }
    }
    }
  • 提升英文文档权重(固定字段值)

    GET /_search
    {
    "query": {
      "function_score": {
        "query": {
          "match": {
            "title": "data"
          }
        },
        "script_score": {
          "script": "return doc['language'].value == 'EN' ? 1.5 : 1.0"
        }
      }
    }
    }
  • 提升英文文档权重(字段值传参)

    GET zt_title_info_alias/_search
    {
    "query": {
      "function_score": {
        "query": {
          "match": {
            "title": "data"
          }
        },
        "script_score": {
          "script": {
            "params": {
              "recommend_language": "EN"
            },
            "source": "return doc['language'].value == params.recommend_language ? 1.5 : 1.0"
          }
        }
      }
    }
    }

Script score query

  • 注意 function_score 子查询的 script_score 不同于一级查询中的 script_score,前者的返回值乘以原 score 为最终 score,后者的返回值即为最终 score
  • 示例,给 score 加 10 分

    {
    "query": {
      "script_score": {
        "query": {
          "match": {
            "title": "data"
          }
        },
        "script": {
          "lang": "painless",
          "source": "_score + 10;"
        }
      }
    }
    }
本文出自 qbit snap

你可能感兴趣的:(Elasticsearch 查询权重(qbit))