solr分页查询

x33g5p2x  于2021-12-20 转载在 其他  
字(1.7k)|赞(0)|评价(0)|浏览(507)

分页查询:

package com.solorj.solrj_test;

import java.io.IOException;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocumentList;

/**
 * @ClassName: PageDemo 
 * @Description: Solr分页查询
 * @date 
 */
public class PageDemo {
	
	// 单点
	public static final String SOLR_URL = "http://localhost:8983/solr/solr_java";
	
	/**
	 * @Description: 查询显示指定行数数据
	 * @param rows
	 * @return
	 */
	public static SolrDocumentList queryRowsData(int rows){
		try {
			HttpSolrClient httpSolrClient = new HttpSolrClient.Builder(SOLR_URL).build();
			SolrQuery query = new SolrQuery();
			// 设定查询字段
			query.set("q", "id:*");
			// 设定返回记录数,默认为10条
			query.setRows(rows);
			QueryResponse response = httpSolrClient.query(query);
			SolrDocumentList list = response.getResults();
			return list;
		} catch (SolrServerException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * @Description: 分页查询
	 * @param start
	 * @param rows
	 * @return
	 */
	public static SolrDocumentList queryPageData(int start,int rows){
		try {
			HttpSolrClient httpSolrClient = new HttpSolrClient.Builder(SOLR_URL).build();
	        SolrQuery query = new SolrQuery();  
	        //设定查询字段  
	        query.set("q", "id:*");
	        //指定返回结果字段  
	        query.setIncludeScore(true);  
	        //分页开始页数  
	        query.setStart(start);  
	        //设定返回记录数,默认为10条  
	        query.setRows(rows);  
	        
	        QueryResponse response = httpSolrClient.query(query);  
	       //获取bean  
	       //  List<Object> bean = response.getBeans(Object.class);  
	        SolrDocumentList list = response.getResults();  
	        
	        return list;
	        
		} catch (Exception e) {
			e.printStackTrace();
		}
        return  null;  
	}
	

	public static void main(String[] args) {
		SolrDocumentList list1 = queryRowsData(2);
		SolrDocumentList list2 = queryPageData(0,5);
		System.out.println("list:"+list2.size());
	}
}

相关文章