Solr涓殑group涓巉acet鐨勫尯鍒?/p>
濡傛灉鏄畝鍗曠殑浣跨敤鐨勮瘽錛岄偅涔團acet涓巊roup閮藉彲浠ョ敤鏉ヨ繘琛屾暟鎹殑鑱氬悎鏌ヨ錛屼絾鏄粬浠繕鏄湁寰堝ぇ鐨勫尯鍒殑銆?/p>
棣栧厛涓奻acet璺焔roup鐨勬搷浣滐細
Facet鐨勪緥瀛愶細
public voidFacetFieldQuery() throws Exception {
solrServer = createSolrServer();
SolrQueryquery = newSolrQuery();//寤虹珛涓涓柊鐨勬煡璇?/span>
query.setQuery("jobsName:璁$畻鏈虹淮鎶?/span>");
query.setFacet(true);//璁劇疆facet=on
// 鍒嗙被淇℃伅鍒嗕負錛氳柂姘達紝鍙戝竷鏃墮棿錛屾暀鑲茶儗鏅紝宸ヤ綔緇忛獙錛屽叕鍙哥被鍨嬶紝宸ヤ綔綾誨瀷
query.addFacetField(new String[] {"salary","publishDate",
"educateBackground","jobExperience","companytype","jobsType" });//璁劇疆闇瑕?/span>facet鐨勫瓧孌?/span>
query.setFacetLimit(10);// 闄愬埗facet榪斿洖鐨勬暟閲?/span>
query.setFacetMissing(false);//涓嶇粺璁?/span>null鐨勫?/span>
query.setFacetMinCount(1);// 璁劇疆榪斿洖鐨勬暟鎹腑姣忎釜鍒嗙粍鐨勬暟鎹渶灝忓鹼紝姣斿璁劇疆涓?/span>1錛屽垯緇熻鏁伴噺鏈灝忎負1錛屼笉鐒朵笉鏄劇ず
//query.addFacetQuery("publishDate:[2014-04-11T00:00:00Z TO2014-04-13T00:00:00Z]");
QueryResponseresponse = solrServer.query(query);
System.out.println("鏌ヨ鏃墮棿錛?/span>" + response.getQTime());
List<FacetField>facets = response.getFacetFields();//榪斿洖鐨?/span>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();
}
}
榪愯緇撴灉濡備笅錛?/p>
鏌ヨ鏃墮棿錛?6
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鏌ヨ錛?/p>
/**group鏌ヨ
* @throws Exception
*/
public void GroupFieldQuery() throws Exception {
solrServer = createSolrServer();
SolrQuery query = new SolrQuery("jobsName:璁$畻鏈虹淮鎶?/span>");
// 璁劇疆閫氳繃facet鏌ヨ涓?/span>true錛岃〃紺烘煡璇㈡椂浣跨敤facet鏈哄埗
query.setParam(GroupParams.GROUP,true);
query.setParam(GroupParams.GROUP_FIELD,"salary");
// 璁劇疆姣忎釜quality瀵瑰簲鐨?/span>
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鐨勬煡璇㈢粨鏋滀富瑕佹槸鍒嗙粍淇℃伅錛氭湁浠涔堝垎緇勶紝姣忎釜鍒嗙粍鍖呮嫭澶氬皯璁板綍錛涗絾鏄垎緇勪腑鏈夊摢浜涙暟鎹槸涓嶅彲鐭ラ亾鐨勶紝鍙湁榪涗竴姝ユ悳绱€?br /> group鍒欑被浼間簬鍏崇郴鏁版嵁搴撶殑group by錛屽彲浠ョ敤浜庝竴涓垨鑰呭嚑涓瓧孌靛幓閲嶃佹樉紺轟竴涓猤roup鐨勫墠鍑犳潯璁板綍絳夈?/p>
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鐨?/p>
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
鍏朵腑facet鏌ヨ鍑虹殑濡備笅錛氾紙鍙埅鍙栭儴鍒嗙粨鏋滐級
鏍規嵁鏉′歡鏌ヨ鍑虹殑鏄煡璇㈢粨鏋滐紝facet鏄仛綾誨悗鐨勪俊鎭窡鏌ヨ鏉′歡鏄垎寮鐨勶紝鏌ヨ緇撴灉涔熻窡facet娌″叧緋匯?/p>
浣嗘槸涓嬮潰鐪媑roup鏌ヨ鐨?/p>
涔熷氨鏄綘鐨勬煡璇㈡潯浠舵槸璺焔roup鐩稿叧鐨勶紝榪斿洖鐨勬煡璇㈢粨鏋滀篃鏄窡group鐩稿叧鐨勶紝姣斿璇翠綘鎯寵鏌ヨ鐨勭粨鏋滃湪姣忎釜鍒嗙粍涓?閮芥湁鏁版嵁閲囬泦錛岄偅涔堝氨鏈濂界敤group錛岃繖鏍峰嚭鏉ョ殑鏁版嵁璺焔roup涔熸槸鐩稿叧鐨勶紝浣嗘槸鏈変釜闂錛屾瘮濡傝浣犺鏌ヨgroup姣忎釜閲囬泦1涓紝ok閭d箞浣犳煡璇㈢殑 鏃跺欑殑鏉′歡rows灝辨棤鏁堜簡錛堜篃涓嶈兘璇存棤鏁堬紝涓昏鏄湅浣犳庝箞浣跨敤錛夛紝灝辨槸鏈澶氭瘡涓垎緇勭粰浣犺繑鍥炰竴涓紝澶氫簡娌℃湁浜嗐?/p>
鍐嶇粏璇寸偣灝辨槸濡傛灉浣犳兂鏌ヨ褰掓煡璇㈣仛綾誨綊鑱氱被錛岄偅涔堜嬌鐢╢acet錛屽鏋滄兂浣跨敤綾諱技閲囬泦鐨勬晥鏋滐紝姣忎釜group鍒嗙粍閲囬泦澶氬皯涓紝閭d箞浣跨敤group鏌ヨ銆?/p>