# Nested query never returns a result \[solved\]

**URL:** <https://discuss.elastic.co/t/nested-query-never-returns-a-result-solved/29231>\
**Category:** Elasticsearch\
**Created:** [September 14, 2015, 9:49am UTC](https://discuss.elastic.co/t/nested-query-never-returns-a-result-solved/29231 "2015-09-14T09:49:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![perplexa](https://avatars.discourse-cdn.com/v4/letter/p/59ef9b/32.png) [@perplexa](https://discuss.elastic.co/u/perplexa)\
**Post date:** [September 14, 2015, 9:49am UTC](https://discuss.elastic.co/t/nested-query-never-returns-a-result-solved/29231/1 "2015-09-14T09:49:31Z")

</div>

Hello guys,

I am having trouble querying nested documents from ElasticSearch. Below you can find my schema and some example queries incl. data.

```
#!/bin/bash
curl -XPUT 'http://localhost:9200/search/'
curl -XPUT "http://localhost:9200/search/models/_mapping" -d '{
 "models": {
   "_timestamp": {"enabled": true, "store": true},
   "properties": {
     "categories": {"type": "string"},
     "categoryTree": {"type": "string"},
     "products": {
       "type": "nested",
       "properties": {
         "id": {"type": "string"},
         "title": {"type": "string"},
         "description": {"type": "string"},
         "color": {"type": "string"},
         "variants": {
           "type": "nested",
           "properties": {
              "id": {"type": "string"},
              "size": {"type": "string"},
              "sellers": {
                "type": "nested",
                "properties": {
                  "id": {"type": "string"},
                  "deepLinkUrl": {"type": "string"},
                  "stock": {"type": "long"},
                  "price": {
                    "type": "nested",
                    "properties": {
                      "isoCode": {"type": "string"},
                      "original": {"type": "double"},
                      "current": {"type": "double"},
                      "discountPercentage": {"type": "long"}
                    }
                  },
                  "shippingMethods": {
                    "type": "nested",
                     "properties": {
                       "id": {"type": "string"},
                       "carrier": {"type": "string"},
                       "deliveryMethod": {"type": "string"},
                       "deliveryCostsSingleItem": {
                         "type": "nested",
                         "properties": {
                           "isoCode": {"type": "string"},
                           "original": {"type": "double"},
                           "current": {"type": "double"},
                           "discountPercentage": {"type": "long"}
                         }
                       },
                       "deliveryCostsMultiItem": {
                         "type": "nested",
                         "properties": {
                           "isoCode": {"type": "string"},
                           "original": {"type": "double"},
                           "current": {"type": "double"},
                           "discountPercentage": {"type": "long"}
                         }
                       },
                       "deliveryTimeMin": {"type": "long"},
                       "deliveryTimeMax": {"type": "long"},
                       "handlingTimeMin": {"type": "long"},
                       "handlingTimeMax": {"type": "long"}
                   }
                 }
               }
             }
           }
         },
         "images": {
           "type": "nested",
           "properties": {
             "type": {"type": "string"},
             "urls": {
               "type": "nested",
               "properties": {
                 "small": {"type": "string"},
                 "medium": {"type": "string"},
                 "large": {"type": "string"}
               }
             }
           }
         },
         "attributes": {
           "type": "nested",
           "properties": {
             "id": {"type": "string"},
             "value": {"type": "string"},
             "title": {"type": "string"}
           }
         }
       }
     }
   }
 }
}'
```

I try and query the data with the following queries:

```
#!/bin/bash

# this is working - notice default_field = _all
curl -XPOST "http://localhost:9200/search/models/_search" -d '{
  "query": {
    "bool": {
      "must": [
        {
          "query_string": {
            "default_field": "_all",
            "query": "Blau"
          }
        }
      ]
    }
  }
}'

# this is not working, trying to limit the search to default_field = models.products.color
# it seems this happens with ALL nested documents, as i can easily filter by e.g. models.categories
curl -XPOST "http://localhost:9200/search/models/_search" -d '{
  "query": {
    "bool": {
      "must": [
        {
          "query_string": {
            "default_field": "models.products.color",
            "query": "Blau"
          }
        }
      ]
    }
  }
}'

# doesn't work either, despite using nested paths.
curl -XPOST "http://localhost:9200/search/models/_search" -d '{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "models.products",
            "query": {
              "bool": {
                "must": [
                  { "match": { "models.products.color": "Blau" }}
                ]
        }}}}
      ]
}}}'
```

I really have no clue what I'm doing wrong here. Any help would be highly appreciated.

Example data can be found here: [http://pastebin.com/WG3xj2qz](http://pastebin.com/WG3xj2qz)

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [September 14, 2015, 10:09am UTC](https://discuss.elastic.co/t/nested-query-never-returns-a-result-solved/29231/2 "2015-09-14T10:09:35Z")

</div>

Hey,

can you try changing the `path` and the `match` fields and remove the type prefix. So it becomes `"path": "products"` and `"match" : { "products.color" : "Blau" }`

Worked for me in a quick test, maybe my recreation was wrong.

--Alex

---

<div class="post-metadata">

**Author:** ![perplexa](https://avatars.discourse-cdn.com/v4/letter/p/59ef9b/32.png) [@perplexa](https://discuss.elastic.co/u/perplexa)\
**Post date:** [September 14, 2015, 10:20am UTC](https://discuss.elastic.co/t/nested-query-never-returns-a-result-solved/29231/3 "2015-09-14T10:20:16Z")

</div>

Hi,

that did the trick and everything is working as intended!

You rock! 🙂

Many, many thanks!

---

<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 5, 2017, 11:50pm UTC](https://discuss.elastic.co/t/nested-query-never-returns-a-result-solved/29231/4 "2017-07-05T23:50:31Z")

</div>


