分页查询:
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());
}
}
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/vtopqx/article/details/73230063
内容来源于网络,如有侵权,请联系作者删除!