How to give different weights to exact, phonetic and fuzzy queries?

主宰稳场 提交于 2020-04-17 22:37:10

问题


Note: I checked out this answer, but could not solve the problem.

So currently I am using the following query:

{
    "_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
    }
}

What I want to achieve is that I want to give different weights to exact, phonetic and fuzy queries in the order exact > fuzzy > phonetic. How do I acheive this?

This is my mapping - (My analyzer is a Metaphone analyzer)

{
    "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"
                }
            }
        }
    }
}

回答1:


You might index phonetic fields on an separate sub-field as follow :

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

Then, you can do a Function score query to have the order exact > fuzzy > phonetic :

{
  "_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
          }
        }
      ]
    }
  }
}

Hope this helps !



来源:https://stackoverflow.com/questions/60770188/how-to-give-different-weights-to-exact-phonetic-and-fuzzy-queries

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!