搜索功能的实现
效果图
模块划分
需要的我们自己写dao层mapper层
dao层存在我们搜索的结果
/** * 商品搜索dao */ @Repository public class SearchDao { @Autowired private SolrServer solrServer; /** * 根据查询条件查询索引库 * @param solrQuery * @return */ public SearchResult search(SolrQuery solrQuery) throws SolrServerException { //根据solrQuery查询索引库 QueryResponse query = solrServer.query(solrQuery); //取查询结果 SolrDocumentList results = query.getResults(); //取查询结果总记录数 long numFound = results.getNumFound(); //创建一个SearchResullt对象 SearchResult searchResult=new SearchResult(); searchResult.setRecordCount(numFound); //取商品列表,需要取高亮的显示 Map, Map , List >> highlighting = query.getHighlighting(); //创建一个存储商品列表的集合 List itemList =new ArrayList<>(); //遍历文档列表,从域中去内容取高亮中的需要的字段id必须要有 for (SolrDocument document :results) { //创建一个SearchItem对象 SearchItem searchItem=new SearchItem(); //设置需要SearchItem对象的属性 searchItem.setId((String) document.get("id")); searchItem.setCategory_name((String) document.get("item_category_name")); searchItem.setImage((String) document.get("item_image")); searchItem.setPrice((Long) document.get("item_price")); searchItem.setSell_point((String) document.get("item_sell_point")); //取高亮显示 List list = highlighting.get(document.get("id")).get("item_title"); //创建一个title空字符串 String title=""; //判断title数据中是否有高度数据 if (list !=null && list.size()>0){//有高亮数据 title=list.get(0); }else {//没有高亮数据就取文档中的数据 title= (String) document.get("item_title"); } //将标题添加到searchItem对象中 searchItem.setTitle(title); //添加到商品列表 itemList.add(searchItem); } //添加商品列表到SearchResullt对象 searchResult.setItemList(itemList); //返回结果 return searchResult; } }
Itemmapper.xml配置
xml version="1.0" encoding="UTF-8" ?> mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >namespace="com.e3mall.search.mapper.ItemMapper">
依照我红色的字体写对应ItemMapper接口类即可附图依照
SearchItemServiceImpl.java
/** * 商品数据索引库Service */ @Service public class SearchItemServiceImpl implements SearchItemService { @Autowired private ItemMapper itemMapper; @Autowired private SolrServer solrServer; /** * 将删商品数据导入索引库 * @return */ @Override public E3Result importItems() { try { //查询商品列表 ListitemList = itemMapper.getItemList(); //导入到索引库 for (SearchItem item :itemList) { //创建文档对象 SolrInputDocument document=new SolrInputDocument(); //向文档添加域 document.addField("id",item.getId()); document.addField("item_title",item.getTitle()); document.addField("item_sell_point",item.getSell_point()); document.addField("item_price",item.getPrice()); document.addField("item_image",item.getImage()); document.addField("item_category_name",item.getCategory_name()); //写入索引库 solrServer.add(document); } //提交 solrServer.commit(); //返回成功 return E3Result.ok(); }catch (Exception e){ e.printStackTrace(); return E3Result.build(500,"商品导入失败!"); } } }
applicationContent-service.xml配置
代码
<dubbo:service interface="com.e3mall.search.service.SearchItemService" ref="searchItemServiceImpl" timeout="600000"/>
表现层
/** * 商品搜索Controller */ @Controller public class SearchController { @Autowired private SearchService searchService; @Value("${SEACHER_RESULT_ROWS}") private Integer SEACHER_RESULT_ROWS; /** * 分页查询功能 * @param keyword 查询添加 * @param page 结果从第几条记录开始显示这里我们设置了默认值1 * @param model * @return */ @RequestMapping("/search") public String search(String keyword, @RequestParam(defaultValue = "1") Integer page, Model model) throws Exception { keyword=new String(keyword.getBytes("ISO-8859-1"),"utf-8"); //调用服务查询商品列表 SearchResult result = searchService.search(keyword, page, SEACHER_RESULT_ROWS); //把结果传递给页面 model.addAttribute("query",keyword); model.addAttribute("totalPages",result.getTotalPages()); model.addAttribute("page",page); model.addAttribute("recourdCount",result.getRecordCount()); model.addAttribute("itemList",result.getItemList()); //返回逻辑页面 return "search"; } }
springmvc.xml引入服务
<dubbo:application name="e3-manager-web"/> <dubbo:registry protocol="zookeeper" address="192.168.25.128:2181"/> <dubbo:reference interface="com.e3mall.search.service.SearchService" id="searchService" />