所以我一直在尝试将nGram匹配添加到我的ElasticSearch索引中,但我遇到了以下问题 .
执行标准 string 查询仅返回完全匹配 . 在特定测试字段上运行 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 Queries
产生结果的查询:
curl -X GET "http://localhost:9200/users/_search?pretty=true" -d '{"query":{"text":{"test":{"query":"piz"}}}}'
产生NO结果的查询:
curl -X GET "http://localhost:9200/users/_search?pretty=true" -d '{"query":{"query_string":{"query":"pizz"}}}'
有没有办法让一般的query_string搜索查看所有索引字段并匹配ngrams,而不是必须在特定列上进行文本/匹配搜索?
1 回答
这是预期的行为 . 默认情况下,“query_string”查询在“_all”字段上执行 . 由于此字段使用StandardAnalyzer进行索引,因此它的索引标记将与“test”字段(您配置为使用nGram分析器)的索引标记不同 .
您可以通过以下几种方式更改此行为:
更改索引设置中的映射,并为"_all"字段配置nGram分析器
发送和"_analyzer"字段作为文档的一部分(它将被拾取并用于没有为其配置显式分析器的所有字段)
使用"fields"属性指定要执行"query_string"的字段
从以上三个选项中,最推荐#3 . 明确指定字段可以更好地控制数据(如何对其进行索引和查询) .