栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如果将min_gram设置为1,则在ngram过滤器上突出显示Elasticsearch是很奇怪的

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如果将min_gram设置为1,则在ngram过滤器上突出显示Elasticsearch是很奇怪的

简短答案

您需要检查 映射 并查看是否使用

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



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/409469.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号