# Search nested fields

**URL:** <https://discuss.elastic.co/t/search-nested-fields/379672>\
**Category:** Elasticsearch\
**Created:** [July 1, 2025, 2:09pm UTC](https://discuss.elastic.co/t/search-nested-fields/379672 "2025-07-01T14:09:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sa.moskalenko](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sa.moskalenko/32/143938_2.png) [@sa.moskalenko](https://discuss.elastic.co/u/sa.moskalenko)\
**Post date:** [July 1, 2025, 2:09pm UTC](https://discuss.elastic.co/t/search-nested-fields/379672/1 "2025-07-01T14:09:04Z")

</div>

I create index with nested field

````auto
{
    "chat": {
        "aliases": {},
        "mappings": {
            "properties": {
                "dataset_id": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "id": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "messages": {
                    "properties": {
                        "chat_id": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "content": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "content_id": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "created_at": {
                            "type": "date"
                        },
                        "dialog_id": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "id": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "index": {
                            "type": "long"
                        },
                        "role": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "row_number": {
                            "type": "long"
                        },
                        "trainable": {
                            "type": "boolean"
                        }
                    }
                }
            }
        },
        "settings": {
            "index": {
                "routing": {
                    "allocation": {
                        "include": {
                            "_tier_preference": "data_content"
                        }
                    }
                },
                "number_of_shards": "1",
                "provided_name": "chat",
                "creation_date": "1751372790348",
                "number_of_replicas": "1",
                "uuid": "67iZTI7IT8ehAqr7k7ZetA",
                "version": {
                    "created": "9009000"
                }
            }
        }
    }
}```

and trying to search messages from user

````

{  
"query": {  
"nested": {  
"path": "messages",  
"query": {  
"bool": {  
"must": [  
{  
"match": {  
"messages.role.keyword": "user"  
}  
}  
]  
}  
}  
}  
}  
}```

but I've got error

```auto
{
    "error": {
        "root_cause": [
            {
                "type": "query_shard_exception",
                "reason": "failed to create query: [nested] failed to find nested object under path [messages]",
                "index_uuid": "67iZTI7IT8ehAqr7k7ZetA",
                "index": "chat"
            }
        ],
        "type": "search_phase_execution_exception",
        "reason": "all shards failed",
        "phase": "query",
        "grouped": true,
        "failed_shards": [
            {
                "shard": 0,
                "index": "chat",
                "node": "lwXfQmtGQlu5e8aDxK1xYw",
                "reason": {
                    "type": "query_shard_exception",
                    "reason": "failed to create query: [nested] failed to find nested object under path [messages]",
                    "index_uuid": "67iZTI7IT8ehAqr7k7ZetA",
                    "index": "chat",
                    "caused_by": {
                        "type": "illegal_state_exception",
                        "reason": "[nested] failed to find nested object under path [messages]"
                    }
                }
            }
        ]
    },
    "status": 400
}

```

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [July 1, 2025, 2:20pm UTC](https://discuss.elastic.co/t/search-nested-fields/379672/2 "2025-07-01T14:20:06Z")

</div>

There is a difference between nested fields and [nested mapping type](https://www.elastic.co/docs/reference/elasticsearch/mapping-reference/nested). The nested query type assumes use of the nested mapping type, which you do not use in the mappings you showed us.

---

<div class="post-metadata">

**Author:** ![sa.moskalenko](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sa.moskalenko/32/143938_2.png) [@sa.moskalenko](https://discuss.elastic.co/u/sa.moskalenko)\
**Post date:** [July 1, 2025, 2:29pm UTC](https://discuss.elastic.co/t/search-nested-fields/379672/3 "2025-07-01T14:29:12Z")

</div>

> [@Christian\_Dahlqvist](#):
>
> the mappings you showed us.

this is my schema

````auto
{
  "mappings": {
    "dynamic_templates": [
      {
        "full_name": {
          "match_mapping_type": "string",
          "path_match": "messages.*",
          "mapping": {
            "type": "text",
            "copy_to": "content"
          }
        }
      }
    ],
    "properties": {
      "id": {
        "type": "keyword"
      },
      "dataset_id": {
        "type": "keyword"
      },
      "messages": {
        "type": "nested"
      },
      "content": {
        "type": "text"
      },
      "row_number": {
        "type": "integer"
      }
    }
  }
}```
````

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [July 1, 2025, 2:32pm UTC](https://discuss.elastic.co/t/search-nested-fields/379672/4 "2025-07-01T14:32:50Z")

</div>

Have a look at the docs I linked to. You do not have any nested mappings so the nested query is not appropriate.
