🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# position_increment_gap(短语位置间隙) 被解析的 **text fields**(文本字段)会将 **term**(词根)的位置考虑进去,目的是为了能支持 **proximity queries**(近似查询)和 **phrase queries**(短语查询)。当我们索引一个含有多个值的 **text fields**(文本字段)时,会在各个值之间加入一个"假想"的间隙,这样就可以阻止大多数跨值匹配的短语查询。这个间隙的大小使用 **position_increment_gap **参数来设定,默认值是100。 例如: | `curl -XPUT ``'localhost:9200/my_index/groups/1?pretty'` `-H ``'Content-Type: application/json'` `-d'` `{` `"names"``: [ ``"John Abraham"``, ``"Lincoln Smith"``]` `}` `'` `curl -XGET ``'localhost:9200/my_index/groups/_search?pretty'` `-H ``'Content-Type: application/json'` `-d'` `{` `"query"``: {` `"match_phrase"``: {` `"names"``: {` `"query"``: ``"Abraham Lincoln"` `#1` `}` `}` `}` `}` `'` `curl -XGET ``'localhost:9200/my_index/groups/_search?pretty'` `-H ``'Content-Type: application/json'` `-d'` `{` `"query"``: {` `"match_phrase"``: {` `"names"``: {` `"query"``: ``"Abraham Lincoln"``,` `"slop"``: 101 ``#2` `}` `}` `}` `}` `'` | | 1 | 这个短语查询跟我们预期的一样,与这个文档不匹配。 | | 2 | 这个短语查询会匹配到文档,因为虽然 **Abraham **和**Lincoln** 在不同的 **string**(字符串),但 **slop**> **position_increment_gap**`。` | **position_increment_gap **可以在映射时设定特定的值。例如: | `curl -XPUT ``'localhost:9200/my_index?pretty'` `-H ``'Content-Type: application/json'` `-d'` `{` `"mappings"``: {` `"groups"``: {` `"properties"``: {` `"names"``: {` `"type"``: ``"text"``,` `"position_increment_gap"``: 0 ``#1` `}` `}` `}` `}` `}` `'` `curl -XPUT ``'localhost:9200/my_index/groups/1?pretty'` `-H ``'Content-Type: application/json'` `-d'` `{` `"names"``: [ ``"John Abraham"``, ``"Lincoln Smith"``]` `}` `'` `curl -XGET ``'localhost:9200/my_index/groups/_search?pretty'` `-H ``'Content-Type: application/json'` `-d'` `{` `"query"``: {` `"match_phrase"``: {` `"names"``: ``"Abraham Lincoln"` `#2` `}` `}` `}` `'` | | 1 | 下一个数组中的第一个词根与前一个数组中的最后一个词根的间隙将会是0。 | | 2 | 这个短语查询会很奇怪的匹配到我们的文档,但这个就是我们映射时设定的目的。 | 注意 同一索引中相同名字的字段可以设定不同的**position_increment_gap**值。他的值可以通过 **[PUT mapping API](https://www.elastic.co/guide/en/elasticsearch/reference/5.3/indices-put-mapping.html "Put Mapping") **使用同名字段来进行更新。