# Facets and nested objects

**URL:** <https://discuss.elastic.co/t/facets-and-nested-objects/7552>\
**Category:** Elasticsearch\
**Created:** [May 3, 2012, 2:32pm UTC](https://discuss.elastic.co/t/facets-and-nested-objects/7552 "2012-05-03T14:32:47Z")\
**Posts on this page:** 2\
**Page:** 1

<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, 2:32pm UTC](https://discuss.elastic.co/t/facets-and-nested-objects/7552/1 "2012-05-03T14:32:47Z")

</div>

Hi. I have the following type of documents:

{ doc\_title : "doc1",  
classifications: [  
{assigned\_classification : [  
{schema: "sch1"},  
{symbol: "ASD34"} ]  
},  
{assigned\_classification : [  
{schema: "sch1"},  
{symbol: "DWW65"} ]  
},  
{assigned\_classification : [  
{schema: "sch2"},  
{symbol: "5667G"} ]  
} ]  
}

{ doc\_title : "doc2",  
classifications: [  
{assigned\_classification : [  
{schema: "sch1"},  
{symbol: "DFG65"} ]  
},  
{assigned\_classification : [  
{schema: "sch2"},  
{symbol: "34XXX"} ]  
} ]  
}

I would like to build a facet on "symbol" ONLY for those with schema="sch1" (i.e. faceting on symbols from the schema sch1)

I tried the following:

OPTION 1 (failed)

1. create a nested mapping on the field "classifications":

{  
"type1" : {  
"properties" : {  
"classifications" : {  
"type" : "nested"  
}  
}  
}  
}

1. use the following query with facet:

{  
"query": {  
"query\_string": {  
"query": "doc\_title:doc\*"  
}  
},  
"from": 0,  
"size": 10,  
"sort": [],  
"facets": {  
"facet\_concepts": {  
"terms": {  
"field": "classifications.assigned\_classification.symbol",  
"params": {  
"target": "classifications.assigned\_classification.schema",  
"value": "sch1"  
},  
"script": "doc[target].value == value",  
"\_cache": false,  
"size": 10,  
"order": "count"  
}  
}  
}  
}

This didnt work because it gives as results in the facets ALSO symbols from the schema "sch2".

OPTION 2 (failed)

1. create a nested mapping on the field "classifications":

{  
"type1" : {  
"properties" : {  
"classifications" : {  
"type" : "nested"  
}  
}  
}  
}

1. use the following query with facet:

{  
"query": {  
"query\_string": {  
"query": "doc\_title:doc\*"  
}  
},  
"from": 0,  
"size": 10,  
"sort": [],  
"facets": {  
"facet\_concepts": {  
"terms": {  
"field": "classifications.assigned\_classification.symbol",  
"\_cache": false,  
"size": 10,  
"order": "count"  
},  
"facet\_filter": {  
"term": {  
"classifications.assigned\_classification.schema": "sch1"  
}  
}  
}  
}  
}

This also didnt work because it gives as results in the facets ALSO symbols from the schema "sch2".

COULD you indicate why it is failing?

I guess it is because the definition of the nested object and the structure of the documents, but I am not sure.

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 6, 2017, 3:30am UTC](https://discuss.elastic.co/t/facets-and-nested-objects/7552/2 "2017-07-06T03:30:11Z")

</div>

Closed on July 6, 2017, 3:30am UTC


