如果我有两个字符串:
- 能源部,乔
- 能源部,乔纳森
我想实现这样的搜索:
- “Doe”>“Doe,乔”,“Doe,乔纳森”
- “Doe J”>“Doe,乔”,“Doe,乔纳森”
- “Jon Doe”>“Doe,乔纳森”
- “乔纳多”>“多伊,乔纳森”
这是我的代码:
settings analysis: {
filter: {
nameNGram: {
type: "edgeNGram",
min_gram: 1,
max_gram: 20,
}
},
tokenizer: {
non_word: {
type: "pattern",
pattern: "[^\\w]+"
}
},
analyzer: {
name_analyzer: {
type: "custom",
tokenizer: "non_word",
filter: ["lowercase", "nameNGram"]
},
}
} do
mapping do
indexes :name, type: "multi_field", fields: {
analyzed: { type: "string", index: :analyzed, index_analyzer: "name_analyzer" }, # for indexing
unanalyzed: { type: "string", index: :not_analyzed, :include_in_all => false } # for sorting
}
end
end
def self.search(params)
tire.search(:page => params[:page], :per_page => 20) do
query do
string "name.analyzed:" + params[:query], default_operator: "AND"
end
sort do
by "name.unanalyzed", "asc"
end
end
end
不幸的是,这似乎不起作用......标记化看起来很棒,对于“Doe,Jonathan”,我得到类似“d”,“do”,“doe”,“j”,“jo”,“jon”的东西”、“jona”等,但如果我搜索“do AND jo”,我什么也得不到。但是,如果我搜索“jona”,我会返回“Doe, Jonathan”。我究竟做错了什么?