solr-jd
springMVC.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd"> <!-- 配置扫描 器 --> <context:component-scan base-package="com.itheima.jd"/> <!-- 配置处理器映射器 适配器 --> <mvc:annotation-driven/> <!-- 配置视图解释器 jsp --> <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> <!-- 配置SolrServer对象 --> <bean id="solrServer" class="org.apache.solr.client.solrj.impl.HttpSolrServer"> <constructor-arg index="0" value="http://localhost:8080/solr/collection1"/> </bean> </beans>
SearchDaoImpl:
@Repository public class SearchDaoImpl implements SearchDao { @Autowired private SolrServer solrServer; @Override public SearchResult search(SolrQuery query) throws Exception { // 1)根据Query对象进行查询 QueryResponse response = solrServer.query(query); // 2)得到查询结果 SolrDocumentList solrDocumentList = response.getResults(); // 3)取查询结果的总记录数 long numFound = solrDocumentList.getNumFound(); // 4)取商品列表,包含高亮的结果 List<Product> products = new ArrayList<>(); //取高亮显示的结果 Map<String, Map<String, List<String>>> highlighting = response.getHighlighting(); for (SolrDocument solrDocument : solrDocumentList) { //创建一个Product对象 Product product = new Product(); product.setPid((String) solrDocument.get("id")); product.setCatalog_name((String) solrDocument.get("product_catalog_name")); //取高亮结果 List<String> list = highlighting.get(solrDocument.get("id")).get("product_name"); String productName = ""; if (list != null && list.size() > 0) { productName = list.get(0); } else { productName = (String) solrDocument.get("product_name"); } product.setName(productName); product.setPicture((String) solrDocument.get("product_picture")); product.setPrice((float) solrDocument.get("product_price")); //添加到商品列表 products.add(product); } // 5)把结果封装到SearchResult对象中 SearchResult result = new SearchResult(); result.setRecordCount(numFound); result.setProductList(products); // 6)返回结果 return result; } }
SearchServiceImpl:
@Service public class SearchServiceImpl implements SearchService { @Autowired private SearchDao searchDao; private static final int ROWS = 60; @Override public SearchResult search(String queryString, String catalog_name, String price, int sort, int page) throws Exception { // 1、接收Controller传递过来的参数 // 2、创建一个SolrQuery对象 SolrQuery query = new SolrQuery(); // 3、根据参数设置查询条件。。。。 if (queryString != null && !"".equals(queryString)) { query.setQuery(queryString); } else { //显示所有商品 query.setQuery("*:*"); } //分类名称过滤条件 if (catalog_name != null && !"".equals(catalog_name)) { query.addFilterQuery("product_catalog_name:" + catalog_name); } //价格区间过滤 if (price != null && !"".equals(price)) { //取价格区间 String[] strings = price.split("-"); query.addFilterQuery("product_price:["+strings[0]+" TO "+strings[1]+"]"); } //排序条件 //0:升序 1:降序 默认是升序 if (sort != 1) { query.setSort("product_price", ORDER.asc); } else { query.setSort("product_price", ORDER.desc); } //设置分页信息 query.setStart((page -1) * ROWS); query.setRows(ROWS); //设置默认搜索域 query.set("df", "product_keywords"); //开启高亮显示 query.setHighlight(true); query.addHighlightField("product_name"); query.setHighlightSimplePre("<em style=\"color:red\">"); query.setHighlightSimplePost("</em>"); // 4、调用dao执行查询 SearchResult searchResult = searchDao.search(query); // 5、取查询结果 long recordCount = searchResult.getRecordCount(); // 6、计算查询结果的总页数 long pageCount = recordCount / ROWS; if (recordCount % ROWS > 0) { pageCount++; } searchResult.setPageCount(pageCount); // 7、返回结果 return searchResult; } }
SearchController:
@Controller public class SearchController { @Autowired private SearchService searchService; @RequestMapping("list") public String search(String queryString, String catalog_name, String price, @RequestParam(defaultValue="0")int sort, @RequestParam(defaultValue="1")int page, Model model) throws Exception { //调用Service查询商品列表 SearchResult searchResult = searchService.search(queryString, catalog_name, price, sort, page); //把结果传递给jsp model.addAttribute("result", searchResult); //查询参数回显 model.addAttribute("queryString", queryString); model.addAttribute("catalog_name", catalog_name); model.addAttribute("price", price); model.addAttribute("sort", sort); model.addAttribute("page", page); //返回逻辑视图 return "product_list"; } }