Span first 查询
此 span_first
查询匹配从字段开头开始并在指定位置数内结束的 span。当您想查找出现在文档开头附近的词语或短语时,此查询很有用。
例如,您可以使用 span_first
查询执行以下搜索:
- 查找字段前几个词中包含特定词语的文档。
- 确保某些短语出现在文本的开头或附近。
- 仅当模式出现在距离开头指定距离内时才匹配。
示例
要尝试本节中的示例,请完成设置步骤。
以下查询在描述的前 4 个位置内搜索词干“dress”:
GET /clothing/_search
{
"query": {
"span_first": {
"match": {
"span_term": {
"description.stemmed": "dress"
}
},
"end": 4
}
}
}
该查询匹配文档 1 和 2。
- 文档 1 和 2 在第三个位置包含单词
dress
(“Long-sleeved dress…” 和 “Beautiful long dress”)。单词的索引从 0 开始,因此单词“dress”位于位置 2。 - 单词
dress
的位置必须小于4
,这由end
参数指定。
响应
{
"took": 13,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 0.110377684,
"hits": [
{
"_index": "clothing",
"_id": "1",
"_score": 0.110377684,
"_source": {
"description": "Long-sleeved dress shirt with a formal collar and button cuffs. "
}
},
{
"_index": "clothing",
"_id": "2",
"_score": 0.110377684,
"_source": {
"description": "Beautiful long dress in red silk, perfect for formal events."
}
}
]
}
}
match
参数可以包含任何类型的 span 查询,从而允许在字段开头匹配更复杂的模式。
参数
下表列出了 span_first
查询支持的所有顶级参数。所有参数都是必需的。
参数 | 数据类型 | 描述 |
---|---|---|
match | 对象 | 要匹配的 span 查询。这定义了您在字段开头搜索的模式。 |
end | 整数 | 允许的 span 查询匹配的最大结束位置(不包括)。例如,end: 4 匹配位置 0-3 的词条。 |