position_increment_gap

编辑

已分析的文本字段会考虑词项的位置,以便支持邻近或短语查询。当索引具有多个值的文本字段时,会在值之间添加一个“伪”间隔,以防止大多数短语查询跨值匹配。此间隔的大小使用 position_increment_gap 配置,默认为 100

例如

resp = client.index(
    index="my-index-000001",
    id="1",
    document={
        "names": [
            "John Abraham",
            "Lincoln Smith"
        ]
    },
)
print(resp)

resp1 = client.search(
    index="my-index-000001",
    query={
        "match_phrase": {
            "names": {
                "query": "Abraham Lincoln"
            }
        }
    },
)
print(resp1)

resp2 = client.search(
    index="my-index-000001",
    query={
        "match_phrase": {
            "names": {
                "query": "Abraham Lincoln",
                "slop": 101
            }
        }
    },
)
print(resp2)
response = client.index(
  index: 'my-index-000001',
  id: 1,
  body: {
    names: [
      'John Abraham',
      'Lincoln Smith'
    ]
  }
)
puts response

response = client.search(
  index: 'my-index-000001',
  body: {
    query: {
      match_phrase: {
        names: {
          query: 'Abraham Lincoln'
        }
      }
    }
  }
)
puts response

response = client.search(
  index: 'my-index-000001',
  body: {
    query: {
      match_phrase: {
        names: {
          query: 'Abraham Lincoln',
          slop: 101
        }
      }
    }
  }
)
puts response
const response = await client.index({
  index: "my-index-000001",
  id: 1,
  document: {
    names: ["John Abraham", "Lincoln Smith"],
  },
});
console.log(response);

const response1 = await client.search({
  index: "my-index-000001",
  query: {
    match_phrase: {
      names: {
        query: "Abraham Lincoln",
      },
    },
  },
});
console.log(response1);

const response2 = await client.search({
  index: "my-index-000001",
  query: {
    match_phrase: {
      names: {
        query: "Abraham Lincoln",
        slop: 101,
      },
    },
  },
});
console.log(response2);
PUT my-index-000001/_doc/1
{
  "names": [ "John Abraham", "Lincoln Smith"]
}

GET my-index-000001/_search
{
  "query": {
    "match_phrase": {
      "names": {
        "query": "Abraham Lincoln" 
      }
    }
  }
}

GET my-index-000001/_search
{
  "query": {
    "match_phrase": {
      "names": {
        "query": "Abraham Lincoln",
        "slop": 101 
      }
    }
  }
}

这个短语查询不匹配我们的文档,这是完全预期的。

这个短语查询匹配我们的文档,即使 AbrahamLincoln 在不同的字符串中,因为 slop > position_increment_gap

position_increment_gap 可以在映射中指定。例如

resp = client.indices.create(
    index="my-index-000001",
    mappings={
        "properties": {
            "names": {
                "type": "text",
                "position_increment_gap": 0
            }
        }
    },
)
print(resp)

resp1 = client.index(
    index="my-index-000001",
    id="1",
    document={
        "names": [
            "John Abraham",
            "Lincoln Smith"
        ]
    },
)
print(resp1)

resp2 = client.search(
    index="my-index-000001",
    query={
        "match_phrase": {
            "names": "Abraham Lincoln"
        }
    },
)
print(resp2)
response = client.indices.create(
  index: 'my-index-000001',
  body: {
    mappings: {
      properties: {
        names: {
          type: 'text',
          position_increment_gap: 0
        }
      }
    }
  }
)
puts response

response = client.index(
  index: 'my-index-000001',
  id: 1,
  body: {
    names: [
      'John Abraham',
      'Lincoln Smith'
    ]
  }
)
puts response

response = client.search(
  index: 'my-index-000001',
  body: {
    query: {
      match_phrase: {
        names: 'Abraham Lincoln'
      }
    }
  }
)
puts response
const response = await client.indices.create({
  index: "my-index-000001",
  mappings: {
    properties: {
      names: {
        type: "text",
        position_increment_gap: 0,
      },
    },
  },
});
console.log(response);

const response1 = await client.index({
  index: "my-index-000001",
  id: 1,
  document: {
    names: ["John Abraham", "Lincoln Smith"],
  },
});
console.log(response1);

const response2 = await client.search({
  index: "my-index-000001",
  query: {
    match_phrase: {
      names: "Abraham Lincoln",
    },
  },
});
console.log(response2);
PUT my-index-000001
{
  "mappings": {
    "properties": {
      "names": {
        "type": "text",
        "position_increment_gap": 0 
      }
    }
  }
}

PUT my-index-000001/_doc/1
{
  "names": [ "John Abraham", "Lincoln Smith"]
}

GET my-index-000001/_search
{
  "query": {
    "match_phrase": {
      "names": "Abraham Lincoln" 
    }
  }
}

下一个数组元素中的第一个词项与前一个数组元素中的最后一个词项的距离为 0 个词项。

这个短语查询匹配我们的文档,这很奇怪,但这是我们在映射中要求的。