solr高級查詢應用---按字段分組查詢(group)

solr的group查詢類似於關係數據庫的group by,可以用於一個或者幾個字段去重、顯示一個group的前幾條記錄等。

下面,進行簡單的實戰操作,以下solr中的測試數據,參考:https://blog.csdn.net/weixin_43231076/article/details/102687856

代碼如下:
SolrServer類:

public class SolrServer {

	private static final String url = "http://192.168.12.130:8080/test_core2";
	//private static final String url = "http://192.168.11.172:5306/test_core1";
	private static HttpSolrClient httpSolrClient = null;
	
	public static HttpSolrClient getSolrClient() {
		if(httpSolrClient == null) {
			httpSolrClient = new HttpSolrClient(url);
			httpSolrClient.setDefaultMaxConnectionsPerHost(1000);		//設置對應請求的目標主機線程數爲1000條
			httpSolrClient.setMaxTotalConnections(1000); 				//設置最大的連接數
			httpSolrClient.setConnectionTimeout(60000);					//設置連接超時時間(單位毫秒) 1000
			httpSolrClient.setSoTimeout(60000);							// 設置讀數據超時時間(單位毫秒) 1000
			httpSolrClient.setFollowRedirects(false);					//遵循從定向
			httpSolrClient.setAllowCompression(true);					//允許壓縮
		}
		return httpSolrClient;
	}
	
}

測試類:

package com.dss.solr.test;

import java.util.List;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrRequest;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.Group;
import org.apache.solr.client.solrj.response.GroupCommand;
import org.apache.solr.client.solrj.response.GroupResponse;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.params.CommonParams;
import org.apache.solr.common.params.GroupParams;

import com.dss.solr.server.SolrServer;

public class SolrGroupTest {

	public static void queryGroup() throws Exception{
		HttpSolrClient client = SolrServer.getSolrClient();
		
		SolrQuery sQuery = new SolrQuery();
		//打開分組功能
		sQuery.setParam(GroupParams.GROUP, true);
		
		//設置要進行group分組的field,如果要進行多個Field分組,則設置多個Field,如下	
		sQuery.setParam(GroupParams.GROUP_FIELD, "brand_s");
		//sQuery.setParam(GroupParams.GROUP_FIELD, "brand_s","subMajor_s");
		
		//設置每個分組最多返回的記錄數(默認爲1),如果只需要分組的數量,可以設置爲0
		sQuery.setParam(GroupParams.GROUP_LIMIT, "5");
		
		//設置每個分組裏從第幾條數據開始返回(默認是0),搭配GroupParams.GROUP_LIMIT可以進行組內分頁
		sQuery.setParam(GroupParams.GROUP_OFFSET, "0");
		
		//是否返回總的組數
		sQuery.setParam(GroupParams.GROUP_TOTAL_COUNT, true);
		
		//組內配置Field進行排序
		sQuery.setParam(GroupParams.GROUP_SORT, "id desc");
		
		//組件配置Field進行排序
		sQuery.setParam(CommonParams.SORT, "id desc");
		
		//這裏的start和rows用戶組件的分頁,即每次展示多少個組的數據
		sQuery.setStart(0);
		sQuery.setRows(10);
		
		//可以設置要求返回的Field
		sQuery.setParam(CommonParams.FL, "id,title_s,brand_s,subMajor_s");
		
		sQuery.setQuery("*:*");
		
		QueryResponse queryResponse = client.query(sQuery, SolrRequest.METHOD.POST);
		GroupResponse groupResponse = queryResponse.getGroupResponse();
		if(groupResponse != null) {
			/**
			 * 設置幾個Field進行分組,則values.size就是幾
			 */
			List<GroupCommand> values = groupResponse.getValues();
			if(values != null) {
				System.out.println("values.size()======" + values.size());
				for(GroupCommand value : values) {
					/**
					 * value.getName()==當前分組的Field的名稱
					 * value.getNGroups()==當前分組的Field的總的組數
					 */
					System.out.println(value.getName() + "=======" + value.getNGroups());
					List<Group> groups = value.getValues();
					if(groups != null) {
						for(Group group : groups) {
							System.out.println(group.getGroupValue());
							System.out.println("=======");
							
							/**
							 * group.getResult()表示當前組的數據記錄數,如果上面配置 GroupParams.GROUP_LIMIT==0,則爲null
							 */
							SolrDocumentList result = group.getResult();
							System.out.println("resultNumFound=====" + result.getNumFound());
							if(result != null) {
								for(SolrDocument item : result) {
									/**
									 * item就是solr中每一個索引的數據
									 */
									System.out.println(item);
								}
							}
						}
					}
				}
			}
		}
	}
	public static void main(String[] args) throws Exception {
		queryGroup();
	}
}

運行結果如下:

values.size()======1
brand_s=======4
長虹
=======
resultNumFound=====1
SolrDocument{id=1005, title_s=Changhong/長虹 65A4U 65英寸電視機4K智能網絡平板液晶屏LED彩電, subMajor_s=電視, brand_s=長虹}
創維
=======
resultNumFound=====2
SolrDocument{id=1004, title_s=創維5T 65英寸4K全面屏電視機智能網絡wifi平板液晶屏家用彩電 55, subMajor_s=電視, brand_s=創維}
SolrDocument{id=1002, title_s=創維55M1 55英寸4K高清洗衣機, subMajor_s=洗衣機, brand_s=創維}
海信
=======
resultNumFound=====1
SolrDocument{id=1003, title_s=Hisense/海信HZ55E3D-PRO 空調, subMajor_s=空調, brand_s=海信}
先科
=======
resultNumFound=====1
SolrDocument{id=1001, title_s=SAST/先科 32英寸液晶空調, subMajor_s=空調, brand_s=先科}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章