Solr-SolrJ 分页查询

  1.  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;
        }
     
    }
  2. 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();
                 
            }
        }
     
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值