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

Elasticsearch嵌套inner hits查询结果缺失问题求助

问题描述

查询单个文档的嵌套inner_hits时,排序后总是缺失一条结果(4条里只返回3条);切换排序方向后,之前缺失的结果会出现,但又会缺失另一条。结果统计显示有4个对象,但实际仅展示3个。

索引定义

PUT example_index/
{
    "mappings": {
        "properties": {
            "id": {
                "type": "long"
            },
            "conversation_events": {
                "type": "nested",
                "properties": {
                    "occurred_at": {
                        "type": "date",
                        "format": "yyyy-MM-dd HH:mm:ss||strict_date_optional_time ||epoch_millis"
                    },
                    "type": {
                        "type": "keyword"
                    },
                    "message": {
                        "properties": {
                            "id": {
                                "type": "keyword"
                            }
                        }
                    },
                    "account_event": {
                        "properties": {
                            "id": {
                                "type": "keyword"
                            }
                        }
                    }
                }
            }
        }
    }
}

测试文档

POST /example_index/_doc/1
{
  "id": 1,
  "conversation_events": [
    {
      "occurred_at": "2023-01-01T12:00:00Z",
      "type": "message",
      "message": {
        "id": "message_1"
      }
    },
    {
      "occurred_at": "2023-01-01T12:00:01Z",
      "type": "account_event",
      "account_event": {
        "id": "account_event_1"
      }
    },
    {
      "occurred_at": "2023-01-01T12:00:02Z",
      "type": "message",
      "message": {
        "id": "message_2"
      }
    },
    {
      "occurred_at": "2023-01-01T12:00:03Z",
      "type": "account_event",
      "account_event": {
        "id": "account_event_2"
      }
    }
  ]
}

查询语句

GET example_index/_search
{
  "_source": "inner_hits", 
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "id": 1
          }
        },
        {
          "nested": {
            "inner_hits": {
              "sort": [
                {
                  "conversation_events.occurred_at": {
                    "order": "desc"
                  }
                }
              ]
            },
            "path": "conversation_events",
            "query": {
              "match_all": {}
            }
          }
        }
      ]
    }
  }
}

响应结果

{
  "took" : 9,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 2.0,
    "hits" : [
      {
        "_index" : "example_index",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 2.0,
        "_source" : { },
        "inner_hits" : {
          "conversation_events" : {
            "hits" : {
              "total" : {
                "value" : 4,
                "relation" : "eq"
              },
              "max_score" : null,
              "hits" : [
                {
                  "_index" : "example_index",
                  "_type" : "_doc",
                  "_id" : "1",
                  "_nested" : {
                    "field" : "conversation_events",
                    "offset" : 3
                  },
                  "_score" : null,
                  "_source" : {
                    "occurred_at" : "2023-01-01T12:00:03Z",
                    "account_event" : {
                      "id" : "account_event_2"
                    },
                    "type" : "account_event"
                  },
                  "sort" : [
                    1672574403000
                  ]
                },
                {
                  "_index" : "example_index",
                  "_type" : "_doc",
                  "_id" : "1",
                  "_nested" : {
                    "field" : "conversation_events",
                    "offset" : 2
                  },
                  "_score" : null,
                  "_source" : {
                    "occurred_at" : "2023-01-01T12:00:02Z",
                    "type" : "message",
                    "message" : {
                      "id" : "message_2"
                    }
                  },
                  "sort" : [
                    1672574402000
                  ]
                },
                {
                  "_index" : "example_index",
                  "_type" : "_doc",
                  "_id" : "1",
                  "_nested" : {
                    "field" : "conversation_events",
                    "offset" : 1
                  },
                  "_score" : null,
                  "_source" : {
                    "occurred_at" : "2023-01-01T12:00:01Z",
                    "account_event" : {
                      "id" : "account_event_1"
                    },
                    "type" : "account_event"
                  },
                  "sort" : [
                    1672574401000
                  ]
                }
              ]
            }
          }
        }
      }
    ]
  }
}

复现环境(Docker Compose)

version: "3"
services:
  elastic:
    image: docker.elastic.co/elasticsearch/elasticsearch-oss:7.10.2
    environment:
      - cluster.name=es-docker-cluster
      - bootstrap.memory_lock=true
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
      - discovery.type=single-node
      - bootstrap.system_call_filter=false
    ulimits:
      memlock:
        soft: -1
        hard: -1
    volumes:
      - data01:/usr/share/elasticsearch/data
    ports:
      - 9200:9200
      - 9300:9300
    networks:
      - elastic

  kibana:
    image: docker.elastic.co/kibana/kibana-oss:7.10.2
    platform: linux/amd64
    container_name: kibana
    environment:
      - ELASTICSEARCH_HOSTS=http://elastic:9200
      - ELASTICSEARCH_URL=http://elastic:9200
    ports:
      - 5601:5601
    networks:
      - elastic
    depends_on:
      - elastic

volumes:
  data01:
    driver: local

networks:
  elastic:
    driver: bridge
原因与解决方案

问题根源在于**inner_hits的默认返回数量限制**:Elasticsearch中,inner_hits默认仅返回3条结果,这就是统计显示有4条但实际只展示3条的原因。

要获取全部嵌套文档,只需在inner_hits配置中显式设置size参数为需要的数量(比如4,或更大的值以覆盖更多场景):

GET example_index/_search
{
  "_source": "inner_hits", 
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "id": 1
          }
        },
        {
          "nested": {
            "inner_hits": {
              "sort": [
                {
                  "conversation_events.occurred_at": {
                    "order": "desc"
                  }
                }
              ],
              "size": 4  // 新增此参数,指定返回全部4条嵌套结果
            },
            "path": "conversation_events",
            "query": {
              "match_all": {}
            }
          }
        }
      ]
    }
  }
}

如果不确定嵌套文档的最大数量,也可以将size设置为一个足够大的值(比如1000),确保所有嵌套结果都能被返回。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.07 18:54:58