首页 > 解决方案 > 如何在 Elasticsearch 中为精确搜索提供比语音搜索更高的分数?

问题描述

我目前正在使用 Elasticsearch 的语音分析器。我希望查询给完全匹配的分数比拼音匹配的分数更高。这是我正在使用的查询:


{
    "query": {
        "multi_match" : {
            "query" : "Abhijeet",
            "fields" : ["content", "title"]




        }
    },         
     "size": 10,
     "_source": [ "title", "bench", "court", "id_" ],
     "highlight": {
        "fields" : {
            "title" : {},
            "content":{}
        }
    }

}


当我搜索 时Abhijeet,最热门的查询是Abhijit并且只是稍后才会Abhijeet出现。我希望始终首先出现精确匹配,然后是语音匹配。这可以做到吗?

编辑:

映射

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

这是我用来设置语音分析器的代码:

{
    "settings": {
        "index": {
            "analysis": {
                "analyzer": {
                    "my_analyzer": {
                        "tokenizer": "standard",
                        "filter": [
                            "lowercase",
                            "my_metaphone"
                        ]
                    }
                },
                "filter": {
                    "my_metaphone": {
                        "type": "phonetic",
                        "encoder": "metaphone",
                        "replace": true
                    }
                }
            }
        }
    },
    "mappings": {
        "properties": {
            "author": {
                "type": "text",
                "analyzer": "my_analyzer"
            },
            "bench": {
                "type": "text",
                "analyzer": "my_analyzer"
            },
            "citation": {
                "type": "text"
            },
            "content": {
                "type": "text",
                "analyzer": "my_analyzer"
            },
            "court": {
                "type": "text"
            },
            "date": {
                "type": "text"
            },
            "id_": {
                "type": "text"
            },
            "title": {
                "type": "text",
                "analyzer": "my_analyzer"
            },
            "verdict": {
                "type": "text"
            }
        }
    }
}

现在,我只想查询titleandcontent字段。在这里,我希望首先出现精确匹配,然后是语音匹配。

标签: elasticsearchelastic-stackelasticsearch-pluginelasticsearch-analyzers

解决方案


一般的解决方法是:

  • 使用-bool查询,
  • 在 must 子句中使用您的 ponectic 查询/查询,
  • 和 should 子句中的非语音查询/查询

如果您将索引的映射和设置包含在您的问题中,我可以更新答案。

更新:解决方法

A. 扩展您的映射以使用多字段titlecontent

"title": {
  "type": "text",
  "analyzer": "my_analyzer",
  "fields" : {
    "standard" : {
      "type" : "text"
    }
  }
},
...
"content": {
  "type": "text",
  "analyzer": "my_analyzer"
  "fields" : {
    "standard" : {
      "type" : "text"
    }
  }
},

B. 填充字段(例如,通过重新索引所有内容):

POST courts_2/_update_by_query

C. 调整您的查询以利用新引入的字段:

GET courts_2/_search
{
  "_source": ["title","bench","court","id_"],
  "size": 10,
  "query": {
    "bool": {
      "must": {
        "multi_match": {
          "query": "Abhijeet",
          "fields": ["title", "content"]
        }
      },
      "should": {
        "multi_match": {
          "query": "Abhijeet",
          "fields": ["title.standard", "content.standard"]
        }
      }
    }
  },
  "highlight": {
    "fields": {
      "title": {},
      "content": {}
    }
  }
}

推荐阅读