# Multi level nested docs query filters and facets

**URL:** <https://discuss.elastic.co/t/multi-level-nested-docs-query-filters-and-facets/8164>\
**Category:** Elasticsearch\
**Created:** [June 20, 2012, 7:56am UTC](https://discuss.elastic.co/t/multi-level-nested-docs-query-filters-and-facets/8164 "2012-06-20T07:56:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rauan\_Maemirov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rauan_maemirov/32/826_2.png) [@Rauan\_Maemirov](https://discuss.elastic.co/u/Rauan_Maemirov)\
**Post date:** [June 20, 2012, 7:56am UTC](https://discuss.elastic.co/t/multi-level-nested-docs-query-filters-and-facets/8164/1 "2012-06-20T07:56:49Z")

</div>

Hey, all. I'm having problems with multi level nested docs. Earlier I've  
tried to use two neighbor nesteds in one doc, but the concept was wrong.  
Here I've got another schema, much simplified for better understanding. I'm  
asking for help to make the proof of concept for multi level nested docs  
(that were mentioned in docs, but I've found no examples at all for this  
kind of use case).

Let's assume well-known schema mapping with authors, their books and stores  
where the books could be bought:

```
"authors": {
    "properties": {
        "name": {"type": "string"},
        "books": {
            "type": "nested", "index": "not_analyzed",
            "properties": {
                "title": {"type": "string"},
                "published_year": {"type": "integer", "index": 

```

"not\_analyzed"},  
"options": {"type": "string", "index": "not\_analyzed"},  
"ordered": {"type": "integer", "index": "not\_analyzed"},  
"stores": {  
"type": "nested", "index": "not\_analyzed",  
"properties": {  
"store": {"type": "integer", "index":  
"not\_analyzed"},  
"price": {"type": "float", "index":  
"not\_analyzed"},  
"stock": {"type": "integer", "index":  
"not\_analyzed"}  
}  
}  
}  
}  
}  
}

Here I have some options, which I would like to facet on by ordered number,  
e.g: how many books of fiction genre have been ordered, etc.

Some example docs:

{  
"name": "John Steinbeck",  
"books": [  
{  
"title": "The Grapes of Wrath", "published\_year": 1939,  
"options": ["genre#fiction", "country#american", "pulitzer#yes"],  
"ordered": 4,  
"stores": [  
{"store": 76, "price": 15.5, "stock": 16},  
{"store": 18, "price": 14.3, "stock": 54}  
]  
}  
]  
}

{  
"name": "Philip Roth",  
"books": [  
{  
"title": "American Pastoral", "published\_year": 1997,  
"options": ["genre#fiction", "country#american"],  
"ordered": 2,  
"stores": [  
{"store": 26, "price": 23.4, "stock": 65},  
{"store": 73, "price": 20.3, "stock": 45}  
]  
}  
]  
}

Okay, no let's try to search for books published after 1900

curl -s '[http://localhost:9200/nestedtest/authors/\_search?pretty=true](http://localhost:9200/nestedtest/authors/_search?pretty=true)' -d '{  
"filter": {  
"nested": {  
"\_scope": "latest",  
"path": "books",  
"filter": {  
"bool": {  
"must": [  
{"range": {"books.published\_year": {"gt": 1990}}}  
]  
}  
}  
}  
},  
"facets": {"options": {  
"terms\_stats": {  
"key\_field": "books.options",  
"value\_field": "books.ordered"  
},  
"scope": "latest"  
}  
}}' && echo

(apparently you have to indicate path to field in facets if the field names  
are not unique. e.g: you have another 'ordered' field in a deeper level)

Here's full test script: [https://gist.github.com/2958662](https://gist.github.com/2958662)

Now how can I filter the results by available stock and price, i.e. filter  
out books with specified price range and stock availability for all books  
published after 1900? Let's say: all the books available in stock cheaper  
that 20 bucks.  
Where should I put second nested filter? (with respect to perfomance and  
caching)  
I tried to write requests with filtered query, but was totally lost in  
exceptions, it was returned that nested is not supported within filtered.

---

<div class="post-metadata">

**Author:** ![Rauan\_Maemirov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rauan_maemirov/32/826_2.png) [@Rauan\_Maemirov](https://discuss.elastic.co/u/Rauan_Maemirov)\
**Post date:** [June 20, 2012, 9:46am UTC](https://discuss.elastic.co/t/multi-level-nested-docs-query-filters-and-facets/8164/2 "2012-06-20T09:46:31Z")

</div>

I've solved it, please check the updated  
gist. [Multi level nested docs · GitHub](https://gist.github.com/2958662)

curl -s '[http://localhost:9200/nestedtest/authors/\_search?pretty=true](http://localhost:9200/nestedtest/authors/_search?pretty=true)' -d '{  
"filter": {  
"nested": {  
"\_scope": "latest",  
"path": "books",  
"query": {  
"filtered": {  
"query": {  
"bool": {  
"must": [  
{"range": {"books.published\_year": {"gt": 1900}}}  
]  
}  
},  
"filter": {  
"nested": {  
"\_scope": "available\_in\_stock",  
"path": "books.stores",  
"query": {  
"bool": {  
"must": [  
{"range": {"books.stores.price": {"lt": 20.0}}}  
]  
}  
}  
}  
}

```
            }
        }
    }
},
"facets": {"options": {
	"terms_stats": {
		"key_field": "options",
		"value_field": "ordered"
	},
	"scope": "latest"
}

```

}}' && echo

I wonder, if this is the correct way to filter nesteds? I'm a little bit  
suspicious, if it's ok to use query instead of filter, since it's claimed  
to be slower.

Le mercredi 20 juin 2012 13:56:49 UTC+6, Rauan Maemirov a écrit :

>

---

<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, 3:23am UTC](https://discuss.elastic.co/t/multi-level-nested-docs-query-filters-and-facets/8164/3 "2017-07-06T03:23:16Z")

</div>


