# Facet\_filter for different scopes

**URL:** <https://discuss.elastic.co/t/facet-filter-for-different-scopes/9405>\
**Category:** Elasticsearch\
**Created:** [October 18, 2012, 9:34am UTC](https://discuss.elastic.co/t/facet-filter-for-different-scopes/9405 "2012-10-18T09:34:34Z")\
**Posts on this page:** 8\
**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:** [October 18, 2012, 9:34am UTC](https://discuss.elastic.co/t/facet-filter-for-different-scopes/9405/1 "2012-10-18T09:34:34Z")

</div>

Hey, list.

I'm struggling with multifacet navigation, where facets are located in  
different scopes.

Let's see example:

authors: {  
'name': 'Mark Twain',  
'country': 'US',  
'books': [  
{  
'year': 1927  
},  
{  
'year': 1935  
}  
]  
}

Here `books` is a nested object.

I want to make facets on country and book years.  
My problem is I can not do like this:

```
"facets": {
    "book-years": {
        "scope": "book-year-filter",
        "terms": {
            "field": "books.year"
        },
        "facet_filter": {
                "in": {
                    "country": [
                        "US", "GB"
                    ]
                }
        }
    }
},

```

How could I solve this?

--

---

<div class="post-metadata">

**Author:** ![radu\_gheorghe](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/radu_gheorghe/32/556_2.png) [@radu\_gheorghe](https://discuss.elastic.co/u/radu_gheorghe)\
**Post date:** [October 18, 2012, 12:05pm UTC](https://discuss.elastic.co/t/facet-filter-for-different-scopes/9405/2 "2012-10-18T12:05:57Z")

</div>

Hello Rauan,

This should work:

```
"facets": {
    "book-years": {
        "terms": {
            "field": "year"
        },
        "nested": "books"
    }
}

```

## Best regards, Radu

[http://sematext.com/](http://sematext.com/) -- Elasticsearch -- Solr -- Lucene

On Thu, Oct 18, 2012 at 12:34 PM, Rauan Maemirov [rauan1987@gmail.com](mailto:rauan1987@gmail.com) wrote:

> Hey, list.
> 
> I'm struggling with multifacet navigation, where facets are located in  
> different scopes.
> 
> Let's see example:
> 
> authors: {  
> 'name': 'Mark Twain',  
> 'country': 'US',  
> 'books': [  
> {  
> 'year': 1927  
> },  
> {  
> 'year': 1935  
> }  
> ]  
> }
> 
> Here `books` is a nested object.
> 
> I want to make facets on country and book years.  
> My problem is I can not do like this:
> 
> ```
> "facets": {
> "book-years": {
> "scope": "book-year-filter",
> "terms": {
> "field": "books.year"
> },
> "facet_filter": {
> "in": {
> "country": [
> "US", "GB"
> ]
> }
> }
> }
> },
> 
> ```
> 
> How could I solve this?
> 
> --

--

---

<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:** [October 18, 2012, 4:44pm UTC](https://discuss.elastic.co/t/facet-filter-for-different-scopes/9405/3 "2012-10-18T16:44:58Z")

</div>

That doesn't help.  
I have troubles with facet\_filter, but not with nested scope.

Le jeudi 18 octobre 2012 18:06:06 UTC+6, Radu Gheorghe a écrit :

> Hello Rauan,
> 
> This should work:
> 
> ```
> "facets": { 
> "book-years": { 
> "terms": { 
> "field": "year" 
> }, 
> "nested": "books" 
> } 
> } 
> 
> ```
> 
> ## Best regards, Radu
> 
> [http://sematext.com/](http://sematext.com/) -- Elasticsearch -- Solr -- Lucene
> 
> On Thu, Oct 18, 2012 at 12:34 PM, Rauan Maemirov \<[raua...@gmail.com](mailto:raua...@gmail.com)\<javascript:\>\>  
> wrote:
> 
> > Hey, list.
> > 
> > I'm struggling with multifacet navigation, where facets are located in  
> > different scopes.
> > 
> > Let's see example:
> > 
> > authors: {  
> > 'name': 'Mark Twain',  
> > 'country': 'US',  
> > 'books': [  
> > {  
> > 'year': 1927  
> > },  
> > {  
> > 'year': 1935  
> > }  
> > ]  
> > }
> > 
> > Here `books` is a nested object.
> > 
> > I want to make facets on country and book years.  
> > My problem is I can not do like this:
> > 
> > ```
> > "facets": { 
> > "book-years": { 
> > "scope": "book-year-filter", 
> > "terms": { 
> > "field": "books.year" 
> > }, 
> > "facet_filter": { 
> > "in": { 
> > "country": [ 
> > "US", "GB" 
> > ] 
> > } 
> > } 
> > } 
> > }, 
> > 
> > ```
> > 
> > How could I solve this?
> > 
> > --

--

---

<div class="post-metadata">

**Author:** ![radu\_gheorghe](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/radu_gheorghe/32/556_2.png) [@radu\_gheorghe](https://discuss.elastic.co/u/radu_gheorghe)\
**Post date:** [October 18, 2012, 9:04pm UTC](https://discuss.elastic.co/t/facet-filter-for-different-scopes/9405/4 "2012-10-18T21:04:02Z")

</div>

On Thu, Oct 18, 2012 at 7:44 PM, Rauan Maemirov [rauan1987@gmail.com](mailto:rauan1987@gmail.com) wrote:

> That doesn't help.  
> I have troubles with facet\_filter, but not with nested scope.

Oh, I see. If your facet scope is "books", then your facet\_filter also  
looks in "books". And "country" is outside that scope.

You could use "include\_in\_parent" or "include\_in\_root" in your nested  
mapping. Something like this:

curl -XPUT localhost:9200/test/test\_nested/\_mapping -d '  
{  
"test\_nested": {  
"properties": {  
"books": {  
"type": "nested",  
"include\_in\_parent": true,  
"properties": {  
"year": {  
"type": "long"  
}  
}  
}  
}  
}  
}'

Then, don't specify any scope to your facet (because "year" is now  
included in the parent/root document), and the facet filter should  
work as well, because "country" is already in the root doc. Something  
like this:

```
"facets": {
    "book-years": {
        "terms": {
            "field": "year"
        },
        "facet_filter": {
            "terms": {
                "country": ["us","gb"]
            }
        }
    }
}

```

## Best regards, Radu

[http://sematext.com/](http://sematext.com/) -- Elasticsearch -- Solr -- Lucene

> Le jeudi 18 octobre 2012 18:06:06 UTC+6, Radu Gheorghe a écrit :
> 
> > Hello Rauan,
> > 
> > This should work:
> > 
> > ```
> > "facets": {
> > "book-years": {
> > "terms": {
> > "field": "year"
> > },
> > "nested": "books"
> > }
> > }
> > 
> > ```
> > 
> > ## Best regards, Radu
> > 
> > [http://sematext.com/](http://sematext.com/) -- Elasticsearch -- Solr -- Lucene
> > 
> > On Thu, Oct 18, 2012 at 12:34 PM, Rauan Maemirov [raua...@gmail.com](mailto:raua...@gmail.com)  
> > wrote:
> > 
> > > Hey, list.
> > > 
> > > I'm struggling with multifacet navigation, where facets are located in  
> > > different scopes.
> > > 
> > > Let's see example:
> > > 
> > > authors: {  
> > > 'name': 'Mark Twain',  
> > > 'country': 'US',  
> > > 'books': [  
> > > {  
> > > 'year': 1927  
> > > },  
> > > {  
> > > 'year': 1935  
> > > }  
> > > ]  
> > > }
> > > 
> > > Here `books` is a nested object.
> > > 
> > > I want to make facets on country and book years.  
> > > My problem is I can not do like this:
> > > 
> > > ```
> > > "facets": {
> > > "book-years": {
> > > "scope": "book-year-filter",
> > > "terms": {
> > > "field": "books.year"
> > > },
> > > "facet_filter": {
> > > "in": {
> > > "country": [
> > > "US", "GB"
> > > ]
> > > }
> > > }
> > > }
> > > },
> > > 
> > > ```
> > > 
> > > How could I solve this?
> > > 
> > > --
> 
> --

--

---

<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:** [October 19, 2012, 8:07am UTC](https://discuss.elastic.co/t/facet-filter-for-different-scopes/9405/5 "2012-10-19T08:07:31Z")

</div>

What if I have same property names for parent and nested objects?  
Will parent prorepties be overwritten by nested?

And what is the difference between include\_in\_parent and include\_in\_root?

Le vendredi 19 octobre 2012 03:04:08 UTC+6, Radu Gheorghe a écrit :

> On Thu, Oct 18, 2012 at 7:44 PM, Rauan Maemirov \<[raua...@gmail.com](mailto:raua...@gmail.com)\<javascript:\>\>  
> wrote:
> 
> > That doesn't help.  
> > I have troubles with facet\_filter, but not with nested scope.
> 
> Oh, I see. If your facet scope is "books", then your facet\_filter also  
> looks in "books". And "country" is outside that scope.
> 
> You could use "include\_in\_parent" or "include\_in\_root" in your nested  
> mapping. Something like this:
> 
> curl -XPUT localhost:9200/test/test\_nested/\_mapping -d '  
> {  
> "test\_nested": {  
> "properties": {  
> "books": {  
> "type": "nested",  
> "include\_in\_parent": true,  
> "properties": {  
> "year": {  
> "type": "long"  
> }  
> }  
> }  
> }  
> }  
> }'
> 
> Then, don't specify any scope to your facet (because "year" is now  
> included in the parent/root document), and the facet filter should  
> work as well, because "country" is already in the root doc. Something  
> like this:
> 
> ```
> "facets": { 
> "book-years": { 
> "terms": { 
> "field": "year" 
> }, 
> "facet_filter": { 
> "terms": { 
> "country": ["us","gb"] 
> } 
> } 
> } 
> } 
> 
> ```
> 
> ## Best regards, Radu
> 
> [http://sematext.com/](http://sematext.com/) -- Elasticsearch -- Solr -- Lucene

--

---

<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:** [October 19, 2012, 9:00am UTC](https://discuss.elastic.co/t/facet-filter-for-different-scopes/9405/6 "2012-10-19T09:00:33Z")

</div>

Thank you so much, getting rid of nested queries and using  
include\_in\_parent helped to resolve the issue.

Le vendredi 19 octobre 2012 03:04:08 UTC+6, Radu Gheorghe a écrit :

> On Thu, Oct 18, 2012 at 7:44 PM, Rauan Maemirov \<[raua...@gmail.com](mailto:raua...@gmail.com)\<javascript:\>\>  
> wrote:
> 
> > That doesn't help.  
> > I have troubles with facet\_filter, but not with nested scope.
> 
> Oh, I see. If your facet scope is "books", then your facet\_filter also  
> looks in "books". And "country" is outside that scope.
> 
> You could use "include\_in\_parent" or "include\_in\_root" in your nested  
> mapping. Something like this:
> 
> curl -XPUT localhost:9200/test/test\_nested/\_mapping -d '  
> {  
> "test\_nested": {  
> "properties": {  
> "books": {  
> "type": "nested",  
> "include\_in\_parent": true,  
> "properties": {  
> "year": {  
> "type": "long"  
> }  
> }  
> }  
> }  
> }  
> }'
> 
> Then, don't specify any scope to your facet (because "year" is now  
> included in the parent/root document), and the facet filter should  
> work as well, because "country" is already in the root doc. Something  
> like this:
> 
> ```
> "facets": { 
> "book-years": { 
> "terms": { 
> "field": "year" 
> }, 
> "facet_filter": { 
> "terms": { 
> "country": ["us","gb"] 
> } 
> } 
> } 
> } 
> 
> ```
> 
> ## Best regards, Radu
> 
> [http://sematext.com/](http://sematext.com/) -- Elasticsearch -- Solr -- Lucene
> 
> > Le jeudi 18 octobre 2012 18:06:06 UTC+6, Radu Gheorghe a écrit :
> > 
> > > Hello Rauan,
> > > 
> > > This should work:
> > > 
> > > ```
> > > "facets": { 
> > > "book-years": { 
> > > "terms": { 
> > > "field": "year" 
> > > }, 
> > > "nested": "books" 
> > > } 
> > > } 
> > > 
> > > ```
> > > 
> > > ## Best regards, Radu
> > > 
> > > [http://sematext.com/](http://sematext.com/) -- Elasticsearch -- Solr -- Lucene
> > > 
> > > On Thu, Oct 18, 2012 at 12:34 PM, Rauan Maemirov [raua...@gmail.com](mailto:raua...@gmail.com)  
> > > wrote:
> > > 
> > > > Hey, list.
> > > > 
> > > > I'm struggling with multifacet navigation, where facets are located  
> > > > in  
> > > > different scopes.
> > > > 
> > > > Let's see example:
> > > > 
> > > > authors: {  
> > > > 'name': 'Mark Twain',  
> > > > 'country': 'US',  
> > > > 'books': [  
> > > > {  
> > > > 'year': 1927  
> > > > },  
> > > > {  
> > > > 'year': 1935  
> > > > }  
> > > > ]  
> > > > }
> > > > 
> > > > Here `books` is a nested object.
> > > > 
> > > > I want to make facets on country and book years.  
> > > > My problem is I can not do like this:
> > > > 
> > > > ```
> > > > "facets": { 
> > > > "book-years": { 
> > > > "scope": "book-year-filter", 
> > > > "terms": { 
> > > > "field": "books.year" 
> > > > }, 
> > > > "facet_filter": { 
> > > > "in": { 
> > > > "country": [ 
> > > > "US", "GB" 
> > > > ] 
> > > > } 
> > > > } 
> > > > } 
> > > > }, 
> > > > 
> > > > ```
> > > > 
> > > > How could I solve this?
> > > > 
> > > > --
> > 
> > --

--

---

<div class="post-metadata">

**Author:** ![radu\_gheorghe](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/radu_gheorghe/32/556_2.png) [@radu\_gheorghe](https://discuss.elastic.co/u/radu_gheorghe)\
**Post date:** [October 19, 2012, 9:23am UTC](https://discuss.elastic.co/t/facet-filter-for-different-scopes/9405/7 "2012-10-19T09:23:30Z")

</div>

Hello Rauan,

On Fri, Oct 19, 2012 at 11:07 AM, Rauan Maemirov [rauan1987@gmail.com](mailto:rauan1987@gmail.com) wrote:

> What if I have same property names for parent and nested objects?  
> Will parent prorepties be overwritten by nested?

No, they won't get overwritten, but the facet without "nested" in it  
will only work for the "year" property in the parent doc. Which would  
get you back to where you started ☹

> And what is the difference between include\_in\_parent and include\_in\_root?

If you have multiple levels of nested, include\_in\_parent should add  
your field to the immediate parent, while include\_in\_root should add  
it to the root document. If you only have a single nested layer (like  
in your example), both should mean the same. I haven't tested both  
yet, but that's what I understand from here:

> **[Elastic — The Search AI Company](https://www.elastic.co)**
>
> Power insights and outcomes with The Elastic Search AI Platform. See into your data and find answers that matter with enterprise solutions designed to help you accelerate time to insight. Try Elastic ...

## Best regards, Radu

[http://sematext.com/](http://sematext.com/) -- Elasticsearch -- Solr -- Lucene

> Le vendredi 19 octobre 2012 03:04:08 UTC+6, Radu Gheorghe a écrit :
> 
> > On Thu, Oct 18, 2012 at 7:44 PM, Rauan Maemirov [raua...@gmail.com](mailto:raua...@gmail.com) wrote:
> > 
> > > That doesn't help.  
> > > I have troubles with facet\_filter, but not with nested scope.
> > 
> > Oh, I see. If your facet scope is "books", then your facet\_filter also  
> > looks in "books". And "country" is outside that scope.
> > 
> > You could use "include\_in\_parent" or "include\_in\_root" in your nested  
> > mapping. Something like this:
> > 
> > curl -XPUT localhost:9200/test/test\_nested/\_mapping -d '  
> > {  
> > "test\_nested": {  
> > "properties": {  
> > "books": {  
> > "type": "nested",  
> > "include\_in\_parent": true,  
> > "properties": {  
> > "year": {  
> > "type": "long"  
> > }  
> > }  
> > }  
> > }  
> > }  
> > }'
> > 
> > Then, don't specify any scope to your facet (because "year" is now  
> > included in the parent/root document), and the facet filter should  
> > work as well, because "country" is already in the root doc. Something  
> > like this:
> > 
> > ```
> > "facets": {
> > "book-years": {
> > "terms": {
> > "field": "year"
> > },
> > "facet_filter": {
> > "terms": {
> > "country": ["us","gb"]
> > }
> > }
> > }
> > }
> > 
> > ```
> > 
> > ## Best regards, Radu
> > 
> > [http://sematext.com/](http://sematext.com/) -- Elasticsearch -- Solr -- Lucene
> 
> --

--

---

<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:07am UTC](https://discuss.elastic.co/t/facet-filter-for-different-scopes/9405/8 "2017-07-06T03:07:56Z")

</div>


