Solr之分页查询-yellowcong

分页查询的时候,只对添加start(哪儿开始)和rows(多少行)两个参数即可,通过设定SolrQuery.setStart 和SolrQuery.setRows ,来进行分页。分页操作在实际开发中,用得还是比较的多的。

分页查询

/**
 * 
 * 作者:yellowcong <br/>
 * 日期:2017/12/05 <br/>
 * 時間:15:47:17 <br/>
 * 描述:分页查询数据
 * @param pageNow  当前页
 * @param pageSize  页面大小
 * @throws Exception
 */
public static void queryByPage(int pageNow,int pageSize) throws Exception {
    SolrServer server = new HttpSolrServer(SOLR_PATH);

    SolrQuery solrQuery = new SolrQuery();


    int start = (pageNow-1) * pageSize;
    int end = pageNow*pageSize;

    System.out.println("开始"+start);
    System.out.println("结束"+end);

    //添加查询
    solrQuery.add("q","*:*");
    //开始
    solrQuery.setStart(start);

    //该参数就是控制条数
    //设定为Integer最大值 
    solrQuery.setRows(end);


    //查询数据 ,默认最大是10
    QueryResponse response = server.query(solrQuery);

    SolrDocumentList docs = response.getResults();
    for(SolrDocument doc :docs) {
        String id = doc.get("id").toString();
        String title = doc.get("title").toString();
        String content = doc.get("content").toString();
        System.out.printf("%s:%s:%s\r\n",id,title,content);
    }
}

运行结果

这里写图片描述

获取所有的数据

通过设定SolrQuery.setStart 和SolrQuery.setRows ,来进行分页,我们将SolrQuery.setRows 设定为Integer.MAX_VALUE ,就取消了默认只能查10条数据的问题了

/**
 * 检索到所有的数据
 * 作者:yellowcong <br/>
 * 日期:2017/12/05 <br/>
 * 時間:15:40:58 <br/>
 * 描述:
 */
public static void getAllData() throws Exception {
    SolrServer server = new HttpSolrServer(SOLR_PATH);

    SolrQuery solrQuery = new SolrQuery();

    //添加查询
    solrQuery.add("q","*:*");
    //开始
    solrQuery.setStart(0);
    //solrQuery.set("start", 0);

    //该参数就是控制条数
    //设定为Integer最大值 
    solrQuery.setRows(Integer.MAX_VALUE);
    //solrQuery.set("rows", Integer.MAX_VALUE);


    //查询数据 ,默认最大是10
    QueryResponse response = server.query(solrQuery);

    SolrDocumentList docs = response.getResults();
    for(SolrDocument doc :docs) {
        String id = doc.get("id").toString();
        String title = doc.get("title").toString();
        String content = doc.get("content").toString();
        System.out.printf("%s:%s:%s\r\n",id,title,content);
    }
}

运行结果

这里写图片描述

完整代码

package com.yellowcong.day12_05;

import java.util.ArrayList;
import java.util.List;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
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;

/**
 *
 * 作者:yellowcong <br/>
 * 日期:2017/12/05 <br/>
 * 時間:15:28:10 <br/>
 * 描述:
 */
public class Demo2 {
    // solr的服务器地址
    private static final String SOLR_PATH = "http://192.168.66.100:8080/solr";

    public static void main(String[] args) throws Exception {

        //清除索引
        deleteAll();

        //建立索引
        index();

        queryByPage(1,20);
    }
    /**
     * 
     * 作者:yellowcong <br/>
     * 日期:2017/12/05 <br/>
     * 時間:15:47:17 <br/>
     * 描述:分页查询数据
     * @param pageNow  当前页
     * @param pageSize  页面大小
     * @throws Exception
     */
    public static void queryByPage(int pageNow,int pageSize) throws Exception {
        SolrServer server = new HttpSolrServer(SOLR_PATH);

        SolrQuery solrQuery = new SolrQuery();


        int start = (pageNow-1) * pageSize;
        int end = pageNow*pageSize;

        System.out.println("开始"+start);
        System.out.println("结束"+end);

        //添加查询
        solrQuery.add("q","*:*");
        //开始
        solrQuery.setStart(start);

        //该参数就是控制条数
        //设定为Integer最大值 
        solrQuery.setRows(end);


        //查询数据 ,默认最大是10
        QueryResponse response = server.query(solrQuery);

        SolrDocumentList docs = response.getResults();
        for(SolrDocument doc :docs) {
            String id = doc.get("id").toString();
            String title = doc.get("title").toString();
            String content = doc.get("content").toString();
            System.out.printf("%s:%s:%s\r\n",id,title,content);
        }
    }

    /**
     * 检索到所有的数据
     * 作者:yellowcong <br/>
     * 日期:2017/12/05 <br/>
     * 時間:15:40:58 <br/>
     * 描述:
     */
    public static void getAllData() throws Exception {
        SolrServer server = new HttpSolrServer(SOLR_PATH);

        SolrQuery solrQuery = new SolrQuery();

        //添加查询
        solrQuery.add("q","*:*");
        //开始
        solrQuery.setStart(0);
        //solrQuery.set("start", 0);

        //该参数就是控制条数
        //设定为Integer最大值 
        solrQuery.setRows(Integer.MAX_VALUE);
        //solrQuery.set("rows", Integer.MAX_VALUE);


        //查询数据 ,默认最大是10
        QueryResponse response = server.query(solrQuery);

        SolrDocumentList docs = response.getResults();
        for(SolrDocument doc :docs) {
            String id = doc.get("id").toString();
            String title = doc.get("title").toString();
            String content = doc.get("content").toString();
            System.out.printf("%s:%s:%s\r\n",id,title,content);
        }
    }
    /**
     * 作者:yellowcong <br/>
     * 日期:2017/12/05 <br/>
     * 時間:14:58:31 <br/>
     * 描述:添加索引
     * @throws Exception 
     * @throws SolrServerException 
     */
    public static void index() throws SolrServerException, Exception {

        SolrServer server = new HttpSolrServer(SOLR_PATH);

        List<SolrInputDocument> docs = new ArrayList<SolrInputDocument>();
        for(int i=1;i<=100;i++) {
            SolrInputDocument doc = new SolrInputDocument();
            doc.addField("id", Integer.toString(i));
            doc.addField("author", "张三"+i);
            doc.addField("title", "Solr"+i);
            doc.addField("content", "Solr是xx"+i);

            docs.add(doc);
        }
        //添加文档
        server.add(docs);

        //提交任务
        server.commit();
    }
    /**
     * 作者:yellowcong <br/>
     * 日期:2017/12/05 <br/>
     * 時間:14:48:59 <br/>
     * 描述:清空Solr里面的所有数据
     */
    public static void deleteAll() throws Exception {
        SolrServer server = new HttpSolrServer(SOLR_PATH);
        // 清空数据
        server.deleteByQuery("*:*");
        server.commit();
    }
}

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>yellowcong</groupId>
    <artifactId>day12_04</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>day12_04</name>
    <url>http://maven.apache.org</url>

    <!-- 配置国内比较快的 阿里云的Maven仓库 -->
    <repositories>
        <repository>
            <id>aliyunmaven</id>
            <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
        </repository>
    </repositories>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <lucene.version>4.5.1</lucene.version>
        <mmseg4j.version>1.9.1</mmseg4j.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
            <scope>test</scope>
        </dependency>

        <!-- lucene核心包 -->
        <dependency>
            <groupId>org.apache.lucene</groupId>
            <artifactId>lucene-core</artifactId>
            <version>${lucene.version}</version>
        </dependency>
        <!--QueryParser 查询类 -->
        <dependency>
            <groupId>org.apache.lucene</groupId>
            <artifactId>lucene-queryparser</artifactId>
            <version>${lucene.version}</version>
        </dependency>
        <!-- 分词器 -->
        <dependency>
            <groupId>org.apache.lucene</groupId>
            <artifactId>lucene-analyzers-common</artifactId>
            <version>${lucene.version}</version>
        </dependency>
        <!-- 高亮显示 -->
        <dependency>
            <groupId>org.apache.lucene</groupId>
            <artifactId>lucene-highlighter</artifactId>
            <version>${lucene.version}</version>
        </dependency>

        <!-- 庖丁解牛分词器 -->
        <dependency>
            <groupId>com.chenlb.mmseg4j</groupId>
            <artifactId>mmseg4j-core</artifactId>
            <version>${mmseg4j.version}</version>
        </dependency>
        <dependency>
            <groupId>com.chenlb.mmseg4j</groupId>
            <artifactId>mmseg4j-analysis</artifactId>
            <version>${mmseg4j.version}</version>
        </dependency>

        <!-- ikanalyzer 分词器 -->
        <dependency>
            <groupId>com.janeluo</groupId>
            <artifactId>ikanalyzer</artifactId>
            <version>2012_u6</version>
        </dependency>

        <!-- solrj -->
        <dependency>
            <groupId>org.apache.solr</groupId>
            <artifactId>solr-solrj</artifactId>
            <version>4.5.1</version>
        </dependency>

        <!-- 日志配置文件,必须有,不然会报错 -->
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.3</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.16</version>
        </dependency>
    </dependencies>
</project>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

狂飙的yellowcong

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值