# Issues with nested queries

**URL:** https://discuss.elastic.co/t/issues-with-nested-queries/16937
**Category:** Elasticsearch
**Created:** [April 10, 2014, 8:16pm UTC](https://discuss.elastic.co/t/issues-with-nested-queries/16937 "2014-04-10T20:16:35Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Vishal\_Bhasin](https://avatars.discourse-cdn.com/v4/letter/v/bbce88/32.png) [@Vishal\_Bhasin](https://discuss.elastic.co/u/Vishal_Bhasin)
#### Post date: [April 10, 2014, 8:16pm UTC](https://discuss.elastic.co/t/issues-with-nested-queries/16937/1 "2014-04-10T20:16:35Z")

</div>

Hello All,

The JSON document schema we are using involves a mix of multiple nesting  
levels (arrays/map within arrays/map) to depict travel itineraries. These  
documents are all stored in ES (Elasticsearch) within a single index. We  
are running into issues around trying to walk through nesting levels  
(finding the right path) and retrieving the correct fields.

Mentioned below is an example that creates an index, mapping, inserts  
document and then runs a query. This is quite similar in structure to the  
larger travel itinerary document we are using. But we did not quite  
understand what ES expects for some of the queries.

Here are some of the questions -

1. 

```
  How is “path” in nested query determined when the document has 

```

nested and non-nested types ?

1. 

```
  Can non-nested query (for non-nested fields) be used when the 

```

document mapping has nested types ? Can you give us some examples ?

1. 

```
  We would like to query for an “inner nested” field but also like 

```

to know attribute of some of the outer levels. Eg. The json document has an  
array whose elements are also a nested array. We need to know the index of  
the top level array while querying the inner nested array.

Test.sh (contents)

#!/bin/sh

#echo "\n --- delete index"

curl -X DELETE '[http://localhost:9200/myindex](http://localhost:9200/myindex)'

echo "\n --- create index and put mapping into place"

curl -X POST '[http://localhost:9200/myindex/](http://localhost:9200/myindex/)' -d '{

```
"mappings" : {

    "root" : {

      "type" : "nested",

      "include_in_parent" : "false",

       "properties" : {

         "person" : {

            "type" : "nested",

            "include_in_parent" : "false",

           "properties" : {

            "name" : {"type" : "string"},

            "works" : {

                "type" : "nested",

                "include_in_parent" : false,

                "properties" : {

                    "title" : {"type" : "string"},

                    "current" : {"type" : "boolean"},

                    "dummy" : {"type" : "string"},

                                            "home": {

                                              "type": "nested",

                      "include_in_parent" : false,

                      "properties" : {

                           "address" : {"type" : "string"},

                           "city" : {"type" : "string"}

                                              }

                                            }

                }

            }

        }

    }

  }

},

"settings" : {

    "number_of_shards" : 1,

    "number_of_replicas" : 0

}

```

}

}'

echo "\n --- index data"

curl -X PUT '[http://localhost:9200/myindex/person/1](http://localhost:9200/myindex/person/1)' -d '

{

"root" : [

```
{

"person" : {

"name" : "Lukas1",

"works" : [

    {

        "title" : "developer",

        "current" : true,

        "dummy" : "match",

        "home" : [ {

             "address" : "123 Main st",

             "city" : "dallas"

           },

           {

             "address" : "123 Main st",

             "city" : "irving"

           }

        ]

    },

    {

        "title" : "dad",

        "current" : true,

        "home" : [ {

           "address" : "123 Main st",

           "city" : "grapevine"

            }

        ]

    }

]

}},

```

{"person" :

{

```
"name" : "Lukas2",

"works" : [

    {

        "title" : "developer",

        "current" : true,

        "dummy" : "match",

        "home" : [ {

             "address" : "123 Main st",

             "city" : "dallas2"

           },

           {

             "address" : "123 Main st",

             "city" : "irving2"

           }

        ]

    },

    {

        "title" : "dad",

        "current" : true,

        "home" : [ {

           "address" : "123 Main st",

           "city" : "grapevine2"

            }

        ]

    }

]

}}

```

]

}'

#echo "\n --- optimize"

#curl -X POST '[http://localhost:9200/myindex/\_optimize](http://localhost:9200/myindex/_optimize)'

echo "\n --- query"

curl -X GET '[http://localhost:9200/myindex/\_search?pretty=true](http://localhost:9200/myindex/_search?pretty=true)' -d '

{

```
"query" : {

    "nested" : {

        "path" : "root.person.works.home",

        "query" : {

            "bool" : {

                "must" : [

                    { "term" : { "root.person.works.home.city" : 

```

"irving2"} }

```
                ]

            }

        }

    }

},

"fields" : [

    "root.person.name"

]

```

}'

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/53fc3c34-fc9a-4173-a172-2987ea386bad%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/53fc3c34-fc9a-4173-a172-2987ea386bad%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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: [July 6, 2017, 1:36am UTC](https://discuss.elastic.co/t/issues-with-nested-queries/16937/2 "2017-07-06T01:36:46Z")

</div>


