Span 或查询
span_or
查询组合了多个 span 查询,并匹配它们 span 的并集。如果包含的 span 查询中至少有一个匹配,则发生匹配。
例如,您可以使用 span_or
查询来
- 查找匹配多个模式中任意一个的 span。
- 将不同的 span 模式组合作为替代方案。
- 在一个查询中匹配多个 span 变体。
示例
要尝试本节中的示例,请完成设置步骤。
以下查询在彼此相距 2 个词的范围内搜索“formal collar”或“button collar”
GET /clothing/_search
{
"query": {
"span_or": {
"clauses": [
{
"span_near": {
"clauses": [
{
"span_term": {
"description": "formal"
}
},
{
"span_term": {
"description": "collar"
}
}
],
"slop": 0,
"in_order": true
}
},
{
"span_near": {
"clauses": [
{
"span_term": {
"description": "button"
}
},
{
"span_term": {
"description": "collar"
}
}
],
"slop": 2,
"in_order": true
}
}
]
}
}
}
该查询在指定的 slop 距离内匹配文档 1(“...formal collar...”)和 3(“...button-down collar...”)。
响应
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 2.170027,
"hits": [
{
"_index": "clothing",
"_id": "1",
"_score": 2.170027,
"_source": {
"description": "Long-sleeved dress shirt with a formal collar and button cuffs. "
}
},
{
"_index": "clothing",
"_id": "3",
"_score": 1.2509141,
"_source": {
"description": "Short-sleeved shirt with a button-down collar, can be dressed up or down."
}
}
]
}
}
参数
下表列出了 span_or
查询支持的所有顶级参数。
参数 | 数据类型 | 描述 |
---|---|---|
clauses | 数组 | 要匹配的 span 查询数组。如果这些 span 查询中有任何一个匹配,则查询匹配。必须包含至少一个 span 查询。必填。 |