Solr中的group與facet的區別
如果是簡單的使用的話,那么Facet與group都可以用來進行數據的聚合查詢,但是他們還是有很大的區別的。
首先上facet跟group的操作:
Facet的例子:
public voidFacetFieldQuery() throws Exception {
solrServer = createSolrServer();
SolrQueryquery = newSolrQuery();//建立一個新的查詢
query.setQuery("jobsName:計算機維護");
query.setFacet(true);//設置facet=on
// 分類信息分為:薪水,發布時間,教育背景,工作經驗,公司類型,工作類型
query.addFacetField(new String[] {"salary","publishDate",
"educateBackground","jobExperience","companytype","jobsType" });//設置需要facet的字段
query.setFacetLimit(10);// 限制facet返回的數量
query.setFacetMissing(false);//不統計null的值
query.setFacetMinCount(1);// 設置返回的數據中每個分組的數據最小值,比如設置為1,則統計數量最小為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();
}
}
運行結果如下:
查詢時間: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
應屆畢業生:309
5-7年:162
1年以上:136
companytype
----------------
民營公司:3702
民營:2605
國企:835
股份制企業:729
其他:707
合資:632
外資(非歐美):377
外商獨資:350
外資(歐美):271
上市公司:228
jobsType
----------------
全職:10734
兼職:59
實習:39
Group查詢:
/**group查詢
* @throws Exception
*/
public void GroupFieldQuery() throws Exception {
solrServer = createSolrServer();
SolrQuery query = new SolrQuery("jobsName:計算機維護");
// 設置通過facet查詢為true,表示查詢時使用facet機制
query.setParam(GroupParams.GROUP,true);
query.setParam(GroupParams.GROUP_FIELD,"salary");
// 設置每個quality對應的
query.setParam(GroupParams.GROUP_LIMIT,"1");
// 設置返回doc文檔數據,因只需要數量,故設置為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()+"數量為:"+group.getResult().getNumFound());
}
}
}
}
}
group查詢...面議數量為:6882
group查詢...4500-5999數量為:75
group查詢...2001-4000數量為:1508
group查詢...其他數量為:671
group查詢...2000-2999數量為:181
group查詢...4001-6000數量為:536
group查詢...2000-4000數量為:19
group查詢...2000-3000數量為:34
group查詢...3000-4499數量為:224
group查詢...3000-5000數量為:82
facet的查詢結果主要是分組信息:有什么分組,每個分組包括多少記錄;但是分組中有哪些數據是不可知道的,只有進一步搜索。
group則類似于關系數據庫的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.
兩者其實用起來還是有比較大的區別的,但是如果說區別的話可以看下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查詢出的如下:(只截取部分結果)



根據條件查詢出的是查詢結果,facet是聚類后的信息跟查詢條件是分開的,查詢結果也跟facet沒關系。
但是下面看group查詢的



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