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
            }
        }
    }
}
'

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
        }
    }
}
'

注意

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

Last updated