Solr中的group與facet的區(qū)別
如果是簡單的使用的話,那么Facet與group都可以用來進(jìn)行數(shù)據(jù)的聚合查詢,但是他們還是有很大的區(qū)別的。
首先上facet跟group的操作:
Facet的例子:
public voidFacetFieldQuery() throws Exception {
solrServer = createSolrServer();
SolrQueryquery = newSolrQuery();//建立一個新的查詢
query.setQuery("jobsName:計(jì)算機(jī)維護(hù)");
query.setFacet(true);//設(shè)置facet=on
// 分類信息分為:薪水,發(fā)布時間,教育背景,工作經(jīng)驗(yàn),公司類型,工作類型
query.addFacetField(new String[] {"salary","publishDate",
"educateBackground","jobExperience","companytype","jobsType" });//設(shè)置需要facet的字段
query.setFacetLimit(10);// 限制facet返回的數(shù)量
query.setFacetMissing(false);//不統(tǒng)計(jì)null的值
query.setFacetMinCount(1);// 設(shè)置返回的數(shù)據(jù)中每個分組的數(shù)據(jù)最小值,比如設(shè)置為1,則統(tǒng)計(jì)數(shù)量最小為1,不然不顯示
//query.addFacetQuery("publishDate:[2014-04-11T00:00:00Z TO2014-04-13T00:00:00Z]");
QueryResponseresponse = solrServer.query(query);
System.out.println("查詢時間:" + response.getQTime());
List<FacetField>facets = response.getFacetFields();//返回的facet列表
for (FacetField facet :facets) {
System.out.println(facet.getName());
System.out.println("----------------");
List<Count>counts = facet.getValues();
for (Count count : counts){
System.out.println(count.getName()+":"+ count.getCount());
}
System.out.println();
}
}
運(yùn)行結(jié)果如下:
查詢時間:66
salary
----------------
面議:6882
2001-4000:1508
其他:671
4001-6000:536
3000-4499:224
2000-2999:181
6001-8000:179
3000-5000:82
1000-2000:81
4500-5999:75
publishDate
----------------
2014-08-05T00:00:00Z:793
2014-08-04T00:00:00Z:775
2014-07-30T00:00:00Z:601
2014-08-07T00:00:00Z:548
2014-08-06T00:00:00Z:539
2014-08-11T00:00:00Z:472
2014-08-20T00:00:00Z:439
2014-08-12T00:00:00Z:438
2014-08-01T00:00:00Z:405
2014-08-03T00:00:00Z:376
educateBackground
----------------
大專:4486
本科:1872
其他:1344
不限:1147
中專:680
高中:472
薪水范圍::430
中技:161
初中:140
碩士:94
jobExperience
----------------
其他:2623
不限:2249
1-3年:1770
1年:1301
2年:773
3-4年:528
3-5年:379
應(yīng)屆畢業(yè)生:309
5-7年:162
1年以上:136
companytype
----------------
民營公司:3702
民營:2605
國企:835
股份制企業(yè):729
其他:707
合資:632
外資(非歐美):377
外商獨(dú)資:350
外資(歐美):271
上市公司:228
jobsType
----------------
全職:10734
兼職:59
實(shí)習(xí):39
Group查詢:
/**group查詢
* @throws Exception
*/
public void GroupFieldQuery() throws Exception {
solrServer = createSolrServer();
SolrQuery query = new SolrQuery("jobsName:計(jì)算機(jī)維護(hù)");
// 設(shè)置通過facet查詢?yōu)?/span>true,表示查詢時使用facet機(jī)制
query.setParam(GroupParams.GROUP,true);
query.setParam(GroupParams.GROUP_FIELD,"salary");
// 設(shè)置每個quality對應(yīng)的
query.setParam(GroupParams.GROUP_LIMIT,"1");
// 設(shè)置返回doc文檔數(shù)據(jù),因只需要數(shù)量,故設(shè)置為0
query.setRows(10);
QueryResponse response = solrServer.query(query);
if (response !=null) {
GroupResponse groupResponse =response.getGroupResponse();
if(groupResponse !=null) {
List<GroupCommand> groupList =groupResponse.getValues();
for(GroupCommand groupCommand : groupList){
List<Group> groups =groupCommand.getValues();
for(Group group : groups) {
System.out.println("group查詢..."+group.getGroupValue()+"數(shù)量為:"+group.getResult().getNumFound());
}
}
}
}
}
group查詢...面議數(shù)量為:6882
group查詢...4500-5999數(shù)量為:75
group查詢...2001-4000數(shù)量為:1508
group查詢...其他數(shù)量為:671
group查詢...2000-2999數(shù)量為:181
group查詢...4001-6000數(shù)量為:536
group查詢...2000-4000數(shù)量為:19
group查詢...2000-3000數(shù)量為:34
group查詢...3000-4499數(shù)量為:224
group查詢...3000-5000數(shù)量為:82
facet的查詢結(jié)果主要是分組信息:有什么分組,每個分組包括多少記錄;但是分組中有哪些數(shù)據(jù)是不可知道的,只有進(jìn)一步搜索。
group則類似于關(guān)系數(shù)據(jù)庫的group by,可以用于一個或者幾個字段去重、顯示一個group的前幾條記錄等。
The Grouping feature only works if groups are inthe same shard. You must use the custom sharding feature to use the Groupingfeature.
兩者其實(shí)用起來還是有比較大的區(qū)別的,但是如果說區(qū)別的話可以看下wiki上的這段
Field Collapsing and Result Grouping aredifferent ways to think about the same Solr feature.
Field Collapsing collapsesa group of results with the same field value down to a single (or fixed number)of entries. For example, most search engines such as Google collapse on site soonly one or two entries are shown, along with a link to click to see moreresults from that site. Field collapsing can also be used to suppress duplicatedocuments.
Result Grouping groupsdocuments with a common field value into groups, returning the top documentsper group, and the top groups based on what documents are in the groups. Oneexample is a search at Best Buy for a common term such as DVD, that shows thetop 3 results for each category ("TVs &Video","Movies","Computers", etc)
下面這兩個查詢語句一個是facet的一個是group的
http://localhost:8080/solr/JobsOtherWeb0/select?q=jobsName%3A%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%BB%B4%E6%8A%A4&group=true&group.field=salary&group.limit=1&rows=10
http://localhost:8080/solr/JobsOtherWeb0/select?q=jobsName%3A%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%BB%B4%E6%8A%A4&facet=true&facet.field=salary&facet.field=publishDate&facet.field=educateBackground&facet.field=jobExperience&facet.field=companytype&facet.field=jobsType&facet.limit=10&facet.missing=false&facet.mincount=1
其中facet查詢出的如下:(只截取部分結(jié)果)



根據(jù)條件查詢出的是查詢結(jié)果,facet是聚類后的信息跟查詢條件是分開的,查詢結(jié)果也跟facet沒關(guān)系。
但是下面看group查詢的



也就是你的查詢條件是跟group相關(guān)的,返回的查詢結(jié)果也是跟group相關(guān)的,比如說你想要查詢的結(jié)果在每個分組中 都有數(shù)據(jù)采集,那么就最好用group,這樣出來的數(shù)據(jù)跟group也是相關(guān)的,但是有個問題,比如說你要查詢group每個采集1個,ok那么你查詢的 時候的條件rows就無效了(也不能說無效,主要是看你怎么使用),就是最多每個分組給你返回一個,多了沒有了。
再細(xì)說點(diǎn)就是如果你想查詢歸查詢聚類歸聚類,那么使用facet,如果想使用類似采集的效果,每個group分組采集多少個,那么使用group查詢。