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());
}
}
}