# A facet on specific embedded docs

**URL:** https://discuss.elastic.co/t/a-facet-on-specific-embedded-docs/6595
**Category:** Elasticsearch
**Created:** [February 5, 2012, 12:57am UTC](https://discuss.elastic.co/t/a-facet-on-specific-embedded-docs/6595 "2012-02-05T00:57:49Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Nick\_Hoffman](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nick_hoffman/32/1872_2.png) [@Nick\_Hoffman](https://discuss.elastic.co/u/Nick_Hoffman)
#### Post date: [February 5, 2012, 12:57am UTC](https://discuss.elastic.co/t/a-facet-on-specific-embedded-docs/6595/1 "2012-02-05T00:57:49Z")

</div>

Hi guys. With documents like this:

{ title: "grimlock", tags: [ {name: "character", value: "grimlock"}, {name:  
"series", value: "original"} ] }

{ title: "megatron", tags: [ {name: "character", value: "megatron"}, {name:  
"series", value: "original"} ] }

How can I build a facet on "tags.value" where "tags.name" is "character"?  
For example, I'd like to get "grimlock" and "megatron", but not "original".

The best I've been able to do so far is below. However, it returns  
"original" and "megatron", instead of just "megatron".

curl 'localhost:9200/test/items/\_search?pretty=1' -d '  
{  
size: 0,

facets: {  
tags: {  
terms: {  
field: "tags.value",  
params: { target: "title", value: "megatron" },  
script: "doc[target].value == value"  
}  
}  
}  
}'

Outputs:

"facets" : {  
"tags" : {  
...snip...  
"terms" : [  
{ "term" : "original", "count" : 1 },  
{ "term" : "megatron", "count" : 1 }  
]

Thanks for your help, everyone.  
Nick

---

<div class="post-metadata">

### Author: ![Jan\_Fiedler](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jan_fiedler/32/2518_2.png) [@Jan\_Fiedler](https://discuss.elastic.co/u/Jan_Fiedler)
#### Post date: [February 5, 2012, 10:24am UTC](https://discuss.elastic.co/t/a-facet-on-specific-embedded-docs/6595/2 "2012-02-05T10:24:45Z")

</div>

I believe that you need to look at 'nested objects' (aka parent / child)  
for your 'tags'. I have not used it myself yet so I am not 100% sure it  
will provide all the faceting capabilities you need (but I think they  
should).

---

<div class="post-metadata">

### Author: ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)
#### Post date: [February 5, 2012, 5:13pm UTC](https://discuss.elastic.co/t/a-facet-on-specific-embedded-docs/6595/3 "2012-02-05T17:13:32Z")

</div>

Yea, nested is the way to go, so each element in the array will be treated as its own "document". Parent/Child is a different feature :).

On Sunday, February 5, 2012 at 12:24 PM, Jan Fiedler wrote:

> I believe that you need to look at 'nested objects' (aka parent / child) for your 'tags'. I have not used it myself yet so I am not 100% sure it will provide all the faceting capabilities you need (but I think they should).

---

<div class="post-metadata">

### Author: ![Nick\_Hoffman](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nick_hoffman/32/1872_2.png) [@Nick\_Hoffman](https://discuss.elastic.co/u/Nick_Hoffman)
#### Post date: [February 5, 2012, 5:56pm UTC](https://discuss.elastic.co/t/a-facet-on-specific-embedded-docs/6595/4 "2012-02-05T17:56:46Z")

</div>

Thanks guys. Nested documents makes perfect sense.

---

<div class="post-metadata">

### Author: ![posejavier](https://avatars.discourse-cdn.com/v4/letter/p/22d042/32.png) [@posejavier](https://discuss.elastic.co/u/posejavier)
#### Post date: [May 3, 2012, 9:53am UTC](https://discuss.elastic.co/t/a-facet-on-specific-embedded-docs/6595/5 "2012-05-03T09:53:06Z")

</div>

Hi Nick,  
I have the same problem but still didnt find the solution.  
What I did is to index the field as type "nested" and afterwards I applied the script to the facets (basically the same script as you posted).  
The problem is that I still do not receive the filtered facets (I get all the facets like if the field would be not nested)  
How did you solve the problem?

---

<div class="post-metadata">

### Author: ![Nick\_Hoffman](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nick_hoffman/32/1872_2.png) [@Nick\_Hoffman](https://discuss.elastic.co/u/Nick_Hoffman)
#### Post date: [May 3, 2012, 1:21pm UTC](https://discuss.elastic.co/t/a-facet-on-specific-embedded-docs/6595/6 "2012-05-03T13:21:05Z")

</div>

On Thursday, 3 May 2012 05:53:06 UTC-4, jp50045 wrote:

> Hi Nick,  
> I have the same problem but still didnt find the solution.  
> What I did is to index the field as type "nested" and afterwards I applied  
> the script to the facets (basically the same script as you posted).  
> The problem is that I still do not receive the filtered facets (I get all  
> the facets like if the field would be not nested)  
> How did you solve the problem?

Actually, I haven't solved it yet. We delayed the feature that caused me to  
look into this.

When you find a solution, please do post it here!

Cheers,  
Nick

---

<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:30am UTC](https://discuss.elastic.co/t/a-facet-on-specific-embedded-docs/6595/7 "2017-07-06T03:30:12Z")

</div>


