# A problem of facet on search response

**URL:** <https://discuss.elastic.co/t/a-problem-of-facet-on-search-response/7502>\
**Category:** Elasticsearch\
**Created:** [April 28, 2012, 6:08am UTC](https://discuss.elastic.co/t/a-problem-of-facet-on-search-response/7502 "2012-04-28T06:08:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yang](https://avatars.discourse-cdn.com/v4/letter/y/77aa72/32.png) [@Yang](https://discuss.elastic.co/u/Yang)\
**Post date:** [April 28, 2012, 6:08am UTC](https://discuss.elastic.co/t/a-problem-of-facet-on-search-response/7502/1 "2012-04-28T06:08:14Z")

</div>

Dear,

I'm playing with Elastic Search, it's powerful and easy to use.

But here I meet a problem about facet on SearchResponse.

I'm trying to build a html page to display how many media files does a man  
own, just like:

username:  
jacky(7)  
yang(5)  
shawn(6)  
mediatype:  
mp3(3)  
mp4(4)  
rmvb(5)  
mkv(6)

this means jacky owns 7 media files, yang owns 5 media file and so on,  
and also there are 3 mp3 files, 4 mp4 files and so on.

and my schema like this:

{"username":"jacky", "mname":"abc1.mp3", "mtype":"mp3"}  
{"username":"jacky", "mname":"abc2.mp3", "mtype":"mp3"}  
{"username":"jacky", "mname":"abc3.mp3", "mtype":"mp3"}

{"username":"jacky", "mname":"abc1.mp4", "mtype":"mp4"}  
{"username":"jacky", "mname":"abc2.mp4", "mtype":"mp4"}  
{"username":"jacky", "mname":"abc3.mp4", "mtype":"mp4"}  
{"username":"jacky", "mname":"abc4.mp4", "mtype":"mp4"}

{"username":"yang", "mname":"abc1.rmvb", "mtype":"rmvb"}  
{"username":"yang", "mname":"abc2.rmvb", "mtype":"rmvb"}  
{"username":"yang", "mname":"abc3.rmvb", "mtype":"rmvb"}  
{"username":"yang", "mname":"abc4.rmvb", "mtype":"rmvb"}  
{"username":"yang", "mname":"abc5.rmvb", "mtype":"rmvb"}

{"username":"shawn", "mname":"abc1.mkv", "mtype":"mkv"}  
{"username":"shawn", "mname":"abc2.mkv", "mtype":"mkv"}  
{"username":"shawn", "mname":"abc3.mkv", "mtype":"mkv"}  
{"username":"shawn", "mname":"abc4.mkv", "mtype":"mkv"}  
{"username":"shawn", "mname":"abc5.mkv", "mtype":"mkv"}  
{"username":"shawn", "mname":"abc6.mkv", "mtype":"mkv"}

//I just execute the es java api like below:  
TermsFacetBuilder facet = new TermsFacetBuilder("userandmtype");  
facet.fields("username", "mtype");  
...  
searchRequest.setFacet(facet);  
... execute search request and get the response

I print the searchResponse, below is facet part:

...  
"facets" : {  
"userandmtype" : {  
"\_type" : "terms",  
"missing" : 0,  
"total" : 18,  
"other" : 0,  
"terms" : [  
{"term" : "mp3", "count" : 3},  
{"term" : "mp4", "count" : 4},  
{"term" : "rmvb", "count" : 5},  
{"term" : "mkv", "count" : 6},  
{"term" : "jacky", "count" : 7},  
{"term" : "yang", "count" : 5},  
{"term" : "shawn", "count" : 6}  
]  
}  
}  
...

from this result, I have to manually define which term would be displayed  
under "username",  
and which term would be displayed under "mediatype".

I'm wondering whether there is a way to get the facets result like this:

...  
"facets" : {  
"userandmtype" : {  
"\_type" : "terms",  
"missing" : 0,  
"total" : 18,  
"other" : 0,  
"terms" : [  
{"mtype":{{"term" : "mp3", "count" : 3},  
{"term" : "mp4", "count" : 4},  
{"term" : "rmvb", "count" : 5},  
{"term" : "mkv", "count" : 6}},  
"username":{{"term" : "jacky", "count" : 7},  
{"term" : "yang", "count" : 5},  
{"term" : "shawn", "count" : 6}}}  
]  
}  
}  
...

or some way else could let me know which term belongs to which field.

Could anybody give me a hint or some resource to refer, as I saw the guide  
on [elasticsearch.org](http://elasticsearch.org),  
I didn't find some thing could help me resolve this problem.

Thanks very much~~

--  
Yang

---

<div class="post-metadata">

**Author:** ![vineeth\_mohan](https://avatars.discourse-cdn.com/v4/letter/v/bc79bd/32.png) [@vineeth\_mohan](https://discuss.elastic.co/u/vineeth_mohan)\
**Post date:** [April 28, 2012, 6:44am UTC](https://discuss.elastic.co/t/a-problem-of-facet-on-search-response/7502/2 "2012-04-28T06:44:30Z")

</div>

you should give 2 facets and not just one

TermsFacetBuilder facetUser = new TermsFacetBuilder("user");  
facet.fields("username");

TermsFacetBuilder facetMType = new TermsFacetBuilder("mtype");  
facet.fields("mtype");

And add both to the search query.

Thanks  
Vineeth

On Fri, Apr 27, 2012 at 11:08 PM, Yang [yuyang030405@gmail.com](mailto:yuyang030405@gmail.com) wrote:

> Dear,
> 
> I'm playing with Elastic Search, it's powerful and easy to use.
> 
> But here I meet a problem about facet on SearchResponse.
> 
> I'm trying to build a html page to display how many media files does a man  
> own, just like:
> 
> username:  
> jacky(7)  
> yang(5)  
> shawn(6)  
> mediatype:  
> mp3(3)  
> mp4(4)  
> rmvb(5)  
> mkv(6)
> 
> this means jacky owns 7 media files, yang owns 5 media file and so on,  
> and also there are 3 mp3 files, 4 mp4 files and so on.
> 
> and my schema like this:
> 
> {"username":"jacky", "mname":"abc1.mp3", "mtype":"mp3"}  
> {"username":"jacky", "mname":"abc2.mp3", "mtype":"mp3"}  
> {"username":"jacky", "mname":"abc3.mp3", "mtype":"mp3"}
> 
> {"username":"jacky", "mname":"abc1.mp4", "mtype":"mp4"}  
> {"username":"jacky", "mname":"abc2.mp4", "mtype":"mp4"}  
> {"username":"jacky", "mname":"abc3.mp4", "mtype":"mp4"}  
> {"username":"jacky", "mname":"abc4.mp4", "mtype":"mp4"}
> 
> {"username":"yang", "mname":"abc1.rmvb", "mtype":"rmvb"}  
> {"username":"yang", "mname":"abc2.rmvb", "mtype":"rmvb"}  
> {"username":"yang", "mname":"abc3.rmvb", "mtype":"rmvb"}  
> {"username":"yang", "mname":"abc4.rmvb", "mtype":"rmvb"}  
> {"username":"yang", "mname":"abc5.rmvb", "mtype":"rmvb"}
> 
> {"username":"shawn", "mname":"abc1.mkv", "mtype":"mkv"}  
> {"username":"shawn", "mname":"abc2.mkv", "mtype":"mkv"}  
> {"username":"shawn", "mname":"abc3.mkv", "mtype":"mkv"}  
> {"username":"shawn", "mname":"abc4.mkv", "mtype":"mkv"}  
> {"username":"shawn", "mname":"abc5.mkv", "mtype":"mkv"}  
> {"username":"shawn", "mname":"abc6.mkv", "mtype":"mkv"}
> 
> //I just execute the es java api like below:  
> TermsFacetBuilder facet = new TermsFacetBuilder("userandmtype");  
> facet.fields("username", "mtype");  
> ...  
> searchRequest.setFacet(facet);  
> ... execute search request and get the response
> 
> I print the searchResponse, below is facet part:
> 
> ...  
> "facets" : {  
> "userandmtype" : {  
> "\_type" : "terms",  
> "missing" : 0,  
> "total" : 18,  
> "other" : 0,  
> "terms" : [  
> {"term" : "mp3", "count" : 3},  
> {"term" : "mp4", "count" : 4},  
> {"term" : "rmvb", "count" : 5},  
> {"term" : "mkv", "count" : 6},  
> {"term" : "jacky", "count" : 7},  
> {"term" : "yang", "count" : 5},  
> {"term" : "shawn", "count" : 6}  
> ]  
> }  
> }  
> ...
> 
> from this result, I have to manually define which term would be displayed  
> under "username",  
> and which term would be displayed under "mediatype".
> 
> I'm wondering whether there is a way to get the facets result like this:
> 
> ...  
> "facets" : {  
> "userandmtype" : {  
> "\_type" : "terms",  
> "missing" : 0,  
> "total" : 18,  
> "other" : 0,  
> "terms" : [  
> {"mtype":{{"term" : "mp3", "count" : 3},  
> {"term" : "mp4", "count" : 4},  
> {"term" : "rmvb", "count" : 5},  
> {"term" : "mkv", "count" : 6}},  
> "username":{{"term" : "jacky", "count" : 7},  
> {"term" : "yang", "count" : 5},  
> {"term" : "shawn", "count" : 6}}}  
> ]  
> }  
> }  
> ...
> 
> or some way else could let me know which term belongs to which field.
> 
> Could anybody give me a hint or some resource to refer, as I saw the guide  
> on [elasticsearch.org](http://elasticsearch.org),  
> I didn't find some thing could help me resolve this problem.
> 
> Thanks very much~~
> 
> --  
> Yang

---

<div class="post-metadata">

**Author:** ![Yang](https://avatars.discourse-cdn.com/v4/letter/y/77aa72/32.png) [@Yang](https://discuss.elastic.co/u/Yang)\
**Post date:** [April 28, 2012, 7:06am UTC](https://discuss.elastic.co/t/a-problem-of-facet-on-search-response/7502/3 "2012-04-28T07:06:04Z")

</div>

WOW ~~~~  
It works, cool!!!!!  
Thank you very much, Vineeth!!!!

On Saturday, April 28, 2012 2:44:30 PM UTC+8, Vineeth Mohan wrote:

> you should give 2 facets and not just one
> 
> TermsFacetBuilder facetUser = new TermsFacetBuilder("user");  
> facet.fields("username");
> 
> TermsFacetBuilder facetMType = new TermsFacetBuilder("mtype");  
> facet.fields("mtype");
> 
> And add both to the search query.
> 
> Thanks  
> Vineeth
> 
> On Fri, Apr 27, 2012 at 11:08 PM, Yang [yuyang030405@gmail.com](mailto:yuyang030405@gmail.com) wrote:
> 
> > Dear,
> > 
> > I'm playing with Elastic Search, it's powerful and easy to use.
> > 
> > But here I meet a problem about facet on SearchResponse.
> > 
> > I'm trying to build a html page to display how many media files does a  
> > man own, just like:
> > 
> > username:  
> > jacky(7)  
> > yang(5)  
> > shawn(6)  
> > mediatype:  
> > mp3(3)  
> > mp4(4)  
> > rmvb(5)  
> > mkv(6)
> > 
> > this means jacky owns 7 media files, yang owns 5 media file and so on,  
> > and also there are 3 mp3 files, 4 mp4 files and so on.
> > 
> > and my schema like this:
> > 
> > {"username":"jacky", "mname":"abc1.mp3", "mtype":"mp3"}  
> > {"username":"jacky", "mname":"abc2.mp3", "mtype":"mp3"}  
> > {"username":"jacky", "mname":"abc3.mp3", "mtype":"mp3"}
> > 
> > {"username":"jacky", "mname":"abc1.mp4", "mtype":"mp4"}  
> > {"username":"jacky", "mname":"abc2.mp4", "mtype":"mp4"}  
> > {"username":"jacky", "mname":"abc3.mp4", "mtype":"mp4"}  
> > {"username":"jacky", "mname":"abc4.mp4", "mtype":"mp4"}
> > 
> > {"username":"yang", "mname":"abc1.rmvb", "mtype":"rmvb"}  
> > {"username":"yang", "mname":"abc2.rmvb", "mtype":"rmvb"}  
> > {"username":"yang", "mname":"abc3.rmvb", "mtype":"rmvb"}  
> > {"username":"yang", "mname":"abc4.rmvb", "mtype":"rmvb"}  
> > {"username":"yang", "mname":"abc5.rmvb", "mtype":"rmvb"}
> > 
> > {"username":"shawn", "mname":"abc1.mkv", "mtype":"mkv"}  
> > {"username":"shawn", "mname":"abc2.mkv", "mtype":"mkv"}  
> > {"username":"shawn", "mname":"abc3.mkv", "mtype":"mkv"}  
> > {"username":"shawn", "mname":"abc4.mkv", "mtype":"mkv"}  
> > {"username":"shawn", "mname":"abc5.mkv", "mtype":"mkv"}  
> > {"username":"shawn", "mname":"abc6.mkv", "mtype":"mkv"}
> > 
> > //I just execute the es java api like below:  
> > TermsFacetBuilder facet = new TermsFacetBuilder("userandmtype");  
> > facet.fields("username", "mtype");  
> > ...  
> > searchRequest.setFacet(facet);  
> > ... execute search request and get the response
> > 
> > I print the searchResponse, below is facet part:
> > 
> > ...  
> > "facets" : {  
> > "userandmtype" : {  
> > "\_type" : "terms",  
> > "missing" : 0,  
> > "total" : 18,  
> > "other" : 0,  
> > "terms" : [  
> > {"term" : "mp3", "count" : 3},  
> > {"term" : "mp4", "count" : 4},  
> > {"term" : "rmvb", "count" : 5},  
> > {"term" : "mkv", "count" : 6},  
> > {"term" : "jacky", "count" : 7},  
> > {"term" : "yang", "count" : 5},  
> > {"term" : "shawn", "count" : 6}  
> > ]  
> > }  
> > }  
> > ...
> > 
> > from this result, I have to manually define which term would be displayed  
> > under "username",  
> > and which term would be displayed under "mediatype".
> > 
> > I'm wondering whether there is a way to get the facets result like this:
> > 
> > ...  
> > "facets" : {  
> > "userandmtype" : {  
> > "\_type" : "terms",  
> > "missing" : 0,  
> > "total" : 18,  
> > "other" : 0,  
> > "terms" : [  
> > {"mtype":{{"term" : "mp3", "count" : 3},  
> > {"term" : "mp4", "count" : 4},  
> > {"term" : "rmvb", "count" : 5},  
> > {"term" : "mkv", "count" : 6}},  
> > "username":{{"term" : "jacky", "count" : 7},  
> > {"term" : "yang", "count" : 5},  
> > {"term" : "shawn", "count" : 6}}}  
> > ]  
> > }  
> > }  
> > ...
> > 
> > or some way else could let me know which term belongs to which field.
> > 
> > Could anybody give me a hint or some resource to refer, as I saw the  
> > guide on [elasticsearch.org](http://elasticsearch.org),  
> > I didn't find some thing could help me resolve this problem.
> > 
> > Thanks very much~~
> > 
> > --  
> > Yang

---

<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/a-problem-of-facet-on-search-response/7502/4 "2017-07-06T03:30:46Z")

</div>


