位置增量间隔参数(Position Increment Gap)

Position Increment Gap 参数 #

position_increment_gap 参数控制数组中相邻文本值之间的位置间隔。该参数影响短语查询(match_phrase)在跨数组元素时的匹配行为。

相关指南 #

默认值 #

默认值为 100

为什么需要 position_increment_gap #

当一个 text 字段接受数组值时,各元素的文本会被拼接分析。例如:

PUT my-index/_doc/1
{
  "tags": ["quick brown", "fox jumps"]
}

分析后的词条位置:

词条位置
quick0
brown1
fox102 (1 + 100 + 1)
jumps103

默认间隔 100 使得 match_phrase 查询 "brown fox" 不会匹配该文档,因为 brown(位置 1)和 fox(位置 102)之间有 100 个位置的间隔,远大于短语查询允许的 slop。

示例 #

允许跨元素匹配 #

PUT my-index
{
  "mappings": {
    "properties": {
      "tags": {
        "type": "text",
        "position_increment_gap": 0
      }
    }
  }
}

设为 0 后,"brown fox" 的短语查询将跨元素匹配。

自定义间隔 #

PUT my-index
{
  "mappings": {
    "properties": {
      "names": {
        "type": "text",
        "position_increment_gap": 50
      }
    }
  }
}

何时调整 #

场景建议
严格避免跨数组元素匹配保持默认值 100
允许跨元素短语匹配设为 0
需要精细控制跨元素匹配距离设为合适的正整数

注意事项 #

  • 仅对 text 类型字段的数组值有意义
  • 如果字段不存储数组值,该参数无影响
  • match_phrase 查询的 slop 参数配合使用