# How to check if facet is selected

**URL:** <https://discuss.elastic.co/t/how-to-check-if-facet-is-selected/4263>\
**Category:** Elasticsearch\
**Created:** [April 20, 2011, 8:00am UTC](https://discuss.elastic.co/t/how-to-check-if-facet-is-selected/4263 "2011-04-20T08:00:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![paranoiabla](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/paranoiabla/32/2160_2.png) [@paranoiabla](https://discuss.elastic.co/u/paranoiabla)\
**Post date:** [April 20, 2011, 8:00am UTC](https://discuss.elastic.co/t/how-to-check-if-facet-is-selected/4263/1 "2011-04-20T08:00:13Z")

</div>

Hi there,

I am building an application here with some facets that I represent as  
checkboxes. On each checkbox click I send such a parameter: ?  
selected\_facets=priceEUR\_[100.0-200.0] and them my server-side  
controller is building a request to ES.  
The JSON that I get back is something like this:

"facets" : {  
"description" : {  
"Type" : "terms",  
"missing" : 5437,  
"terms" : []  
},  
"name" : {  
"Type" : "terms",  
"missing" : 5437,  
"terms" : []  
},  
"priceEUR" : {  
"Type" : "range",  
"ranges" : [  
{  
"from" : 0.0,  
"to" : 5.0,  
"count" : 595,  
"total" : 1457.8904200000002,  
"mean" : 2.4502360000000003  
},  
{  
"from" : 100.0,  
"to" : 200.0,  
"count" : 2207,  
"total" : 879698.2974919997,  
"mean" : 398.59460692886256  
},  
{  
"from" : 5.0,  
"to" : 100.0,  
"count" : 2096,  
"total" : 67012.99705100003,  
"mean" : 31.97184973807253  
}  
]  
}  
}

and so I pass it to the JSP and iterate to display the checkboxes  
again. My problem comes when I have to decide whether a checkbox is  
checked or not. It would be really nice if priceEUR facet had a  
'selected' attribute, or smth to distinguish the selected value, like  
the one below:

```
"priceEUR" : {
	"Type" : "range",
	"ranges" : [
		{
			"from" : 0.0,
			"to" : 5.0,
			"count" : 595,
			"total" : 1457.8904200000002,
			"mean" : 2.4502360000000003
		},
		{
			"from" : 100.0,
			"to" : 200.0,
			"count" : 2207,
			"total" : 879698.2974919997,
			"mean" : 398.59460692886256,
                            "selected" :

```

true  
\<\<\<\<-------------------------------------|||||  
},  
{  
"from" : 5.0,  
"to" : 100.0,  
"count" : 2096,  
"total" : 67012.99705100003,  
"mean" : 31.97184973807253  
}  
]  
}

I can, of course do some computation in the controller and alter the  
returned JSON, but I thought it would be a nice feature for ES to  
return the selected value.

Thank you for your time.

---

<div class="post-metadata">

**Author:** ![Karussell1](https://avatars.discourse-cdn.com/v4/letter/k/50afbb/32.png) [@Karussell1](https://discuss.elastic.co/u/Karussell1)\
**Post date:** [April 20, 2011, 9:13am UTC](https://discuss.elastic.co/t/how-to-check-if-facet-is-selected/4263/2 "2011-04-20T09:13:56Z")

</div>

'Selected' can mean a lot of different things. What if you want to  
'select' two values? (combination via OR)

I think you have to look into the original query to get information of  
how and which values are filtered.

For jetwick I'm even more lazy and put my custom query object into the  
session so that I can just do filterQueries.contains(xy) ...

Regards,  
Peter.

On Apr 20, 10:00 am, paranoiabla [paranoia...@gmail.com](mailto:paranoia...@gmail.com) wrote:

> Hi there,
> 
> I am building an application here with some facets that I represent as  
> checkboxes. On each checkbox click I send such a parameter: ?  
> selected\_facets=priceEUR\_[100.0-200.0] and them my server-side  
> controller is building a request to ES.  
> The JSON that I get back is something like this:
> 
> "facets" : {  
> "description" : {  
> "Type" : "terms",  
> "missing" : 5437,  
> "terms" :   
> },  
> "name" : {  
> "Type" : "terms",  
> "missing" : 5437,  
> "terms" :   
> },  
> "priceEUR" : {  
> "Type" : "range",  
> "ranges" : [  
> {  
> "from" : 0.0,  
> "to" : 5.0,  
> "count" : 595,  
> "total" : 1457.8904200000002,  
> "mean" : 2.4502360000000003  
> },  
> {  
> "from" : 100.0,  
> "to" : 200.0,  
> "count" : 2207,  
> "total" : 879698.2974919997,  
> "mean" : 398.59460692886256  
> },  
> {  
> "from" : 5.0,  
> "to" : 100.0,  
> "count" : 2096,  
> "total" : 67012.99705100003,  
> "mean" : 31.97184973807253  
> }  
> ]  
> }
> 
> }
> 
> and so I pass it to the JSP and iterate to display the checkboxes  
> again. My problem comes when I have to decide whether a checkbox is  
> checked or not. It would be really nice if priceEUR facet had a  
> 'selected' attribute, or smth to distinguish the selected value, like  
> the one below:
> 
> ```
> "priceEUR" : {
> "Type" : "range",
> "ranges" : [
> {
> "from" : 0.0,
> "to" : 5.0,
> "count" : 595,
> "total" : 1457.8904200000002,
> "mean" : 2.4502360000000003
> },
> {
> "from" : 100.0,
> "to" : 200.0,
> "count" : 2207,
> "total" : 879698.2974919997,
> "mean" : 398.59460692886256,
> "selected" :
> 
> ```
> 
> true  
> \<\<\<\<-------------------------------------|||||  
> },  
> {  
> "from" : 5.0,  
> "to" : 100.0,  
> "count" : 2096,  
> "total" : 67012.99705100003,  
> "mean" : 31.97184973807253  
> }  
> ]  
> }
> 
> I can, of course do some computation in the controller and alter the  
> returned JSON, but I thought it would be a nice feature for ES to  
> return the selected value.
> 
> Thank you for your time.

---

<div class="post-metadata">

**Author:** ![paranoiabla](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/paranoiabla/32/2160_2.png) [@paranoiabla](https://discuss.elastic.co/u/paranoiabla)\
**Post date:** [April 20, 2011, 9:34am UTC](https://discuss.elastic.co/t/how-to-check-if-facet-is-selected/4263/3 "2011-04-20T09:34:56Z")

</div>

Hi Karussell,

of course it doesn't make much sense if you use a mixture of AND/OR  
filters,  
but I still think it would be useful in the case when you use only OR  
or AND queries.

I have the original values and I am currently setting the  
selected:true in the JSON by  
myself and it works great. I still feel it looks ugly though.

Cheers, Petar.

On Apr 20, 11:13 am, Karussell [tableyourt...@googlemail.com](mailto:tableyourt...@googlemail.com) wrote:

> 'Selected' can mean a lot of different things. What if you want to  
> 'select' two values? (combination via OR)
> 
> I think you have to look into the original query to get information of  
> how and which values are filtered.
> 
> For jetwick I'm even more lazy and put my custom query object into the  
> session so that I can just do filterQueries.contains(xy) ...
> 
> Regards,  
> Peter.
> 
> On Apr 20, 10:00 am, paranoiabla [paranoia...@gmail.com](mailto:paranoia...@gmail.com) wrote:
> 
> > Hi there,
> 
> > I am building an application here with some facets that I represent as  
> > checkboxes. On each checkbox click I send such a parameter: ?  
> > selected\_facets=priceEUR\_[100.0-200.0] and them my server-side  
> > controller is building a request to ES.  
> > The JSON that I get back is something like this:
> 
> > "facets" : {  
> > "description" : {  
> > "Type" : "terms",  
> > "missing" : 5437,  
> > "terms" :   
> > },  
> > "name" : {  
> > "Type" : "terms",  
> > "missing" : 5437,  
> > "terms" :   
> > },  
> > "priceEUR" : {  
> > "Type" : "range",  
> > "ranges" : [  
> > {  
> > "from" : 0.0,  
> > "to" : 5.0,  
> > "count" : 595,  
> > "total" : 1457.8904200000002,  
> > "mean" : 2.4502360000000003  
> > },  
> > {  
> > "from" : 100.0,  
> > "to" : 200.0,  
> > "count" : 2207,  
> > "total" : 879698.2974919997,  
> > "mean" : 398.59460692886256  
> > },  
> > {  
> > "from" : 5.0,  
> > "to" : 100.0,  
> > "count" : 2096,  
> > "total" : 67012.99705100003,  
> > "mean" : 31.97184973807253  
> > }  
> > ]  
> > }
> 
> > }
> 
> > and so I pass it to the JSP and iterate to display the checkboxes  
> > again. My problem comes when I have to decide whether a checkbox is  
> > checked or not. It would be really nice if priceEUR facet had a  
> > 'selected' attribute, or smth to distinguish the selected value, like  
> > the one below:
> 
> > ```
> > "priceEUR" : {
> > "Type" : "range",
> > "ranges" : [
> > {
> > "from" : 0.0,
> > "to" : 5.0,
> > "count" : 595,
> > "total" : 1457.8904200000002,
> > "mean" : 2.4502360000000003
> > },
> > {
> > "from" : 100.0,
> > "to" : 200.0,
> > "count" : 2207,
> > "total" : 879698.2974919997,
> > "mean" : 398.59460692886256,
> > "selected" :
> > 
> > ```
> > 
> > true  
> > \<\<\<\<-------------------------------------|||||  
> > },  
> > {  
> > "from" : 5.0,  
> > "to" : 100.0,  
> > "count" : 2096,  
> > "total" : 67012.99705100003,  
> > "mean" : 31.97184973807253  
> > }  
> > ]  
> > }
> 
> > I can, of course do some computation in the controller and alter the  
> > returned JSON, but I thought it would be a nice feature for ES to  
> > return the selected value.
> 
> > Thank you for your time.

---

<div class="post-metadata">

**Author:** ![Karussell1](https://avatars.discourse-cdn.com/v4/letter/k/50afbb/32.png) [@Karussell1](https://discuss.elastic.co/u/Karussell1)\
**Post date:** [April 21, 2011, 11:35am UTC](https://discuss.elastic.co/t/how-to-check-if-facet-is-selected/4263/4 "2011-04-21T11:35:42Z")

</div>

Hi Petar!

> of course it doesn't make much sense if you use a mixture of AND/OR  
> filters,  
> but I still think it would be useful in the case when you use only OR  
> or AND queries.

Yes this would be useful. But I don't think it relates into the core  
of ES. (Also in solr there is no such feature)  
Maybe it should be done in a separete facet helper plugin or  
something.  
Then various multiselect scenarios could be recognized - that would be  
very neat - also for me 😉

> I have the original values and I am currently setting the  
> selected:true in the JSON by  
> myself and it works great. I still feel it looks ugly though.

Why do you need to set it to true?  
Do you mean you have a controller and you want to make it easier for  
the view?  
This does not sound ugly IMHO.

Regards,  
Peter.

---

<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, 4:07am UTC](https://discuss.elastic.co/t/how-to-check-if-facet-is-selected/4263/5 "2017-07-06T04:07:55Z")

</div>


