bool查询中不同的得分函数

时间:2019-06-24 12:50:05

标签: elasticsearch

我正在处理Elasticsearch的产品索引。我有一个包含多个匹配项的布尔查询,用于检索我想要的结果:

{
  "query": {
    "bool": {
      "should": [
        {
          "multi_match": {
            "query": "term",
            "fields": [
              "name^3.0",
              "name.fullName^10.0",
              "description",
              "description.fullDesc",
              "detail",
              "detail.fullDetail"
            ],
            "type": "cross_fields",
            "operator": "AND",
            "slop": 0,
            "prefix_length": 0,
            "max_expansions": 50,
            "zero_terms_query": "NONE",
            "auto_generate_synonyms_phrase_query": true,
            "fuzzy_transpositions": true,
            "boost": 10
          }
        },
        {
          "multi_match": {
            "query": "term",
            "fields": [
              "name.fullName",
              "description.fullDesc",
              "detail.fullDetail"
            ],
            "type": "cross_fields",
            "operator": "OR",
            "slop": 0,
            "prefix_length": 0,
            "max_expansions": 50,
            "zero_terms_query": "NONE",
            "auto_generate_synonyms_phrase_query": true,
            "fuzzy_transpositions": true,
            "boost": 6
          }
        }
      ],
      "adjust_pure_negative": true,
      "boost": 1
    }
  }
}

我需要对在多匹配查询中检索的每组文档分别进行评分。我已经考虑过使用分数函数,但是它允许我为整个布尔查询定义一个分数函数,如下所示:

{
  "from": 0,
  "size": 500,
  "query": {
    "function_score": {
      "query": {
        "bool": {
          "should": [
            {
              "multi_match": {
                "query": "term",
                "fields": [
                  "name^3.0",
                  "name.fullName^10.0",
                  "description",
                  "description.fullDesc",
                  "detail",
                  "detail.fullDetail"
                ],
                "type": "cross_fields",
                "operator": "AND",
                "slop": 0,
                "prefix_length": 0,
                "max_expansions": 50,
                "zero_terms_query": "NONE",
                "auto_generate_synonyms_phrase_query": true,
                "fuzzy_transpositions": true,
                "boost": 10
              }
            },
            {
              "multi_match": {
                "query": "term",
                "fields": [
                  "name.fullName",
                  "description.fullDesc",
                  "detail.fullDetail"
                ],
                "type": "cross_fields",
                "operator": "OR",
                "slop": 0,
                "prefix_length": 0,
                "max_expansions": 50,
                "zero_terms_query": "NONE",
                "auto_generate_synonyms_phrase_query": true,
                "fuzzy_transpositions": true,
                "boost": 6
              }
            }
          ],
          "adjust_pure_negative": true,
          "boost": 1
        }
      },
      "boost": "5",
      "field_value_factor": {
        "field": "gain",
        "factor": 1.2,
        "modifier": "sqrt",
        "missing": 1
      },
      "boost_mode": "multiply"
    }
  },
  "version": true
}

我需要定义不同的得分函数(每个多重匹配查询一个)来独立对结果进行得分。

1 个答案:

答案 0 :(得分:1)

您可以在multi_match查询中包装每个function_score查询。然后,您可以轻松地为每个multi_match定义不同的功能。这就是查询的样子:

{
  "from": 0,
  "size": 500,
  "query": {
    "bool": {
      "should": [
        {
          "function_score": {
            "query": {
              "multi_match": {
                "query": "term",
                "fields": [
                  "name^3.0",
                  "name.fullName^10.0",
                  "description",
                  "description.fullDesc",
                  "detail",
                  "detail.fullDetail"
                ],
                "type": "cross_fields",
                "operator": "AND",
                "slop": 0,
                "prefix_length": 0,
                "max_expansions": 50,
                "zero_terms_query": "NONE",
                "auto_generate_synonyms_phrase_query": true,
                "fuzzy_transpositions": true,
                "boost": 10
              }
            },
            "boost": "5",
            "field_value_factor": {
              "field": "gain",
              "factor": 1.2,
              "modifier": "sqrt",
              "missing": 1
            },
            "boost_mode": "multiply"
          }
        },
        {
          "function_score": {
            "query": {
              "multi_match": {
                "query": "term",
                "fields": [
                  "name.fullName",
                  "description.fullDesc",
                  "detail.fullDetail"
                ],
                "type": "cross_fields",
                "operator": "OR",
                "slop": 0,
                "prefix_length": 0,
                "max_expansions": 50,
                "zero_terms_query": "NONE",
                "auto_generate_synonyms_phrase_query": true,
                "fuzzy_transpositions": true,
                "boost": 6
              }
            },
            "boost": "5",
            "field_value_factor": {
              "field": "gain",
              "factor": 1.2,
              "modifier": "sqrt",
              "missing": 1
            },
            "boost_mode": "multiply"
          }
        }
      ],
      "adjust_pure_negative": true,
      "boost": 1
    }
  },
  "version": true
}

请注意,在以上查询中,我在两个multi_match查询中都使用了相同的得分逻辑。您可以根据需要对其进行修改。