position_increment_gap(短语位置间隙)

被解析的 text fields(文本字段)会将 term(词根)的位置考虑进去,目的是为了能支持 proximity queries(近似查询)和 phrase queries(短语查询)。当我们索引一个含有多个值的 text fields(文本字段)时,会在各个值之间加入一个”假想”的间隙,这样就可以阻止大多数跨值匹配的短语查询。这个间隙的大小使用 position_increment_gap 参数来设定,默认值是100。

例如:

  1. curl -XPUT 'localhost:9200/my_index/groups/1?pretty' -H 'Content-Type: application/json' -d'
  2. {
  3. "names": [ "John Abraham", "Lincoln Smith"]
  4. }
  5. '
  6. curl -XGET 'localhost:9200/my_index/groups/_search?pretty' -H 'Content-Type: application/json' -d'
  7. {
  8. "query": {
  9. "match_phrase": {
  10. "names": {
  11. "query": "Abraham Lincoln" #1
  12. }
  13. }
  14. }
  15. }
  16. '
  17. curl -XGET 'localhost:9200/my_index/groups/_search?pretty' -H 'Content-Type: application/json' -d'
  18. {
  19. "query": {
  20. "match_phrase": {
  21. "names": {
  22. "query": "Abraham Lincoln",
  23. "slop": 101 #2
  24. }
  25. }
  26. }
  27. }
  28. '

| 1 | 这个短语查询跟我们预期的一样,与这个文档不匹配。 | | 2 | 这个短语查询会匹配到文档,因为虽然 Abraham Lincoln 在不同的 string(字符串),但 slop > position_increment_gap |

position_increment_gap 可以在映射时设定特定的值。例如:

  1. curl -XPUT 'localhost:9200/my_index?pretty' -H 'Content-Type: application/json' -d'
  2. {
  3. "mappings": {
  4. "groups": {
  5. "properties": {
  6. "names": {
  7. "type": "text",
  8. "position_increment_gap": 0 #1
  9. }
  10. }
  11. }
  12. }
  13. }
  14. '
  15. curl -XPUT 'localhost:9200/my_index/groups/1?pretty' -H 'Content-Type: application/json' -d'
  16. {
  17. "names": [ "John Abraham", "Lincoln Smith"]
  18. }
  19. '
  20. curl -XGET 'localhost:9200/my_index/groups/_search?pretty' -H 'Content-Type: application/json' -d'
  21. {
  22. "query": {
  23. "match_phrase": {
  24. "names": "Abraham Lincoln" #2
  25. }
  26. }
  27. }
  28. '

| 1 | 下一个数组中的第一个词根与前一个数组中的最后一个词根的间隙将会是0。 | | 2 | 这个短语查询会很奇怪的匹配到我们的文档,但这个就是我们映射时设定的目的。 |

注意

同一索引中相同名字的字段可以设定不同的 position_increment_gap 值。他的值可以通过 PUT mapping API 使用同名字段来进行更新。

原文链接 : https://www.elastic.co/guide/en/elasticsearch/reference/5.3/position-increment-gap.html(修改该链接为官网对应的链接)

译文链接 : http://www.apache.wiki/pages/viewpage.action?pageId=9406794(修改该链接为 ApacheCN 对应的译文链接)

贡献者 : 郭峰ApacheCNApache中文网