# TermsFacetBuilder for Search using java Cleint

**URL:** <https://discuss.elastic.co/t/termsfacetbuilder-for-search-using-java-cleint/3315>\
**Category:** Elasticsearch\
**Created:** [September 9, 2010, 9:33am UTC](https://discuss.elastic.co/t/termsfacetbuilder-for-search-using-java-cleint/3315 "2010-09-09T09:33:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![yogeshronly4u](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yogeshronly4u/32/3322_2.png) [@yogeshronly4u](https://discuss.elastic.co/u/yogeshronly4u)\
**Post date:** [September 9, 2010, 9:33am UTC](https://discuss.elastic.co/t/termsfacetbuilder-for-search-using-java-cleint/3315/1 "2010-09-09T09:33:42Z")

</div>

Hi,

M looking for any working example or suggestion as how to use terms  
facet under search for java client.

I need to convert following REST Query to corresponding Java Client  
Call.

{  
"size" : 1,  
"query" : {  
"term" : {  
"nto" : "627331749"  
}  
},  
"facets": {  
"tag": {  
"terms" : {  
"field": "ncontent.cnt.gid",  
"size": 5  
},  
"filter" : {  
"term" : {  
"nuid" : "627331749"  
}  
}  
}  
}  
}

I have managed with following , now need to integrate terms facet  
builder with rest of code

SearchResponse response =  
notifyDataStrucAbstract.elasticSearchClient.prepareSearch(ConfigList.getString("elasticSearchDbName"))  
.setSearchType(SearchType.DFS\_QUERY\_THEN\_FETCH)  
.setQuery(termQuery("nto" , uid))  
.setFrom(0).setSize(60).setExplain(true)  
.addFacet(facet)  
.execute()  
.actionGet();

Any kind of input will be appreciated

---

<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:** [September 9, 2010, 12:26pm UTC](https://discuss.elastic.co/t/termsfacetbuilder-for-search-using-java-cleint/3315/2 "2010-09-09T12:26:43Z")

</div>

The FacetBuilders allow you to construct facets that can be added to the  
addFacet method. Note that in the latest REST interface, a filter for a  
facet is now called facet\_filter.

```
    SearchResponse searchResponse = client.prepareSearch()
            .setSize(0)
            .setQuery(termQuery("stag", "111"))

```

.addFacet(FacetBuilders.termsFacet("facet1").field("stag").size(10))  
.execute().actionGet();

-shay.banon

On Thu, Sep 9, 2010 at 12:33 PM, [yogeshronly4u@gmail.com](mailto:yogeshronly4u@gmail.com) \<  
[yogeshronly4u@gmail.com](mailto:yogeshronly4u@gmail.com)\> wrote:

> Hi,
> 
> M looking for any working example or suggestion as how to use terms  
> facet under search for java client.
> 
> I need to convert following REST Query to corresponding Java Client  
> Call.
> 
> {  
> "size" : 1,  
> "query" : {  
> "term" : {  
> "nto" : "627331749"  
> }  
> },  
> "facets": {  
> "tag": {  
> "terms" : {  
> "field": "ncontent.cnt.gid",  
> "size": 5  
> },  
> "filter" : {  
> "term" : {  
> "nuid" : "627331749"  
> }  
> }  
> }  
> }  
> }
> 
> I have managed with following , now need to integrate terms facet  
> builder with rest of code
> 
> SearchResponse response =
> 
> notifyDataStrucAbstract.elasticSearchClient.prepareSearch(ConfigList.getString("elasticSearchDbName"))
> 
> .setSearchType(SearchType.DFS\_QUERY\_THEN\_FETCH)  
> .setQuery(termQuery("nto" , uid))  
> .setFrom(0).setSize(60).setExplain(true)  
> .addFacet(facet)  
> .execute()  
> .actionGet();
> 
> Any kind of input will be appreciated

---

<div class="post-metadata">

**Author:** ![yogeshronly4u](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yogeshronly4u/32/3322_2.png) [@yogeshronly4u](https://discuss.elastic.co/u/yogeshronly4u)\
**Post date:** [September 13, 2010, 6:50am UTC](https://discuss.elastic.co/t/termsfacetbuilder-for-search-using-java-cleint/3315/3 "2010-09-13T06:50:09Z")

</div>

Thnx Shay , that helped.

I understand ppl concentrating more on getting gr8 app like ES to  
higher excellence n i congratulate whole team for the same, only thing  
we need to address now is on full scale documentation and working  
examples for bigger developer audience.

Looking forward for more treats from ES, Cheers 😉

---

<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:19am UTC](https://discuss.elastic.co/t/termsfacetbuilder-for-search-using-java-cleint/3315/4 "2017-07-06T04:19:26Z")

</div>


