(二)solr搜索服务的使用

一.与spring整合--配置文件

    <!-- solr单机版 -->
<bean id="httpSolrServer" class="org.apache.solr.client.solrj.impl.HttpSolrServer">
<constructor-arg name="baseURL" value="http://192.168.25.154:8080/solr"/>
</bean>

<!-- solr集群 -->
<!-- <bean id="cloudSolrServer" class="org.apache.solr.client.solrj.impl.CloudSolrServer">
<constructor-arg name="zkHost" value="192.168.25.154:2181,192.168.25.154:2182,192.168.25.154:2183"/>
<property name="defaultCollection" value="collection2"/>

</bean> -->


二 业务层的实现

  

/**
 * 商品搜索服务实现类
 * <p>Title: SearchServiceImpl</p>
 * <p>Description: </p>
 * @version 1.0
 */
@Service
public class SearchServiceImpl implements SearchService {


@Autowired

private ItemSearchDao itemSearchDao;

商品搜索服务实现类
@Override
public Result search(String queryString, int page, int rows) throws Exception {
// 1、创建一个SolrQuery对象。
SolrQuery query  = new SolrQuery();
// 2、设置查询条件
query.setQuery(queryString);
// 3、设置分页条件
if (page < 1) page = 1;
query.setStart((page -1) * rows);
query.setRows(rows);
// 4、需要指定默认搜索域。
query.set("df", "item_title");
// 5、设置高亮
query.setHighlight(true);
query.addHighlightField("item_title");
query.setHighlightSimplePre("<em style=\"color:red\">");
query.setHighlightSimplePost("</em>");
// 6、执行查询,调用SearchDao。得到SearchResult
Result result = itemSearchDao.search(query);
// 7、需要计算总页数。
long recordCount = searchResult.getRecordCount();
long pageCount = recordCount / rows;
if (recordCount % rows > 0) {
pageCount++;
}
result.setPageCount(pageCount);
// 返回SearchResult
return result;
}

}

//导入商品数据到索引库--方法
@Override
public Result importAllItemToIndex() throws Exception{
// 1、查询所有商品数据。
List<SearchItem> itemList = itemMapper.getItemList();
// 2、创建一个SolrServer对象。
for (SearchItem searchItem : itemList) {
// 3、为每个商品创建一个SolrInputDocument对象。
SolrInputDocument document = new SolrInputDocument();
// 4、为文档添加域
document.addField("id", searchItem.getId());
document.addField("item_title", searchItem.getTitle());
document.addField("item_sell_point", searchItem.getSell_point());
document.addField("item_price", searchItem.getPrice());
document.addField("item_image", searchItem.getImage());
document.addField("item_category_name", searchItem.getCategory_name());
document.addField("item_desc", searchItem.getItem_des());
// 5、向索引库中添加文档。
solrServer.add(document);
}
//提交
solrServer.commit();
// 6、返回TaotaoResult。
return Result.ok();
}

三,数据库持久化层

/**
 * 商品搜索dao
 * <p>Title: ItemSearchDao</p>
 * <p>Description: </p> 
 * @version 1.0
 */
@Repository
public class ItemSearchDao {
@Autowired
private SolrServer solrServer;

public SearchResult search(SolrQuery query) throws Exception {
//根据Query对象查询索引库
QueryResponse response = solrServer.query(query);
//取查询结果
SolrDocumentList solrDocumentList = response.getResults();
SearchResult result = new SearchResult();
//取查询结果总记录数
result.setRecordCount(solrDocumentList.getNumFound());
//取结果集
List<SearchItem> itemList = new ArrayList<>();
for (SolrDocument solrDocument : solrDocumentList) {
SearchItem searchItem = new SearchItem();
searchItem.setCategory_name((String) solrDocument.get("item_category_name"));
searchItem.setId(Long.parseLong(solrDocument.get("id").toString()));
searchItem.setImage((String) solrDocument.get("item_image"));
searchItem.setPrice((Long) solrDocument.get("item_price"));
//取高亮显示
String itemTitle = null;
Map<String, Map<String, List<String>>> highlighting = response.getHighlighting();
List<String> list = highlighting.get(solrDocument.get("id")).get("item_title");
if (list != null && list.size() > 0) {
itemTitle = list.get(0);
} else {
itemTitle = (String) solrDocument.get("item_title");
}

searchItem.setTitle(itemTitle);
//添加到商品列表
itemList.add(searchItem);
}
result.setItemList(itemList);
//返回结果
return result;
}

}


四。activemq进行同步到搜索服务

public class ItemAddListener implements MessageListener{


@Autowired
private ItemMapper itemMapper;
@Autowired
private SolrServer solrServer;


@Override
public void onMessage(Message message) {
// 从消息中取商品id
try {
TextMessage textMessage = (TextMessage) message;
String strItemId = textMessage.getText();
//转换成Long
Long itemId = new Long(strItemId);
//根据商品Id查询商品消息
SearchItem searchItem = itemMapper.getItemById(itemId);
//把商品消息添加到索引库
SolrInputDocument document = new SolrInputDocument();
// 4、为文档添加域
document.addField("id", searchItem.getId());
document.addField("item_title", searchItem.getTitle());
document.addField("item_sell_point", searchItem.getSell_point());
document.addField("item_price", searchItem.getPrice());
document.addField("item_image", searchItem.getImage());
document.addField("item_category_name", searchItem.getCategory_name());
document.addField("item_desc", searchItem.getItem_des());
// 5、向索引库中添加文档。
solrServer.add(document);
//提交
solrServer.commit();

} catch (Exception e) {
e.printStackTrace();
}

}


}


猜你喜欢

转载自blog.csdn.net/weixin_39494923/article/details/79482984