A problem of facet on search response


(Yang) #1

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,
I didn't find some thing could help me resolve this problem.

Thanks very much~~

--
Yang


(vineeth mohan) #2

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 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,
I didn't find some thing could help me resolve this problem.

Thanks very much~~

--
Yang


(Yang) #3

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 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,
I didn't find some thing could help me resolve this problem.

Thanks very much~~

--
Yang


(system) #4