# Querying Nested Structure

**URL:** https://discuss.elastic.co/t/querying-nested-structure/305960
**Category:** Elasticsearch
**Created:** [May 30, 2022, 1:44pm UTC](https://discuss.elastic.co/t/querying-nested-structure/305960 "2022-05-30T13:44:00Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![obadaqaf](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/obadaqaf/32/106338_2.png) [@obadaqaf](https://discuss.elastic.co/u/obadaqaf)
#### Post date: [May 30, 2022, 1:44pm UTC](https://discuss.elastic.co/t/querying-nested-structure/305960/1 "2022-05-30T13:44:00Z")

</div>

I try to query documents that have nested objects:

```auto
{
      "name":"Adam",
      "cities":[
         {
            "name":"California",
            "activities":[
               {
                  "name":"Hicking"
               },
               {
                  "name":"Camping"
               },
               {
                  "name":"Festival"
               }
            ]
         },
         {
            "name":"Miami",
            "activities":[
               {
                  "name":"Festival"
               },
               {
                  "name":"Diving"
               },
               {
                  "name":"Surfing"
               }
            ]
         }
      ]
    }

```

I want to retrieve all conditions based on an activity, for example, I want to retrieve all cities where Adam wants to go to festivals. The query result should be California and Miami and if we search about what is the town where Adam will do hicking, the query result should be California.  
This is what it SHOULD be, but the actual result is that when I search about the cities where Adam will do hicking, the query result was California and Miami!!

This is the query I used:

```auto
    GET index/_search
    {
      "query": 
            {
             "bool": {"filter": [
                        {"term": {
                             "cities.activities.name.keyword": "Hicking"
                                 }}
                               ]}
                    },"_source":["cities.name"]
          }

```

And this is the search result:

```auto
        "_index" : "index",
        "_type" : "prod",
        "_id" : "1",
        "_score" : 0.0,
        "_source" : {
          "cities" : [
            {
              "name" : "California"
            },
            {
              "name" : "Miami"
            }
          ]
        }

```

How can make the query in the right way for such a data structure?

Will nested field helps?

Should I redesign the data structure?

---

<div class="post-metadata">

### Author: ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)
#### Post date: [May 30, 2022, 3:23pm UTC](https://discuss.elastic.co/t/querying-nested-structure/305960/2 "2022-05-30T15:23:22Z")

</div>

Hi!

One possibility is to use [Nested Query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-nested-query.html), using the cities and/or activities field as "nested".  
To find out which nested object was matched use [inner\_hits](https://www.elastic.co/guide/en/elasticsearch/reference/current/inner-hits.html).

This is a possibility:

```auto
GET idx_test/_search
{
  "_source": [
    "cities.name"
  ],
  "query": {
    "nested": {
      "path": "cities",
      "query": {
        "term": {
          "cities.activities.name.keyword": {
            "value": "Diving"
          }
        }
      },
      "inner_hits": {}
    }
  }
}

```

---

<div class="post-metadata">

### Author: ![obadaqaf](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/obadaqaf/32/106338_2.png) [@obadaqaf](https://discuss.elastic.co/u/obadaqaf)
#### Post date: [May 31, 2022, 1:13pm UTC](https://discuss.elastic.co/t/querying-nested-structure/305960/3 "2022-05-31T13:13:22Z")

</div>

Thanks!! It worked  
But how can I use the fuzzy search with nested queries??

---

<div class="post-metadata">

### Author: ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)
#### Post date: [May 31, 2022, 2:07pm UTC](https://discuss.elastic.co/t/querying-nested-structure/305960/4 "2022-05-31T14:07:40Z")

</div>

Yes. You can try something like this:

```auto
{
          "nested": {
            "path": "field",
            "query": {
              "match": {
                "field_name": {
                  "query": "any text",
                  "fuzziness": "AUTO"
                }
              }
            }
          }
        }

```

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [June 28, 2022, 2:08pm UTC](https://discuss.elastic.co/t/querying-nested-structure/305960/5 "2022-06-28T14:08:23Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
