gpt4 book ai didi

elasticsearch - Elasticsearch查询在多个字段上更倾向于完全匹配而不是部分匹配

转载 作者:行者123 更新时间:2023-12-03 00:10:50 25 4
gpt4 key购买 nike

我正在对具有多个字段的文档进行免费文本搜索。当我执行搜索时,我希望在任何标签上都具有完美匹配的文档具有更高的评分。有什么办法可以从查询中做到这一点?

例如,文档有两个字段,分别称为label-alabel-b,当我执行以下多重匹配查询时:

{
"query": {
"bool": {
"must": [
{
"multi_match": {
"query": "apple",
"type": "most_fields",
"fields": [
"label-a",
"label-b"
]
}
}
]
}
}
}

我得到以下结果(仅相关部分):
"hits": [
{
"_index": "salad",
"_type": "fruit",
"_id": "4",
"_score": 0.581694,
"_source": {
"label-a": "apple pie and pizza",
"label-b": "pineapple with apple juice"
}
},
{
"_index": "salad",
"_type": "fruit",
"_id": "2",
"_score": 0.1519148,
"_source": {
"label-a": "grape",
"label-b": "apple"
}
},
{
"_index": "salad",
"_type": "fruit",
"_id": "1",
"_score": 0.038978107,
"_source": {
"label-a": "apple apple apple apple apple apple apple apple apple apple apple apple",
"label-b": "raspberry"
}
},
{
"_index": "salad",
"_type": "fruit",
"_id": "3",
"_score": 0.02250402,
"_source": {
"label-a": "apple pie and pizza",
"label-b": "raspberry"
}
}
]

我希望第二个文档(对于 grape具有值 label-a和对于 apple具有值 label-b的文档)在我搜索值苹果时得分最高,并且其中一个标签具有该确切值。无论确切的术语出现在哪个标签上,它都应该起作用。

最佳答案

因为Elasticsearch使用tf / idf模型进行评分,所以您可以获得这些结果。尝试在索引字段中另外指定“label-a”和“label-b”作为未分析(原始)字段。然后像这样重写您的查询:

{
"query": {
"bool": {
"should": {
"match": {
"label-a.raw": {
"query": "apple",
"boost": 2
}
}
},
"must": [
{
"multi_match": {
"query": "apple",
"type": "most_fields",
"fields": [
"label-a",
"label-b"
]
}
}
]
}
}
}

should子句将增强完全匹配的文档,您可能会首先将它们获取。尝试使用增加的数字,并在运行前检查查询。这只是个主意,您可以做什么

关于elasticsearch - Elasticsearch查询在多个字段上更倾向于完全匹配而不是部分匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40768823/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com