简短答案
您需要检查 映射 并查看是否使用
fast-vector-highlighter。但是,您仍然需要非常小心自己的查询。
详细答案
假设使用ES的新实例
0.20.4上
localhost。
以您的示例为基础,让我们添加显式映射。注意我为该
pre字段设置了两种不同的分析。唯一的区别是
"term_vector":"with_positions_offsets"。
curl -X PUT localhost:9200/myindex -d '{ "settings" : { "index":{ "number_of_replicas":0, "number_of_shards":1, "analysis":{ "analyzer":{ "default":{ "type":"custom", "tokenizer":"keyword", "filter":[ "lowercase", "my_ngram" ] } }, "filter":{ "my_ngram":{ "type":"nGram", "min_gram":1, "max_gram":20 } } } } }, "mappings" : { "product" : { "properties" : { "pre" : { "type" : "multi_field", "fields" : { "pre" : { "type" : "string", "analyzer" : "default", "store" : "yes" }, "pre.ngram" : { "type" : "string", "analyzer" : "default", "store" : "yes", "term_vector":"with_positions_offsets" } } } } } }}'索引一些数据。
curl -X POST 'localhost:9200/myindex/product' -d '{ "pre" : "Samsung Galaxy i7500"}'curl -X POST 'localhost:9200/myindex/product' -d '{ "pre" : "Samsung Galaxy 5 Europa"}'curl -X POST 'localhost:9200/myindex/product' -d '{ "pre" : "Samsung Galaxy Mini"}'现在我们可以运行查询了。
1)搜索“ i”以查看带有突出显示的字符搜索功能
curl -X GET 'localhost:9200/myindex/product/_search?pretty' -d '{ "fields" : [ "pre" ], "query" : { "term" : { "pre" : "i" } }, "highlight" : { "number_of_fragments" : 0, "fields" : { "pre":{}, "pre.ngram":{} } }}'这将产生两个搜索结果:
# 1..."fields" : { "pre" : "Samsung Galaxy Mini"},"highlight" : { "pre.ngram" : [ "Samsung Galaxy M<em>i</em>n<em>i</em>" ], "pre" : [ "Samsung Galaxy M<em>i</em>n<em>i</em>" ]}# 2..."fields" : { "pre" : "Samsung Galaxy i7500"},"highlight" : { "pre.ngram" : [ "Samsung Galaxy <em>i</em>7500" ], "pre" : [ "Samsung Galaxy <em>i</em>7500" ]}这次
pre和
pre.ngem字段均正确突出显示。但是当使用更长的查询时,情况会迅速改变:
2)搜索“ ym”
curl -X GET 'localhost:9200/myindex/product/_search?pretty' -d '{ "fields" : [ "pre" ], "query" : { "term" : { "pre" : "y m" } }, "highlight" : { "number_of_fragments" : 0, "fields" : { "pre":{}, "pre.ngram":{} } }}'这样产生:
"fields" : { "pre" : "Samsung Galaxy Mini"},"highlight" : { "pre.ngram" : [ "Samsung Galax<em>y M</em>ini" ], "pre" : [ "Samsung Galaxy Min<em>y M</em>i" ]}该
pre领域是不正确高亮(类似你的情况)。
重要的一点是, 使用查询 一词代替了 query_string 。



