ElasticSearch嵌套nested字段精确匹配仅含单一值文档的查询咨询
符合需求的ElasticSearch DSL查询构造方案
核心逻辑说明
要实现需求只需同时满足两个过滤条件:
- 文档的
Types嵌套数组中至少存在一个Label等于目标搜索值 - 文档的
Types嵌套数组中不存在任何Label不等于目标搜索值的元素
完整DSL示例(以搜索Label值为Dialog为例)
{ "query": { "bool": { "must": [ { "nested": { "path": "Types", "query": { "term": { "Types.Label": "Dialog" } } } } ], "must_not": [ { "nested": { "path": "Types", "query": { "bool": { "must_not": [ { "term": { "Types.Label": "Dialog" } } ] } } } } ] } } }
字段说明
must块的nested查询:确保返回的文档至少有一个Types嵌套对象的Label匹配目标值,避免漏返回符合要求的文档must_not块的nested查询:过滤掉所有存在其他Label值的文档,保证返回结果的Types数组中仅包含目标搜索值- 如果你索引中
Types.Label是text类型,将term查询替换为match查询即可,或指定对应keyword子字段比如Types.Label.keyword
内容的提问来源于stack exchange,提问作者arjun bollam
相关产品推荐
相关产品推荐

