【发布时间】:2012-08-20 04:55:05
【问题描述】:
所以我一直在尝试将 nGram 匹配添加到我的 ElasticSearch 索引中,但我遇到了以下问题。
执行标准 string 查询只会返回完全匹配。对特定 test 字段运行 match 查询会产生预期的 nGram 匹配。
我根据 these(1) examples(2) 为我的字段设置 nGram 过滤器和分析器。映射代码如下:
tire.settings :number_of_shards => 1,
:number_of_replicas => 1,
:analysis => {
:analyzer => {
"str_search_analyzer" => {
"tokenizer" => "keyword",
"filter" => "lowercase"
},
"str_index_analyzer" => {
"tokenizer" => "keyword",
"filter" => ["lowercase","substring"]
}
},
:filter => {
:substring => {
"type" => "nGram",
"min_gram" => 1,
"max_gram" => 10
}
}
} do
mapping do
indexes :test, :type=>'string',
:search_analyzer => :str_search_analyzer,
:index_analyzer=>:str_index_analyzer
end
end
def to_indexed_json
#adding known word plus random string for testing
{
:test => "pizza" + (0...10).map{ ('a'..'z').to_a[rand(26)] }.join
}.to_json
end
ElasticSearch 查询
产生结果的查询:
curl -X GET "http://localhost:9200/users/_search?pretty=true" -d '{"query":{"text":{"test":{"query":"piz"}}}}'
不产生任何结果的查询:
curl -X GET "http://localhost:9200/users/_search?pretty=true" -d '{"query":{"query_string":{"query":"pizz"}}}'
有没有办法让一般的 query_string 搜索来查看所有索引字段并匹配 ngram,而不必对特定列进行文本/匹配搜索?
【问题讨论】:
标签: ruby-on-rails elasticsearch tire n-gram