【问题标题】:How to give different weights to exact, phonetic and fuzzy queries?如何为精确、语音和模糊查询赋予不同的权重?
【发布时间】:2020-07-01 08:32:47
【问题描述】:

注意:我查看了this的答案,但无法解决问题。

所以目前我正在使用以下查询:

{
    "_source": [
        "title",
        "bench",
        "id_",
        "court",
        "date"
    ],
    "size": 15,
    "from": 0,
    "query": {
        "bool": {
            "must": {
                "multi_match": {
                    "query": "knife",
                    "fields": [
                        "title",
                        "body"

                    ],
                    "operator": "and"
                }
            },
            "should": {
                "multi_match": {
                    "query": "knife",
                    "fields": [
                        "title",
                        "body"
                    ],
                    "fuzziness" : 1,
                    "operator": "and"
                }
            }
        }
    },
    "highlight": {
        "pre_tags": [
            "<tag1>"
        ],
        "post_tags": [
            "</tag1>"
        ],
        "fields": {
            "content": {}
        },
        "fragment_size": 30
    }
}

我想要实现的是,我想按照精确 > 模糊 > 语音的顺序为精确、语音和模糊查询赋予不同的权重。我如何做到这一点?

这是我的映射 - (我的分析器是 Metaphone 分析器)

{
    "courts_2": {
        "mappings": {
            "properties": {
                "author": {
                    "type": "text",
                    "analyzer": "my_analyzer"
                },
                "bench": {
                    "type": "text",
                    "analyzer": "my_analyzer"
                },
                "citation": {
                    "type": "text"
                },
                "content": {
                    "type": "text",
                    "fields": {
                        "standard": {
                            "type": "text"
                        }
                    },
                    "analyzer": "my_analyzer"
                },
                "court": {
                    "type": "text"
                },
                "date": {
                    "type": "text"
                },
                "id_": {
                    "type": "text"
                },
                "title": {
                    "type": "text",
                    "fields": {
                        "standard": {
                            "type": "text"
                        }
                    },
                    "analyzer": "my_analyzer"
                },
                "verdict": {
                    "type": "text"
                }
            }
        }
    }
}

【问题讨论】:

    标签: elasticsearch elasticsearch-5 elasticsearch-plugin


    【解决方案1】:

    您可以在单独的子字段中索引语音字段,如下所示:

       "mappings": {
        "properties": {
          "title": {
            "type": "text",
            "fields": {
              "phonetic": {
                "type": "text",
                "analyzer": "my_analyzer"
              }
            }}}}
    

    然后,您可以执行Function score 查询以获得精确> 模糊> 拼音的顺序:

    {
      "_source": [
        "title",
        "bench",
        "id_",
        "court",
        "date"
      ],
      "size": 15,
      "from": 0,
      "query": {
        "bool": {
          "should": [
            {
              "function_score": {
                "query": {
                  "multi_match": {
                    "query": "knife",
                    "fields": [
                      "title",
                      "body"
                    ],
                    "operator": "and"
                  }
                },
                "boost": 3
              }
            },
            {
              "function_score": {
                "query": {
                  "multi_match": {
                    "query": "knife",
                    "fields": [
                      "title",
                      "body"
                    ],
                    "fuzziness": 1,
                    "operator": "and"
                  }
                },
                "boost": 2
              }
            },
            {
              "function_score": {
                "query": {
                  "multi_match": {
                    "query": "knife",
                    "fields": [
                      "title.phonetic",
                      "body.phonetic"
                    ],
                    "operator": "and"
                  }
                },
                "boost": 1
              }
            }
          ]
        }
      }
    }
    

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 2019-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-03
      • 1970-01-01
      相关资源
      最近更新 更多