solr4j基本使用2

SolrBean的使用和多核查询

import org.apache.solr.client.solrj.beans.Field;


public class SearchGoodsVo {
	/**
	 * ID
	 */
	@Field
	private int id;

	/**
	 * 条码
	 */
	@Field
	private String code;

 

import java.util.List;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrQuery.ORDER;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:spring/mall-context.xml"})
public class SolrBeanTest {
	
	String solrServerUrl="http://192.168.0.210:8080/solr";//solr连接地址
	String solrCore="goods_core";//core名称
	String solrCore2="goods2_core";//core名称
	
	private HttpSolrClient httpSolrClient;//solr实例对象
	
	/**
	 * 获取HttpSolrClient实例对象
	 * @return
	 */
	private HttpSolrClient getInstance() {
		if(httpSolrClient==null) {
			httpSolrClient=new HttpSolrClient(solrServerUrl+"/"+solrCore);
			httpSolrClient.setDefaultMaxConnectionsPerHost(1000);//每台机器的最大连接数
			httpSolrClient.setMaxTotalConnections(10000);//最大连接数
			httpSolrClient.setConnectionTimeout(60000);//设置连接超时时间(单位毫秒) 1000
			httpSolrClient.setSoTimeout(60000); 设置读数据超时时间(单位毫秒) 1000
			httpSolrClient.setFollowRedirects(false);//跟随重定向
			httpSolrClient.setAllowCompression(true);//允许压缩
		}
		return httpSolrClient;
	}
	
	@Test
	/**
	 * 添加(更新)单条数据
	 * @throws Exception
	 */
	public void saveOrUpdateSolrBean() throws Exception {
		//1、获取HttpSolrClient对象
		getInstance();
		//2、创建一个SolrBean对象。
		SearchGoodsVo goodsVo=new SearchGoodsVo();
		goodsVo.setId(1);
		goodsVo.setCode("69445557878");
		goodsVo.setName("测试商品");
		//3、把SolrBean添加到索引库中。
		httpSolrClient.addBean(goodsVo);
		//4、提交。
		httpSolrClient.commit();
	}
	
	@Test
	/**
	 * 查询数据
	 * @throws Exception
	 */
	public void querySolrBean() throws Exception {
		//1、获取HttpSolrClient对象
		getInstance();
		//2、创建一个SolrQuery对象。
		SolrQuery query = new SolrQuery();
		//3、向SolrQuery中添加查询条件、过滤条件。。。【查询字段和搜索词一起作为参数,以冒号作为分隔:filed:value】
		query.setQuery("*:*");//查询所有
		query.setStart(0);//页码从0开始,即第一页
		query.setRows(20);//每页显示记录数
		query.setSort("id", ORDER.desc);//按照id倒序排列,默认schema.xml中的id为string类型,需要修改为int排序才有效,有意义
		//4、执行查询。得到一个Response对象。
		QueryResponse response = httpSolrClient.query(query);
		//5、取查询结果,以Bean的形式
		List<SearchGoodsVo> goodsList = response.getBeans(SearchGoodsVo.class);
		System.out.println("查询结果的总记录数:" + goodsList.size());
		//6、遍历结果并打印。
		for (SearchGoodsVo goods : goodsList) {
			System.out.println(goods.getId()+"||"+goods.getCode()+"||"+goods.getName());
		}
	}
	
	@Test
	/**
	 * 多核查询(goods_core和goods2_core,相当于两张一模一样结构的表做union)
	 * @throws Exception
	 */
	public void queryMulticore() throws Exception {
		//1、获取HttpSolrClient对象
		getInstance();
		//2、创建一个SolrQuery对象。
		SolrQuery query = new SolrQuery();
		//3、向SolrQuery中添加查询条件、过滤条件。。。【查询字段和搜索词一起作为参数,以冒号作为分隔:filed:value】
		query.setQuery("*:*");//查询所有
		query.setStart(0);//页码从0开始,即第一页
		query.setRows(20);//每页显示记录数
		query.setSort("id", ORDER.desc);//按照id倒序排列,默认schema.xml中的id为string类型,需要修改为int排序才有效,有意义
		query.set("shards", solrServerUrl+"/"+solrCore+","+solrServerUrl+"/"+solrCore2);//多核查询:多个url用逗号分隔
		//4、执行查询。得到一个Response对象。
		QueryResponse response = httpSolrClient.query(query);
		//5、取查询结果,以Bean的形式
		List<SearchGoodsVo> goodsList = response.getBeans(SearchGoodsVo.class);
		System.out.println("查询结果的总记录数:" + goodsList.size());
		//6、遍历结果并打印。
		for (SearchGoodsVo goods : goodsList) {
			System.out.println(goods.getId()+"||"+goods.getCode()+"||"+goods.getName());
		}
	}

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值