gpt4 book ai didi

elasticsearch - Elasticsearch multi_match cross_fields 前缀

转载 作者:行者123 更新时间:2023-11-29 02:46:08 24 4
gpt4 key购买 nike

我有一个类型为 cross_fieldsmulti_match 查询,我想通过前缀匹配对其进行改进。

{
"index": "companies",
"size": 25,
"from": 0,
"body": {
"_source": {
"include": [
"name",
"address"
]
},
"query": {
"filtered": {
"query": {
"multi_match": {
"type": "cross_fields",
"query": "Google",
"operator": "and",
"fields": [
"name",
"address"
]
}
}
}
}
}
}

它完美匹配 google mountain view 等查询。 filtered 数组在那里是因为我动态地需要添加地理过滤器。

{
"id": 1,
"name": "Google",
"address": "Mountain View"
}

现在我想在不破坏 cross_fields 的情况下允许前缀匹配。

诸如此类的查询应该匹配:

  • goog
  • 谷歌挂载
  • google mountain vi
  • 山景咕

如果我将 multi_match.type 更改为 phrase_prefix,它将针对单个字段匹配整个查询,因此它仅匹配 mountain vi 但不反对 google mountain vi

我该如何解决?

最佳答案

由于没有答案并且有人可能会看到这个,我遇到了同样的问题,这里是一个解决方案:

使用 the edgeNGrams tokenizer .

您需要更改索引设置和映射。

以下是设置示例:

"settings" : {
"index" : {
"analysis" : {
"analyzer" : {
"ngram_analyzer" : {
"type" : "custom",
"stopwords" : "_none_",
"filter" : [ "standard", "lowercase", "asciifolding", "word_delimiter", "no_stop", "ngram_filter" ],
"tokenizer" : "standard"
},
"default" : {
"type" : "custom",
"stopwords" : "_none_",
"filter" : [ "standard", "lowercase", "asciifolding", "word_delimiter", "no_stop" ],
"tokenizer" : "standard"
}
},
"filter" : {
"no_stop" : {
"type" : "stop",
"stopwords" : "_none_"
},
"ngram_filter" : {
"type" : "edgeNGram",
"min_gram" : "2",
"max_gram" : "20"
}
}
}
}
}

当然,您应该根据自己的用例调整分析器。您可能希望保持默认分析器不变或向其添加 ngram 过滤器,这样您就不必更改映射。最后一个解决方案意味着索引中的所有字段都将获得 ngram 过滤器。

对于映射:

"mappings" : {
"patient" : {
"properties" : {
"name" : {
"type" : "string",
"analyzer" : "ngram_analyzer"
},
"address" : {
"type" : "string",
"analyzer" : "ngram_analyzer"
}
}
}
}

使用 ngram_analyzer 声明您想要自动完成的每个字段。然后你的问题中的查询应该有效。如果您使用其他东西,我很乐意听到。

关于elasticsearch - Elasticsearch multi_match cross_fields 前缀,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28652375/

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