gpt4 book ai didi

如果 min_gram 设置为 1,则 Elasticsearch 在 ngram 过滤器上突出显示很奇怪

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

所以我有这个索引

{
"settings":{
"index":{
"number_of_replicas":0,
"analysis":{
"analyzer":{
"default":{
"type":"custom",
"tokenizer":"keyword",
"filter":[
"lowercase",
"my_ngram"
]
}
},
"filter":{
"my_ngram":{
"type":"nGram",
"min_gram":2,
"max_gram":20
}
}
}
}
}
}

我正在通过轮胎 gem 执行此搜索

{
"query":{
"query_string":{
"query":"xyz",
"default_operator":"AND"
}
},
"sort":[
{
"count":"desc"
}
],
"filter":{
"term":{
"active":true,
"_type":null
}
},
"highlight":{
"fields":{
"name":{

}
},
"pre_tags":[
"<strong>"
],
"post_tags":[
"</strong>"
]
}
}

我有两个帖子应该匹配名为“xyz post”和“xyz question”当我执行此搜索时,我正确地恢复了突出显示的字段

<strong>xyz</strong> question
<strong>xyz</strong> post

事情是这样的……只要我在索引中将 min_gram 更改为 1 并重新索引。突出显示的字段开始像这样返回

<strong>x</strong><strong>y</strong><strong>z</strong> pos<strong>xyz</strong>t
<strong>x</strong><strong>y</strong><strong>z</strong> questio<strong>xyz</strong>n

我就是不明白为什么。

最佳答案

简答

您需要检查您的映射,看看您是否使用了fast-vector-highlighter。但是您仍然需要非常小心地查询。

详细解答

假设在 localhost 上使用 ES 0.20.4 的新实例。

在您的示例之上,让我们添加显式映射。注意我为 code 字段设置了两种不同的分析。唯一的区别是 "term_vector":"with_positions_offsets"

curl -X PUT localhost:9200/myindex -d '
{
"settings" : {
"index":{
"number_of_replicas":0,
"number_of_shards":1,
"analysis":{
"analyzer":{
"default":{
"type":"custom",
"tokenizer":"keyword",
"filter":[
"lowercase",
"my_ngram"
]
}
},
"filter":{
"my_ngram":{
"type":"nGram",
"min_gram":1,
"max_gram":20
}
}
}
}
},
"mappings" : {
"product" : {
"properties" : {
"code" : {
"type" : "multi_field",
"fields" : {
"code" : {
"type" : "string",
"analyzer" : "default",
"store" : "yes"
},
"code.ngram" : {
"type" : "string",
"analyzer" : "default",
"store" : "yes",
"term_vector":"with_positions_offsets"
}
}
}
}
}
}
}'

索引一些数据。

curl -X POST 'localhost:9200/myindex/product' -d '{
"code" : "Samsung Galaxy i7500"
}'

curl -X POST 'localhost:9200/myindex/product' -d '{
"code" : "Samsung Galaxy 5 Europa"
}'

curl -X POST 'localhost:9200/myindex/product' -d '{
"code" : "Samsung Galaxy Mini"
}'

现在我们可以运行查询了。

1) 搜索 'i' 以查看一个字符搜索与突出显示的效果

curl -X GET 'localhost:9200/myindex/product/_search?pretty' -d '{
"fields" : [ "code" ],
"query" : {
"term" : {
"code" : "i"
}
},
"highlight" : {
"number_of_fragments" : 0,
"fields" : {
"code":{},
"code.ngram":{}
}
}
}'

这会产生两个搜索结果:

# 1
...
"fields" : {
"code" : "Samsung Galaxy Mini"
},
"highlight" : {
"code.ngram" : [ "Samsung Galaxy M<em>i</em>n<em>i</em>" ],
"code" : [ "Samsung Galaxy M<em>i</em>n<em>i</em>" ]
}
# 2
...
"fields" : {
"code" : "Samsung Galaxy i7500"
},
"highlight" : {
"code.ngram" : [ "Samsung Galaxy <em>i</em>7500" ],
"code" : [ "Samsung Galaxy <em>i</em>7500" ]
}

这次 codecode.ngem 字段都正确突出显示了。但是当使用更长的查询时,情况很快就会发生变化:

2) 搜索'y m'

curl -X GET 'localhost:9200/myindex/product/_search?pretty' -d '{
"fields" : [ "code" ],
"query" : {
"term" : {
"code" : "y m"
}
},
"highlight" : {
"number_of_fragments" : 0,
"fields" : {
"code":{},
"code.ngram":{}
}
}
}'

这会产生:

"fields" : {
"code" : "Samsung Galaxy Mini"
},
"highlight" : {
"code.ngram" : [ "Samsung Galax<em>y M</em>ini" ],
"code" : [ "Samsung Galaxy Min<em>y M</em>i" ]
}

code 字段未正确突出显示(类似于您的情况)。

一件重要的事情是使用term query而不是query_string

关于如果 min_gram 设置为 1,则 Elasticsearch 在 ngram 过滤器上突出显示很奇怪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13750330/

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