我有一个有job_title字段的表,
我想进行模糊搜索,让用户在错误拼写查询关键字时仍能获得结果。
例如,我有很多记录将“Android开发者”作为其job_title,
当用户发出错误的搜索Job.es_qsearch("Andoirddd")
时,它应该在NGRAM_ANALYZER
但是,我可以从错误的关键字中获得任何结果。
我只能在输入Job.es_qsearch("Android")
我怎样才能达到预期的搜索结果。
谢谢
NGRAM_ANALYZER = {analysis: {
filter: {
autocomplete_filter: {
type: "edge_ngram",
min_gram: 3,
max_gram: 20
}
},
analyzer: {
ngram_analyzer: {
type: :custom,
tokenizer: :standard,
filter: [
:lowercase,
:autocomplete_filter
]
}
}
}}
settings NGRAM_ANALYZER do
mappings do
indexes :job_title, include_in_all: 'ngram_analyzer'
indexes :description, include_in_all: 'ngram_analyzer'
end
end
def self.es_qsearch(*args, &block)
q_params = {
multi_match: {
query: "*#{args}*",
fields: ['job_title^5', 'description']
}
}
__elasticsearch__.search(
{
query: q_params,
}
)
end
{:index=>"jobs",
:type=>"job",
:body=>{:query=>{:bool=>{:must=>[{:fuzzy=>{:description=>{:value=>["electrical"], :fuzziness=>2}}}]}}, :size=>200}},
"bool": {
"must": [
{
"fuzzy": {
"description": {
# min_similarity: 0.4,
"value": args,
# prefix_length: 0,
"fuzziness": 2
}
},
}]
}
"fuzzy": {
"description": {
# min_similarity: 0.4,
"value": args,
# prefix_length: 0,
"fuzziness": 2
}
},