I want to query inner hits on one object. When i return the sorted, nested, inner hits, one is missing. When i return the objects ordered in a different direction, the result is there, but another is missing (there are always 3 out of 4 results present).
Why am i not seeing all results? The result section claims there are 4 objects, but only 3 are present.
Here's my index:
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"
}
}
}
}
}
}
}
}
A single object (with 4 nested objects):
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"
}
}
]
}
And the query:
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": {}
}
}
}
]
}
}
}
The response:
{
"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
]
}
]
}
}
}
}
]
}
}
If you'd like to run my setup, here's the docker-compose file:
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
Awesome reproduction. Thanks! You are getting the top 3 hits, which is expected since 3 is the default value for the size
parameter. If you need more just add the size
parameter with higher value.
GET example_index/_search
{
"_source": "inner_hits",
"query": {
"bool": {
"must": [
{
"term": {
"id": 1
}
},
{
"nested": {
"inner_hits": {
"sort": [
{
"conversation_events.occurred_at": {
"order": "desc"
}
}
],
"size": 10
},
"path": "conversation_events",
"query": {
"match_all": {}
}
}
}
]
}
}
}