六、SOLRJ 分页查询

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;
}
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package how2java;
import java.io.IOException;
import java.util.List;
 
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.beans.DocumentObjectBinder;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
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.SolrInputDocument;
import org.apache.solr.common.util.NamedList;
 
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

 
拿到分页查询的结果,遍历出来
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package how2java;
 
import java.io.IOException;
import java.util.Collection;
 
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
 
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();
             
        }
    }
 
}

 


先运行,看到效果,再学习

下载地址:solr4j.rar:http://download.how2j.cn/1691/solr4j.rar

posted on 2019-01-04 11:19  我是司  阅读(825)  评论(0)    收藏  举报

导航