您需要检查 并查看是否使用fast-vector-Highlighter
。但是,您仍然需要非常小心自己的查询。
假设使用ES的新实例0.20.4
上localhost
。
以您的示例为基础,让我们添加显式映射。注意我为该code
字段设置了两种不同的分析。唯一的区别是"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" : {
"code" : {
"type" : "multi_field",
"fields" : {
"code" : {
"type" : "string",
"analyzer" : "default",
"store" : "yes"
},
"code.ngram" : {
"type" : "string",
"analyzer" : "default",
"store" : "yes",
"term_vector":"with_positions_offsets"
}
}
}
}
}
}
}'
索引一些数据。
curl -X POST 'localhost:9200/myindex/product' -d '{
"code" : "Samsung Galaxy i7500"
}'
curl -X POST 'localhost:9200/myindex/product' -d '{
"code" : "Samsung Galaxy 5 Europa"
}'
curl -X POST 'localhost:9200/myindex/product' -d '{
"code" : "Samsung Galaxy Mini"
}'
现在我们可以运行查询了。