java 操作solr

public class SolrCloudTest {

	@Test
	public void testAddDocument() throws Exception {
		// 创建单机版连接
//		HttpSolrServer solrServer = new HttpSolrServer("http://192.168.1.104:8080/solr");
//		// 创建一个文档对象
//		SolrInputDocument document = new SolrInputDocument();
//		// 向文档中添加域
//		document.addField("id", "test");
//		document.addField("item_title", "测试");
//		// 把文档添加到索引库
//		solrServer.add(document);
//		// 提交
//		solrServer.commit();

		// 创建一个和solr集群的连接
		// 参数就是zookeeper的地址列表,使用逗号分隔
		String zkHost = "192.168.1.103:2181,192.168.1.103:2182,192.168.1.103:2183";

		CloudSolrServer solrServer = new CloudSolrServer(zkHost);
		// 设置默认的collection
		solrServer.setDefaultCollection("collection2");
		// 创建一个文档对象
		SolrInputDocument document = new SolrInputDocument();
		// 向文档中添加域
		document.addField("id", "test");
		document.addField("item_title", "测试");
		// 把文档添加到索引库
		solrServer.add(document);
		// 提交
		solrServer.commit();
	}

	@Test
	public void deleteAddDocument() throws Exception {
		// 创建一个和solr集群的连接
		// 参数就是zookeeper的地址列表,使用逗号分隔
		String zkHost = "192.168.1.103:2181,192.168.1.103:2182,192.168.1.103:2183";
		CloudSolrServer solrServer = new CloudSolrServer(zkHost);
		// 设置默认的collection
		solrServer.setDefaultCollection("collection2");

		solrServer.deleteByQuery("*:*");
		// 提交
		solrServer.commit();
	}

spring+solr

spring-solr.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

	
	<!-- 配置SolrServer对象 -->
	<!--  单机版-->
	<!-- <bean id="httpSolrServer" class="org.apache.solr.client.solrj.impl.HttpSolrServer">
	   <constructor-arg name="baseURL" value="${SOLR.SERVER.URL}"></constructor-arg>
	</bean> -->
	<!-- 集群版 -->
	<bean id="cloudSolrServer" class="org.apache.solr.client.solrj.impl.CloudSolrServer">
	   <constructor-arg name="zkHost" value="192.168.1.103:2181,192.168.1.103:2182,192.168.1.103:2183"></constructor-arg>
	   <property name="defaultCollection" value="collection2"></property>
	</bean>
</beans>


Dao

public interface SearchDao {
  SearchResult search(SolrQuery query)throws Exception;
}

@Repository
public class SearchDaoImpl implements SearchDao{

	@Autowired
	private SolrServer solrServer;
	@Override
	public SearchResult search(SolrQuery query) throws Exception {
		//返回值对象
		SearchResult result = new SearchResult();
		//根据查询条件查询索库
		QueryResponse response = solrServer.query(query);
		//取出查询结果
		SolrDocumentList solrDocumentList = response.getResults();
		//取查询总数量
		result.setRecordCount(solrDocumentList.getNumFound());
		//商品列表
		List<Item> itemList = new ArrayList<>();
		//取高亮显示
		Map<String, Map<String, List<String>>> highlighting = response.getHighlighting();
		
		//取商品列表
		for (SolrDocument solrDocument : solrDocumentList) {
		   //创建一个商品对象
			Item item = new Item();
			item.setId((String)solrDocument.get("id"));
			//取高亮显示的结果
			List<String> list = highlighting.get(solrDocument.get("id")).get("item_title");
			String title ="";
			if(list!=null && list.size()>0){
				title=list.get(0);
			}else{
				title =(String)solrDocument.get("item_title");
			}
			item.setTitle(title);
			item.setImage((String) solrDocument.get("item_image"));
			item.setPrice((long) solrDocument.get("item_price"));
			item.setSell_point((String) solrDocument.get("item_sell_point"));
			item.setCategory_name((String) solrDocument.get("item_category_name"));
			//添加的商品列表
			itemList.add(item);
		}
		result.setItemList(itemList);
		return result;
	}

}

service

public interface SearchService {
	SearchResult search(String queryString,int page,int rows)throws Exception;
}

@Service
public class SearchServiceImpl implements SearchService{

	@Autowired
	private SearchDao searchDao;
	@Override
	public SearchResult search(String queryString, int page, int rows) throws Exception {
		//创建查询对象
		SolrQuery query = new SolrQuery();
		//设置查询条件
		query.setQuery(queryString);
		//设置分页
		query.setStart((page-1)*rows);
		query.setRows(rows);
		//设置默认搜索域
		query.set("df", "item_keywords");
		//设置高亮显示
		query.setHighlight(true);
		query.addHighlightField("item_title");
		query.setHighlightSimplePre("<em style=\"color:red\">");
		query.setHighlightSimplePost("</em>");

		//执行查询
		SearchResult searchResult = searchDao.search(query);
		//计算查询结果总页数
		long recordCount = searchResult.getRecordCount();
		long pageCount = recordCount/rows;
		if(recordCount%rows>0){
			pageCount++;
		}
		searchResult.setPageCount(pageCount);
		searchResult.setCurPage(page);
		return searchResult;
	}

}


public class SearchResult {
    //商品列表
	private List<Item> itemList;
	//总记录
	private  long  recordCount;
	//总页数
	private long pageCount;
	//当前页
	private long curPage;
	public List<Item> getItemList() {
		return itemList;
	}
	public void setItemList(List<Item> itemList) {
		this.itemList = itemList;
	}
	public long getRecordCount() {
		return recordCount;
	}
	public void setRecordCount(long recordCount) {
		this.recordCount = recordCount;
	}
	public long getPageCount() {
		return pageCount;
	}
	public void setPageCount(long pageCount) {
		this.pageCount = pageCount;
	}
	public long getCurPage() {
		return curPage;
	}
	public void setCurPage(long curPage) {
		this.curPage = curPage;
	}
	
	
}



@Autowired
	private SolrServer solrServer;

	@Override
	public Result importAllItems() {
		try {
			// 查询商品列表
			List<Item> itemList = itemMapper.getItemList();
			// 把商品信息写入索引库
			for (Item item : itemList) {
				// 创建一个SolrInputDocument对象
				SolrInputDocument document = new SolrInputDocument();
				document.setField("id", item.getId());
				document.setField("item_title", item.getTitle());
				document.setField("item_sell_point", item.getSell_point());
				document.setField("item_price", item.getPrice());
				document.setField("item_image", item.getImage());
				document.setField("item_category_name", item.getCategory_name());
				document.setField("item_desc", item.getItem_des());
				// 写入索引库

				solrServer.add(document);
			}
			//提交修改
			solrServer.commit();
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return TaotaoResult.build(500, ExceptionUtil.getStackTrace(e));
		}
		return Result.ok();
	}


 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值