弹性搜索的拼写检查 Ngram 不适用于 rails
Spell check Ngram for elastic Search not working with rails
我在我的模型中使用了拼写检查,这样如果用户输入 "Rentaal" 这样的数据,那么它应该获取正确的数据 "Rental"
document.rb代码
require 'elasticsearch/model'
class Document < ApplicationRecord
include Elasticsearch::Model
include Elasticsearch::Model::Callbacks
belongs_to :user
Document.import force: true
def self.search(query)
__elasticsearch__.search({
query: {
multi_match: {
query: query,
fields: ['name^10', 'service']
}
}
})
end
settings index: {
"number_of_shards": 1,
analysis: {
analyzer: {
edge_ngram_analyzer: { type: "custom", tokenizer: "standard", filter:
["lowercase", "edge_ngram_filter", "stop", "kstem" ] },
}
},
filter: {
edge_ngram_filter: { type: "edgeNGram", min_gram: "3", max_gram:
"20" }
}
} do
mapping do
indexes :name, type: "string", analyzer: "edge_ngram_analyzer"
indexes :service, type: "string", analyzer: "edge_ngram_analyzer"
end
end
end
搜索控制器代码:
def search
if params[:query].nil?
@documents = []
else
@documents = Document.search params[:query]
end
end
但是,如果我输入 Rentaal 或任何拼写错误的单词,它不会显示任何内容。
在我的控制台中
@documents.results.to_a
给出一个空数组。
我在这里做错了什么?如果需要更多数据,请告诉我。
尝试在您的 multi_match
查询中添加 fuzziness
:
{
"query": {
"multi_match": {
"query": "Rentaal",
"fields": ["name^10", "service"],
"fuzziness": "AUTO"
}
}
}
说明
Kstem 过滤器用于将单词缩减为词根形式,它不会像您在这里预期的那样工作 - 它会正确处理像 Renta
或 Rent
这样的短语,但不会处理您提供的拼写错误.
您可以检查 词干提取 如何与以下查询一起使用:
curl -X POST \
'http://localhost:9200/my_index/_analyze?pretty=true' \
-d '{
"analyzer" : "edge_ngram_analyzer",
"text" : ["rentaal"]
}'
结果我看到:
{
"tokens": [
{
"token": "ren"
},
{
"token": "rent"
},
{
"token": "renta"
},
{
"token": "rentaa"
},
{
"token": "rentaal"
}
]
}
所以典型的拼写错误将通过应用模糊处理得到更好的处理。
我在我的模型中使用了拼写检查,这样如果用户输入 "Rentaal" 这样的数据,那么它应该获取正确的数据 "Rental"
document.rb代码
require 'elasticsearch/model'
class Document < ApplicationRecord
include Elasticsearch::Model
include Elasticsearch::Model::Callbacks
belongs_to :user
Document.import force: true
def self.search(query)
__elasticsearch__.search({
query: {
multi_match: {
query: query,
fields: ['name^10', 'service']
}
}
})
end
settings index: {
"number_of_shards": 1,
analysis: {
analyzer: {
edge_ngram_analyzer: { type: "custom", tokenizer: "standard", filter:
["lowercase", "edge_ngram_filter", "stop", "kstem" ] },
}
},
filter: {
edge_ngram_filter: { type: "edgeNGram", min_gram: "3", max_gram:
"20" }
}
} do
mapping do
indexes :name, type: "string", analyzer: "edge_ngram_analyzer"
indexes :service, type: "string", analyzer: "edge_ngram_analyzer"
end
end
end
搜索控制器代码:
def search
if params[:query].nil?
@documents = []
else
@documents = Document.search params[:query]
end
end
但是,如果我输入 Rentaal 或任何拼写错误的单词,它不会显示任何内容。 在我的控制台中
@documents.results.to_a
给出一个空数组。
我在这里做错了什么?如果需要更多数据,请告诉我。
尝试在您的 multi_match
查询中添加 fuzziness
:
{
"query": {
"multi_match": {
"query": "Rentaal",
"fields": ["name^10", "service"],
"fuzziness": "AUTO"
}
}
}
说明
Kstem 过滤器用于将单词缩减为词根形式,它不会像您在这里预期的那样工作 - 它会正确处理像 Renta
或 Rent
这样的短语,但不会处理您提供的拼写错误.
您可以检查 词干提取 如何与以下查询一起使用:
curl -X POST \
'http://localhost:9200/my_index/_analyze?pretty=true' \
-d '{
"analyzer" : "edge_ngram_analyzer",
"text" : ["rentaal"]
}'
结果我看到:
{
"tokens": [
{
"token": "ren"
},
{
"token": "rent"
},
{
"token": "renta"
},
{
"token": "rentaa"
},
{
"token": "rentaal"
}
]
}
所以典型的拼写错误将通过应用模糊处理得到更好的处理。