一个分享个人学习、开发经验的Blog,http://www.joyphper.net

一个基于solr搜索结果的分页插件

posted @ 2013-06-18 14:48 | 阅读:4936 | 评论:1 | 分类: Java

 一个基本solr搜索结果的分页插件

下载地址:upload/201306/2013061814465278.zip

使用方法:
 

1、将solr-pager-component-1.0.0.jar复制到webapps/solr/WEB-INF/lib目录下

2、在solrconfig.xml中加入下面代码

<searchComponent name="pager" class="net.joyphper.solr.handler.component.PagerComponent"/>
<requestHandler name="standard" class="solr.SearchHandler" default="true">
	<arr name="last-components">
		<str>pager</str>
	</arr>
</requestHandler>

 3、在访问参数中加上pager=1,如下:

http://127.0.0.1:8983/solr/select/?q=*:*&version=2.2&start=0&rows=10&indent=on&pager=1

在通过程度获取搜索结果的方式使用 

SolrQuery query = new SolrQuery();
query.setQuery("*:*");
//启用分页返回 query.setParam("pager", "1");

 源码如下:

package net.joyphper.solr.pager.component;

import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;

import org.apache.solr.common.params.CommonParams;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.common.util.SimpleOrderedMap;
import org.apache.solr.handler.component.ResponseBuilder;
import org.apache.solr.handler.component.SearchComponent;

public class PagerComponent extends SearchComponent {
	public static final String PARAM_PAGER = "pager";

	@Override
	public void prepare(ResponseBuilder rb) throws IOException {
		/* notify solr we always need resultset ! */
		if (rb.req.getParams().getInt(PARAM_PAGER, 0) != 0)
			rb.setNeedDocSet(true);
	}

	@Override
	public void process(ResponseBuilder rb) throws IOException {
		/* get request params */
		SolrParams par = rb.req.getParams();
		int rows = par.getInt(CommonParams.ROWS, 0);
		int start = par.getInt(CommonParams.START, 0);

		/* neet to work ? */
		if (rows == 0 || rb == null || rb.getResults() == null)
			return;

		if (rb.getResults().docSet == null)
			return;

		int total_count = rb.getResults().docSet.size();

		/* paging pages */
		int total_page = (total_count % rows == 0) ? (total_count / rows)
				: (total_count / rows + 1);

		int current_page = (start / rows)+1; // 当前页
		int pre_page = current_page - 1 > 0 ? current_page - 1 : current_page; // 前一页

		int next_page = current_page + 1 > total_page ? total_page
				: current_page + 1;

		int last_page = total_page;

		int for_start=0;
		int for_end = 0;
		if (total_page > 11) {
			if (current_page < 6) {
				for_start = 1;
				for_end = 11;
			} else if (current_page + 5 > total_page) {
				for_start = total_page - 10;
				for_end = total_page;
			} else {
				for_start = current_page - 5;
				for_end = current_page + 5;
			}
		} else {
			for_start = 1;
			for_end = total_page;
		}
		/* pager list */
		NamedList<Object> lst = new SimpleOrderedMap<Object>();
		Map<String,Integer> lst_pages = new LinkedHashMap<String,Integer>();
		for (int i = for_start; i <= for_end; i++) {
			lst_pages.put(String.valueOf(i) , (i - 1) * rows);
		}
		lst.add("pages", lst_pages);
		
		lst.add("pre", 	(pre_page	- 1) 	* rows);
		lst.add("first", 0 		* rows);
		lst.add("next", (next_page 	- 1)	* rows);
		lst.add("last", (last_page 	- 1) 	* rows);
		lst.add("current", current_page);
		lst.add("count", total_count);
		lst.add("rows", rows);

		/* finish */
		rb.rsp.add("pager", lst);
	}

	@Override
	public String getDescription() {
		return "基于solr的一人颁布插件";
	}

	@Override
	public String getSource() {
		return "http://www.joyphper.net";
	}

	public String getSourceId() {
		return null;
	}

	@Override
	public String getVersion() {
		return "1.0.0";
	}
}

 

TAG: solr , solr分页

共有1条评论 发表评论>>

萧兮 发表于:2015-06-08 08:54
楼主,你的界面是咋调的?
点击换一张验证码