位置增量间隙
position_increment_gap
映射参数定义了多值字段在索引时词元之间的位置距离。这会影响 match_phrase
和 span
查询在搜索同一字段的多个值时的行为方式。
默认情况下,多值字段中的每个新值都被视为与前一个值之间相隔 100
个位置。这有助于防止在搜索可能跨越不同字段值的短语时出现误报。
设置位置增量间隙
使用以下请求创建一个名为 articles
的索引,其中包含一个类型为 text
的 tags
字段,并将 position_increment_gap
设置为 0
。
PUT /articles
{
"mappings": {
"properties": {
"tags": {
"type": "text",
"position_increment_gap": 0
}
}
}
}
索引多值字段
使用以下请求索引一个 tags
字段包含多个值的文档:
PUT /articles/_doc/1
{
"tags": ["machine", "learning"]
}
使用 match_phrase
查询进行搜索
使用以下 match_phrase
查询在 tags
字段中搜索“machine learning”:
GET /articles/_search
{
"query": {
"match_phrase": {
"tags": "machine learning"
}
}
}
结果表明短语匹配成功,因为 position_increment_gap
被设置为 0
,允许来自不同值的词元被视为相邻。
{
...
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 0.5753642,
"hits": [
{
"_index": "articles",
"_id": "1",
"_score": 0.5753642,
"_source": {
"tags": [
"machine",
"learning"
]
}
}
]
}
}
如果 position_increment_gap
保持为 100
,则不会返回任何匹配结果,因为词元 machine
和 learning
将被视为相距 100 个位置。