分页查询的时候,只对添加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>