# Facet filters using nested objects

**URL:** https://discuss.elastic.co/t/facet-filters-using-nested-objects/7818
**Category:** Elasticsearch
**Created:** [May 23, 2012, 8:52am UTC](https://discuss.elastic.co/t/facet-filters-using-nested-objects/7818 "2012-05-23T08:52:16Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![Marcin\_Dojwa](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marcin_dojwa/32/2307_2.png) [@Marcin\_Dojwa](https://discuss.elastic.co/u/Marcin_Dojwa)
#### Post date: [May 23, 2012, 8:52am UTC](https://discuss.elastic.co/t/facet-filters-using-nested-objects/7818/1 "2012-05-23T08:52:16Z")

</div>

Hi,

I have the following problem, please check out

> <https://gist.github.com/anonymous/d3234db747e6e845d1e5>
>
> There are more than three files. show original

1. query\_ok.js - executes OK and this is OK
2. query\_failed.js - this returns empty set because there is exists filter  
in facet\_filter that checks testobj.idtest which is not in nested object  
(testobj.obj1).

So the question is: how to filter documents on any document fields (here:  
testobj.idtest) using facets on field from nested object (here:  
testobj.obj1.date)?

Thanks.  
Best regards  
Marcin.

---

<div class="post-metadata">

### Author: ![Marcin\_Dojwa](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marcin_dojwa/32/2307_2.png) [@Marcin\_Dojwa](https://discuss.elastic.co/u/Marcin_Dojwa)
#### Post date: [May 25, 2012, 2:25pm UTC](https://discuss.elastic.co/t/facet-filters-using-nested-objects/7818/2 "2012-05-25T14:25:54Z")

</div>

Anyone ? 🙂

2012/5/23 Marcin Dojwa [m.dojwa@livechatinc.com](mailto:m.dojwa@livechatinc.com)

> Hi,
> 
> I have the following problem, please check out  
> [Facet filters using nested objects · GitHub](https://gist.github.com/d3234db747e6e845d1e5)
> 
> 1. query\_ok.js - executes OK and this is OK
> 2. query\_failed.js - this returns empty set because there is exists filter  
> in facet\_filter that checks testobj.idtest which is not in nested object  
> (testobj.obj1).
> 
> So the question is: how to filter documents on any document fields (here:  
> testobj.idtest) using facets on field from nested object (here:  
> testobj.obj1.date)?
> 
> Thanks.  
> Best regards  
> Marcin.

---

<div class="post-metadata">

### Author: ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)
#### Post date: [May 25, 2012, 2:31pm UTC](https://discuss.elastic.co/t/facet-filters-using-nested-objects/7818/3 "2012-05-25T14:31:30Z")

</div>

Hi Marcin

I don't see anything obviously wrong, but it is better to gist actual  
curl statements that allow people to copy-and-paste in order to test  
locally, otherwise, speaking for myself, I don't have the time 🙂

clint

> 2012/5/23 Marcin Dojwa [m.dojwa@livechatinc.com](mailto:m.dojwa@livechatinc.com)  
> Hi,
> 
> ```
> I have the following problem, please check
> out https://gist.github.com/d3234db747e6e845d1e5
> 1. query_ok.js - executes OK and this is OK
> 2. query_failed.js - this returns empty set because there is
> exists filter in facet_filter that checks testobj.idtest which
> is not in nested object (testobj.obj1).
>     
>     
> So the question is: how to filter documents on any document
> fields (here: testobj.idtest) using facets on field from
> nested object (here: testobj.obj1.date)?
>     
>     
> Thanks.
> Best regards
> Marcin.
> 
> ```

---

<div class="post-metadata">

### Author: ![benjamin\_leviant](https://avatars.discourse-cdn.com/v4/letter/b/919ad9/32.png) [@benjamin\_leviant](https://discuss.elastic.co/u/benjamin_leviant)
#### Post date: [May 25, 2012, 3:23pm UTC](https://discuss.elastic.co/t/facet-filters-using-nested-objects/7818/4 "2012-05-25T15:23:54Z")

</div>

Hello,

why not this :

{  
"facets": {  
"facet1": {  
"date\_histogram": {  
"field": "date",  
"interval": "hour"  
},  
"facet\_filter": {  
"and": [  
{  
"exists": {  
"field": "testobj.idtest"  
}  
},  
{  
"nested": {  
"path": "testobj.obj1",  
"query": {  
"range": {  
"testobj.obj1.date": {  
"from": "2012-01-01T00:00:00Z",  
"to": "2012-02-01T00:00:00Z"  
}  
}  
}  
}  
}  
]  
}  
}  
}  
}

I hope it can help.

Regards

Benjamin

On Fri, May 25, 2012 at 4:31 PM, Clinton Gormley [clint@traveljury.com](mailto:clint@traveljury.com)wrote:

> Hi Marcin
> 
> I don't see anything obviously wrong, but it is better to gist actual  
> curl statements that allow people to copy-and-paste in order to test  
> locally, otherwise, speaking for myself, I don't have the time 🙂
> 
> clint
> 
> > 2012/5/23 Marcin Dojwa [m.dojwa@livechatinc.com](mailto:m.dojwa@livechatinc.com)  
> > Hi,
> > 
> > ```
> > I have the following problem, please check
> > out https://gist.github.com/d3234db747e6e845d1e5
> > 1. query_ok.js - executes OK and this is OK
> > 2. query_failed.js - this returns empty set because there is
> > exists filter in facet_filter that checks testobj.idtest which
> > is not in nested object (testobj.obj1).
> > 
> > So the question is: how to filter documents on any document
> > fields (here: testobj.idtest) using facets on field from
> > nested object (here: testobj.obj1.date)?
> > 
> > Thanks.
> > Best regards
> > Marcin.
> > 
> > ```

---

<div class="post-metadata">

### Author: ![Marcin\_Dojwa](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marcin_dojwa/32/2307_2.png) [@Marcin\_Dojwa](https://discuss.elastic.co/u/Marcin_Dojwa)
#### Post date: [May 25, 2012, 3:30pm UTC](https://discuss.elastic.co/t/facet-filters-using-nested-objects/7818/5 "2012-05-25T15:30:00Z")

</div>

Hi,

You are right, I added curl\_queries.sh in gist

> <https://gist.github.com/anonymous/d3234db747e6e845d1e5>
>
> There are more than three files. show original

Best regards.

2012/5/25 Clinton Gormley [clint@traveljury.com](mailto:clint@traveljury.com)

> Hi Marcin
> 
> I don't see anything obviously wrong, but it is better to gist actual  
> curl statements that allow people to copy-and-paste in order to test  
> locally, otherwise, speaking for myself, I don't have the time 🙂
> 
> clint
> 
> > 2012/5/23 Marcin Dojwa [m.dojwa@livechatinc.com](mailto:m.dojwa@livechatinc.com)  
> > Hi,
> > 
> > ```
> > I have the following problem, please check
> > out https://gist.github.com/d3234db747e6e845d1e5
> > 1. query_ok.js - executes OK and this is OK
> > 2. query_failed.js - this returns empty set because there is
> > exists filter in facet_filter that checks testobj.idtest which
> > is not in nested object (testobj.obj1).
> > 
> > So the question is: how to filter documents on any document
> > fields (here: testobj.idtest) using facets on field from
> > nested object (here: testobj.obj1.date)?
> > 
> > Thanks.
> > Best regards
> > Marcin.
> > 
> > ```

---

<div class="post-metadata">

### Author: ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)
#### Post date: [May 25, 2012, 4:37pm UTC](https://discuss.elastic.co/t/facet-filters-using-nested-objects/7818/6 "2012-05-25T16:37:50Z")

</div>

> You are right, I added curl\_queries.sh in gist  
> [Facet filters using nested objects · GitHub](https://gist.github.com/d3234db747e6e845d1e5)

Got it.

The problem is that your facet is running an a nested object:

```
     "nested":"testobj.obj1",

```

which means that it can't see field testobj.idtest

To be clear, a nested object is a SEPARATE doc internally. So either,  
you need to use the 'exists' filter as part of the query, or you can  
configure your main object to also store a copy of the nested object  
data:

{  
type: "nested",  
include\_in\_root: true,  
properties: {.... }  
}

Then, instead of running your facet on the nested object, run it on the  
main object (just by leaving out this line: "nested":"testobj.obj1")

Depending on your data, this may or may not suit your purposes

clint

---

<div class="post-metadata">

### Author: ![Marcin\_Dojwa](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marcin_dojwa/32/2307_2.png) [@Marcin\_Dojwa](https://discuss.elastic.co/u/Marcin_Dojwa)
#### Post date: [May 25, 2012, 5:22pm UTC](https://discuss.elastic.co/t/facet-filters-using-nested-objects/7818/7 "2012-05-25T17:22:43Z")

</div>

Hi Benjamin,

Thank you for your help.  
The one you gave does not work because testobj.obj1 is a nested object and  
the date\_histogram works with testobj.obj1.date field. It would work if the  
facet didn't work with field of nested object.  
So, any other ideas ? 🙂 The question is if this is possible in ES 🙂

Best regards.

2012/5/25 benjamin leviant [benjamin.leviant@gmail.com](mailto:benjamin.leviant@gmail.com)

> Hello,
> 
> why not this :
> 
> {  
> "facets": {  
> "facet1": {  
> "date\_histogram": {  
> "field": "date",  
> "interval": "hour"  
> },  
> "facet\_filter": {  
> "and": [  
> {  
> "exists": {  
> "field": "testobj.idtest"  
> }  
> },  
> {  
> "nested": {  
> "path": "testobj.obj1",  
> "query": {  
> "range": {  
> "testobj.obj1.date": {  
> "from": "2012-01-01T00:00:00Z",  
> "to": "2012-02-01T00:00:00Z"  
> }  
> }  
> }  
> }  
> }  
> ]  
> }  
> }  
> }  
> }
> 
> I hope it can help.
> 
> Regards
> 
> Benjamin
> 
> On Fri, May 25, 2012 at 4:31 PM, Clinton Gormley [clint@traveljury.com](mailto:clint@traveljury.com)wrote:
> 
> > Hi Marcin
> > 
> > I don't see anything obviously wrong, but it is better to gist actual  
> > curl statements that allow people to copy-and-paste in order to test  
> > locally, otherwise, speaking for myself, I don't have the time 🙂
> > 
> > clint
> > 
> > > 2012/5/23 Marcin Dojwa [m.dojwa@livechatinc.com](mailto:m.dojwa@livechatinc.com)  
> > > Hi,
> > > 
> > > ```
> > > I have the following problem, please check
> > > out https://gist.github.com/d3234db747e6e845d1e5
> > > 1. query_ok.js - executes OK and this is OK
> > > 2. query_failed.js - this returns empty set because there is
> > > exists filter in facet_filter that checks testobj.idtest which
> > > is not in nested object (testobj.obj1).
> > > 
> > > So the question is: how to filter documents on any document
> > > fields (here: testobj.idtest) using facets on field from
> > > nested object (here: testobj.obj1.date)?
> > > 
> > > Thanks.
> > > Best regards
> > > Marcin.
> > > 
> > > ```

---

<div class="post-metadata">

### Author: ![Marcin\_Dojwa](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marcin_dojwa/32/2307_2.png) [@Marcin\_Dojwa](https://discuss.elastic.co/u/Marcin_Dojwa)
#### Post date: [May 25, 2012, 5:32pm UTC](https://discuss.elastic.co/t/facet-filters-using-nested-objects/7818/8 "2012-05-25T17:32:42Z")

</div>

Hi Clinton,

Thank you,

I was afraid that these were the only one solutions. I did it the first way

- I added exists filter to query but I had to separate two different facets  
and create two different queries instead of one query with two facets  
filtered with facet filters 🙂 But this works as well.  
In my case the copy of testobj.obj1 object will not work because I need to  
count the number of testobj.obj1 elements not the number of root documents  
(am I right this would count the number of documents not the number of  
testobj.obj1 elements?).  
I just wanted to know if it is possible to access root document's fields  
from the nested query. If not I will leave the solution like I said before.

Thank you all for your help.

2012/5/25 Clinton Gormley [clint@traveljury.com](mailto:clint@traveljury.com)

> > You are right, I added curl\_queries.sh in gist  
> > [Facet filters using nested objects · GitHub](https://gist.github.com/d3234db747e6e845d1e5)
> 
> Got it.
> 
> The problem is that your facet is running an a nested object:
> 
> ```
> "nested":"testobj.obj1",
> 
> ```
> 
> which means that it can't see field testobj.idtest
> 
> To be clear, a nested object is a SEPARATE doc internally. So either,  
> you need to use the 'exists' filter as part of the query, or you can  
> configure your main object to also store a copy of the nested object  
> data:
> 
> {  
> type: "nested",  
> include\_in\_root: true,  
> properties: {.... }  
> }
> 
> Then, instead of running your facet on the nested object, run it on the  
> main object (just by leaving out this line: "nested":"testobj.obj1")
> 
> Depending on your data, this may or may not suit your purposes
> 
> clint

---

<div class="post-metadata">

### Author: ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)
#### Post date: [May 25, 2012, 6:31pm UTC](https://discuss.elastic.co/t/facet-filters-using-nested-objects/7818/9 "2012-05-25T18:31:23Z")

</div>

Hiya

> I was afraid that these were the only one solutions. I did it the  
> first way - I added exists filter to query but I had to separate two  
> different facets and create two different queries instead of one query  
> with two facets filtered with facet filters 🙂 But this works as well.  
> In my case the copy of testobj.obj1 object will not work because I  
> need to count the number of testobj.obj1 elements not the number of  
> root documents (am I right this would count the number of documents  
> not the number of testobj.obj1 elements?).  
> I just wanted to know if it is possible to access root document's  
> fields from the nested query. If not I will leave the solution like I  
> said before.

It's not possible. But what about including a copy of that field in  
your nested docs? You would have to do it manually, and it'd take more  
space, but you'd achieve what you're after

clint

> Thank you all for your help.
> 
> 2012/5/25 Clinton Gormley [clint@traveljury.com](mailto:clint@traveljury.com)
> 
> ```
> >
> > You are right, I added curl_queries.sh in gist
> > https://gist.github.com/d3234db747e6e845d1e5
>     
>     
> Got it.
>     
> The problem is that your facet is running an a nested object:
>     
> "nested":"testobj.obj1",
>     
> which means that it can't see field testobj.idtest
>     
> To be clear, a nested object is a SEPARATE doc internally. So
> either,
> you need to use the 'exists' filter as part of the query, or
> you can
> configure your main object to also store a copy of the nested
> object
> data:
>     
> {
> type: "nested",
> include_in_root: true,
> properties: {.... }
> }
>     
> Then, instead of running your facet on the nested object, run
> it on the
> main object (just by leaving out this line:
> "nested":"testobj.obj1")
>     
> Depending on your data, this may or may not suit your purposes
>     
> clint
> 
> ```

---

<div class="post-metadata">

### Author: ![Marcin\_Dojwa](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marcin_dojwa/32/2307_2.png) [@Marcin\_Dojwa](https://discuss.elastic.co/u/Marcin_Dojwa)
#### Post date: [May 25, 2012, 6:44pm UTC](https://discuss.elastic.co/t/facet-filters-using-nested-objects/7818/10 "2012-05-25T18:44:40Z")

</div>

You are right 🙂 this solves the problem 🙂 Thank you very much.

Best regards.

2012/5/25 Clinton Gormley [clint@traveljury.com](mailto:clint@traveljury.com)

> Hiya
> 
> > I was afraid that these were the only one solutions. I did it the  
> > first way - I added exists filter to query but I had to separate two  
> > different facets and create two different queries instead of one query  
> > with two facets filtered with facet filters 🙂 But this works as well.  
> > In my case the copy of testobj.obj1 object will not work because I  
> > need to count the number of testobj.obj1 elements not the number of  
> > root documents (am I right this would count the number of documents  
> > not the number of testobj.obj1 elements?).  
> > I just wanted to know if it is possible to access root document's  
> > fields from the nested query. If not I will leave the solution like I  
> > said before.
> 
> It's not possible. But what about including a copy of that field in  
> your nested docs? You would have to do it manually, and it'd take more  
> space, but you'd achieve what you're after
> 
> clint
> 
> > Thank you all for your help.
> > 
> > 2012/5/25 Clinton Gormley [clint@traveljury.com](mailto:clint@traveljury.com)
> > 
> > ```
> > >
> > > You are right, I added curl_queries.sh in gist
> > > https://gist.github.com/d3234db747e6e845d1e5
> > 
> > Got it.
> > 
> > The problem is that your facet is running an a nested object:
> > 
> > "nested":"testobj.obj1",
> > 
> > which means that it can't see field testobj.idtest
> > 
> > To be clear, a nested object is a SEPARATE doc internally. So
> > either,
> > you need to use the 'exists' filter as part of the query, or
> > you can
> > configure your main object to also store a copy of the nested
> > object
> > data:
> > 
> > {
> > type: "nested",
> > include_in_root: true,
> > properties: {.... }
> > }
> > 
> > Then, instead of running your facet on the nested object, run
> > it on the
> > main object (just by leaving out this line:
> > "nested":"testobj.obj1")
> > 
> > Depending on your data, this may or may not suit your purposes
> > 
> > clint
> > 
> > ```

---

<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:27am UTC](https://discuss.elastic.co/t/facet-filters-using-nested-objects/7818/11 "2017-07-06T03:27:04Z")

</div>


