- SolrUtil
SolrUtil 增加分页查询的方法public static QueryResponse query(String keywords,int startOfPage, int numberOfPage) throws SolrServerException, IOException { SolrQuery query = new SolrQuery(); query.setStart(startOfPage); query.setRows(numberOfPage); query.setQuery(keywords); QueryResponse rsp = client.query(query); return rsp; }
public class SolrUtil { public static SolrClient client; private static String url; static { url = "http://localhost:8983/solr/how2java"; client = new HttpSolrClient.Builder(url).build(); } public static <T> boolean batchSaveOrUpdate(List<T> entities) throws SolrServerException, IOException { DocumentObjectBinder binder = new DocumentObjectBinder(); int total = entities.size(); int count=0; for (T t : entities) { SolrInputDocument doc = binder.toSolrInputDocument(t); client.add(doc); System.out.printf("添加数据到索引中,总共要添加 %d 条记录,当前添加第%d条 %n",total,++count); } client.commit(); return true; } public static QueryResponse query(String keywords,int startOfPage, int numberOfPage) throws SolrServerException, IOException { SolrQuery query = new SolrQuery(); query.setStart(startOfPage); query.setRows(numberOfPage); query.setQuery(keywords); QueryResponse rsp = client.query(query); return rsp; } }
- TestSolr4j
拿到分页查询的结果,遍历出来public class TestSolr4j { public static void main(String[] args) throws SolrServerException, IOException { //查询 QueryResponse queryResponse = SolrUtil.query("name:手机",0,10); SolrDocumentList documents= queryResponse.getResults(); System.out.println("累计找到的条数:"+documents.getNumFound()); if(!documents.isEmpty()){ Collection<String> fieldNames = documents.get(0).getFieldNames(); for (String fieldName : fieldNames) { System.out.print(fieldName+"\t"); } System.out.println(); } for (SolrDocument solrDocument : documents) { Collection<String> fieldNames= solrDocument.getFieldNames(); for (String fieldName : fieldNames) { System.out.print(solrDocument.get(fieldName)+"\t"); } System.out.println(); } } }
Solr-SolrJ 分页查询
最新推荐文章于 2023-04-20 13:46:42 发布