# Unable to retrieve all the facet info when using Java API

**URL:** <https://discuss.elastic.co/t/unable-to-retrieve-all-the-facet-info-when-using-java-api/13814>\
**Category:** Elasticsearch\
**Created:** [October 1, 2013, 8:05am UTC](https://discuss.elastic.co/t/unable-to-retrieve-all-the-facet-info-when-using-java-api/13814 "2013-10-01T08:05:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Amit\_Soni](https://avatars.discourse-cdn.com/v4/letter/a/c0e974/32.png) [@Amit\_Soni](https://discuss.elastic.co/u/Amit_Soni)\
**Post date:** [October 1, 2013, 8:05am UTC](https://discuss.elastic.co/t/unable-to-retrieve-all-the-facet-info-when-using-java-api/13814/1 "2013-10-01T08:05:47Z")

</div>

Hi everyone - I am using Java API client to execute my queries and the  
request has facets too. After the query has been executed, I receive an  
instance of org.elasticsearch.action.search.SeachResponse.

However I am unable to retrieve all the factet information from this  
SearchResponse instance. I tried SearchResponse # getFacets() #  
getFacets()and this returns me a Map. The key of this map is the facet  
name which is  
right and using this I retrieve an instance of  
org.elasticsearch.search.facet.Facet and this is where I am stuck. This  
interface (Facet) has just two methods (Facet # getName() and Facet #  
getType()) and these two methods are not enough to retrieve all the facet  
info like the count etc.

Do I need to determine the type of each facet and then type cast it to  
specific Facet so that I have more methods available? I am writing some  
generic code and hence trying not to do type casting.

Thoughts?

Thanks much!

-Amit.

--  
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:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [October 1, 2013, 8:12am UTC](https://discuss.elastic.co/t/unable-to-retrieve-all-the-facet-info-when-using-java-api/13814/2 "2013-10-01T08:12:07Z")

</div>

Just cast your Facet with the right class.  
See: [https://github.com/dadoonet/hands-on/blob/answers/src/test/java/org/elasticsearchfr/handson/ex3/FacetTest.java#L157](https://github.com/dadoonet/hands-on/blob/answers/src/test/java/org/elasticsearchfr/handson/ex3/FacetTest.java#L157)

--  
David Pilato | Technical Advocate | [Elasticsearch.com](http://Elasticsearch.com)  
@dadoonet | @elasticsearchfr | @scrutmydocs

Le 1 oct. 2013 à 10:05, Amit Soni [amitsoni29@gmail.com](mailto:amitsoni29@gmail.com) a écrit :

> Hi everyone - I am using Java API client to execute my queries and the request has facets too. After the query has been executed, I receive an instance of org.elasticsearch.action.search.SeachResponse.
> 
> However I am unable to retrieve all the factet information from this SearchResponse instance. I tried SearchResponse # getFacets() # getFacets() and this returns me a Map. The key of this map is the facet name which is right and using this I retrieve an instance of org.elasticsearch.search.facet.Facet and this is where I am stuck. This interface (Facet) has just two methods (Facet # getName() and Facet # getType()) and these two methods are not enough to retrieve all the facet info like the count etc.
> 
> Do I need to determine the type of each facet and then type cast it to specific Facet so that I have more methods available? I am writing some generic code and hence trying not to do type casting.
> 
> Thoughts?
> 
> Thanks much!
> 
> -Amit.
> 
> --  
> 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).

--  
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:** ![Amit\_Soni](https://avatars.discourse-cdn.com/v4/letter/a/c0e974/32.png) [@Amit\_Soni](https://discuss.elastic.co/u/Amit_Soni)\
**Post date:** [October 1, 2013, 8:34am UTC](https://discuss.elastic.co/t/unable-to-retrieve-all-the-facet-info-when-using-java-api/13814/3 "2013-10-01T08:34:19Z")

</div>

David - Thanks much for the prompt response!

Actually on my server side (since I am writing some generic code), I am  
trying to understand what a right design should be since I do not know the  
specific Facet class. So in order to cast it, I would need to additionally  
store the facet name \<--\> Facet class mapping which would allow me to  
determine the right Facet class. Does this look like a right way?

-Amit.

On Tue, Oct 1, 2013 at 1:12 AM, David Pilato [david@pilato.fr](mailto:david@pilato.fr) wrote:

> Just cast your Facet with the right class.  
> See:  
> [https://github.com/dadoonet/hands-on/blob/answers/src/test/java/org/elasticsearchfr/handson/ex3/FacetTest.java#L157](https://github.com/dadoonet/hands-on/blob/answers/src/test/java/org/elasticsearchfr/handson/ex3/FacetTest.java#L157)
> 
> --  
> _David Pilato_ | _Technical Advocate_ | _[Elasticsearch.com](http://Elasticsearch.com)_  
> @dadoonet [https://twitter.com/dadoonet](https://twitter.com/dadoonet) | @elasticsearchfr[https://twitter.com/elasticsearchfr](https://twitter.com/elasticsearchfr)  
> | @scrutmydocs [https://twitter.com/scrutmydocs](https://twitter.com/scrutmydocs)
> 
> Le 1 oct. 2013 à 10:05, Amit Soni [amitsoni29@gmail.com](mailto:amitsoni29@gmail.com) a écrit :
> 
> Hi everyone - I am using Java API client to execute my queries and the  
> request has facets too. After the query has been executed, I receive an  
> instance of org.elasticsearch.action.search.SeachResponse.
> 
> However I am unable to retrieve all the factet information from this  
> SearchResponse instance. I tried SearchResponse # getFacets() #  
> getFacets() and this returns me a Map. The key of this map is the facet  
> name which is right and using this I retrieve an instance of  
> org.elasticsearch.search.facet.Facet and this is where I am stuck. This  
> interface (Facet) has just two methods (Facet # getName() and Facet #  
> getType()) and these two methods are not enough to retrieve all the facet  
> info like the count etc.
> 
> Do I need to determine the type of each facet and then type cast it to  
> specific Facet so that I have more methods available? I am writing some  
> generic code and hence trying not to do type casting.
> 
> Thoughts?
> 
> Thanks much!
> 
> -Amit.
> 
> --  
> 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).
> 
> --  
> 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).

--  
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:** ![jprante](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jprante/32/44941_2.png) [@jprante](https://discuss.elastic.co/u/jprante)\
**Post date:** [October 1, 2013, 8:52am UTC](https://discuss.elastic.co/t/unable-to-retrieve-all-the-facet-info-when-using-java-api/13814/4 "2013-10-01T08:52:35Z")

</div>

No need for a class mapping. Each facet carries the facet name. Just keep a  
list of facet classes for iteration over getFacets() you used to build the  
query with the facet factories, e.g. FacetBuilders.termsFacet("facetname")

Jörg

--  
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:14am UTC](https://discuss.elastic.co/t/unable-to-retrieve-all-the-facet-info-when-using-java-api/13814/5 "2017-07-06T02:14:06Z")

</div>


