# Facet filter on nested facets

**URL:** <https://discuss.elastic.co/t/facet-filter-on-nested-facets/11131>\
**Category:** Elasticsearch\
**Created:** [March 13, 2013, 9:34am UTC](https://discuss.elastic.co/t/facet-filter-on-nested-facets/11131 "2013-03-13T09:34:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Simon\_2](https://avatars.discourse-cdn.com/v4/letter/s/5f9b8f/32.png) [@Simon\_2](https://discuss.elastic.co/u/Simon_2)\
**Post date:** [March 13, 2013, 9:34am UTC](https://discuss.elastic.co/t/facet-filter-on-nested-facets/11131/1 "2013-03-13T09:34:31Z")

</div>

Hello,

We are using elasticsearch to store vehicles.

Vehicles are defined (among other things) by a list of criteria (ex :  
color, fuel, gearbox and almost 80 others). Only a few of them are actually  
used for search queries, the others are only here for display.

A criteria is identified by a categoryCode (ex : COLOR) ans a valueCode (ex  
: RED).

We map this criteria as a nested type. The mapping looks like this :

{  
"settings" : {  
...  
},  
"mappings":{  
"vehicle":{  
"properties":{  
...  
"criteria": {  
"type":"nested",  
"properties":{  
"groupCode":{  
"type":"string" , "index" : "not\_analyzed"  
},  
"categoryCode":{  
"type":"string" , "index" : "not\_analyzed"  
},  
"label":{  
"type":"string"  
},  
"valueCode":{  
"type":"string", "index" : "not\_analyzed"  
},  
}  
}  
}  
}  
}  
}

We can already search vehicles through several criteria and getting facets  
from it :

{  
"from": 0,  
"size": 20,  
"facets": {  
"COLOR": {  
"terms": {  
"field": "criteria.valueCode",  
"size": 20  
},  
"facet\_filter": {  
"bool": {  
"must": {  
"terms": {  
"criteria.categoryCode": [  
"COLOR"  
]  
}  
}  
}  
},  
"nested": "criteria",  
}  
}  
}

With this request we can get with the facet COLOR, the different value  
codes and update options in our search engine :  
COLOR: {

- \_type: terms
- missing: 0
- total: 179
- other: 0
- terms: [
  - {
    - term: RED
    - count: 92  
}

  - {
    - term: BLUE
    - count: 82  
}

  - {
    - term: GREEN
    - count: 2  
}

  - {
    - term: YELLOW
    - count: 2  
}

  - {
    - term: BLACK
    - count: 1  
}  
]

}

What we would like to achieve is that if we select the red color for  
example to be able to retrieve other colors as long as an other criteria  
(such as fuel) forbids us to do that.

We can already achieve that with non nested fields with facet filters by  
changing the scope of the facet and excluding the current field from the  
facet filter but with nested fields, it just returns empty facets.

Anyone has an idea on how we can get this result with nested fields ?

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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:** [March 13, 2013, 1:12pm UTC](https://discuss.elastic.co/t/facet-filter-on-nested-facets/11131/2 "2013-03-13T13:12:30Z")

</div>

> }
> 
> What we would like to achieve is that if we select the red color for  
> example to be able to retrieve other colors as long as an other  
> criteria (such as fuel) forbids us to do that.
> 
> We can already achieve that with non nested fields with facet filters  
> by changing the scope of the facet and excluding the current field  
> from the facet filter but with nested fields, it just returns empty  
> facets.

Yes, scopes on nested docs were buggy, and in fact filter scopes have  
been removed in v0.90+

> <https://github.com/elastic/elasticsearch/issues/2606>
>
> Remove support for the \`scope\` field in facets and \`\_scope\` field in the nested …and parent/child queries. The scope support for nested queries will be replaced by the \`nested\` facet option and a facet filter with a nested filter. The nested filters will now support the a \`join\` option. Which controls whether to perform the block join. By default this enabled, but when disabled it returns the nested documents as hits instead of the joined root document.
> 
> Search request with the current scope support.
> 
> \`\`\`
> curl -s -XPOST 'localhost:9200/products/\_search' -d '{
> "query" : {
> "nested" : {
> "path" : "offers",
> "query" : {
> "match" : {
> "offers.color" : "blue"     
> }
> },
> "\_scope" : "my\_scope"
> }
> },
> "facets" : {
> "size" : {
> "terms" : {
> "field" : "offers.size"
> },
> "scope" : "my\_scope"
> }
> }
> }'
> \`\`\`
> 
> The following will be functional equivalent of using the scope support: 
> 
> \`\`\`
> curl -s -XPOST 'localhost:9200/products/\_search?search\_type=count' -d '{
> "query" : {
> "nested" : {
> "path" : "offers",
> "query" : {
> "match" : {
> "offers.color" : "blue"     
> }
> }
> }
> },
> "facets" : {
> "size" : {
> "terms" : {
> "field" : "offers.size"
> },
> "facet\_filter" : {
> "nested" : {
> "path" : "offers",
> "query" : {
> "match" : {
> "offers.color" : "blue"     
> }
> },
> "join" : false
> }   
> },
> "nested" : "offers"
> }
> }
> }'
> \`\`\`
> 
> The scope support for parent/child queries will be replaced by running the child query as filter in a global facet.
> 
> Search request with the current scope support:
> 
> \`\`\`
> curl -s -XPOST 'localhost:9200/products/\_search' -d '{
> "query" : {
> "has\_child" : {
> "type" : "offer",
> "query" : {
> "match" : {
> "color" : "blue"        
> }
> },
> "\_scope" : "my\_scope"
> }
> },
> "facets" : {
> "size" : {
> "terms" : {
> "field" : "size"
> },
> "scope" : "my\_scope"
> }
> }
> }'
> \`\`\`
> 
> The following is the functional equivalent of using the scope support with parent/child queries:
> 
> \`\`\`
> curl -s -XPOST 'localhost:9200/products/\_search' -d '{
> "query" : {
> "has\_child" : {
> "type" : "offer",
> "query" : {
> "match" : {
> "color" : "blue"        
> }
> }
> }
> },
> "facets" : {
> "size" : {
> "terms" : {
> "field" : "size"
> },
> "global" : true,
> "facet\_filter" : {
> "term" : {
> "color" : "blue"    
> }
> }
> }
> }
> }'
> \`\`\`

Instead, include the necessary nested filters within the facet\_filter  
itself

clint

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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, 2:46am UTC](https://discuss.elastic.co/t/facet-filter-on-nested-facets/11131/3 "2017-07-06T02:46:43Z")

</div>


