You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ElasticSearch:bool查询下嵌套查询的inner_hits是否受支持?

在Bool Must的多个嵌套查询中获取各自的Inner Hits

嘿,这个场景完全受支持!你现在只能拿到一个分支的inner_hits,大概率是因为没给每个嵌套查询的inner_hits设置唯一名称,导致后面的结果覆盖了前面的。

Elasticsearch允许在bool must的多个嵌套查询里分别返回各自匹配的嵌套文档,核心要点是给每个inner_hits配置唯一的name属性,避免键名冲突。

举个修正后的示例(对应你给出的Python/JSON结构):

{
    'size': 10,
    'query': {
        'bool': {
            'must': [
                {
                    'nested': {
                        'path': 'attributes',
                        'score_mode': 'avg',
                        'inner_hits': {'name': 'n1_attribute_matches'},  # 唯一名称区分第一个查询的结果
                        'query': {
                            'bool': {
                                'must': [{'match': {'attributes.ename': 'n1'}}]
                            }
                        }
                    }
                },
                {
                    'nested': {
                        'path': 'attributes',
                        'score_mode': 'avg',
                        'inner_hits': {'name': 'other_attribute_matches'},  # 第二个唯一名称
                        'query': {
                            'bool': {
                                'must': [{'match': {'attributes.ename': 'n2'}}]
                            }
                        }
                    }
                }
            ]
        }
    }
}

为什么要设置name?

如果不指定name,所有inner_hits默认都会使用inner_hits作为键名。当你有多个嵌套查询时,后面的结果会覆盖前面的,最终只能看到最后一个嵌套查询的inner_hits。给每个inner_hits分配唯一名称后,返回结果里的每个命中文档下,inner_hits字段会包含你设置的所有名称对应的匹配结果集,这样就能分别获取两个分支的嵌套匹配内容了。

内容的提问来源于stack exchange,提问作者JamEnergy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 03:51:26