Solr-全文检索(Lucene的封装)
整合Dao层的编写
@Repositorypublic class ProductsDaoImp implements ProductsDao {@Autowiredprivate SolrServer solrServer;@Overridepublic List<Products> getQuery(String query, String catalog_name, String price, String sort) throws Exception { List<Products> returnlist = new ArrayList<>(); System.err.println(query + catalog_name + price + sort);// 查询SolrQuery solrQuery = new SolrQuery();// 条件查询 关键字// solrQuery.set("q", "*:*");//一种if (!"".equals(query) && query != null) { solrQuery.setQuery("product_name:" +query);// 二种}else{ solrQuery.set("q", "product_name:*"); }// 过滤条件if (!"".equals(catalog_name) && catalog_name != null) { solrQuery.set("fq", "product_catalog_name:" + catalog_name);// 一种}if (!"".equals(price) && price != null) { String[] split = price.split("-");// 价格0-9 50-*以上solrQuery.set("fq", "product_price:[" + split[0] + " TO " + split[1] + "]"); }// 价格排序if ("1".equals(sort) && sort != null) { solrQuery.addSort("product_price ", ORDER.desc); } else { solrQuery.addSort("product_price ", ORDER.asc); }// 分页solrQuery.setStart(0); solrQuery.setRows(16);// 默认域solrQuery.add("df", "product_keywords");// 指定域solrQuery.add("fl", "id,product_name,product_price,product_picture");// 高亮solrQuery.setHighlight(true);// 开启高亮// 指定要高亮的域solrQuery.addHighlightField("product_name");// 前缀solrQuery.setHighlightSimplePre("");// 后缀solrQuery.setHighlightSimplePost("");// 执行查询QueryResponse response = solrServer.query(solrQuery);// 获取高亮的集Map<String, Map<String, List<String>>> highlighting = response.getHighlighting();// 文档结果集SolrDocumentList docs = response.getResults();// 总条数long numFound = docs.getNumFound();for (SolrDocument doc : docs) { Products productModel = new Products();// doc.get("")的值是域名productModel.setPid((String)doc.get("id")); System.err.println("productModel>"+productModel); productModel.setPrice((float) doc.get("product_price")); productModel.setPicture((String) doc.get("product_picture"));// 通过ID获取对应的高亮if(query !=null && query.length()>0){ Map<String, List<String>> map = highlighting.get((String)doc.get("id")); List<String> liStrings1 = map.get("product_name"); productModel.setDescription(liStrings1.get(0)); }else{ productModel.setDescription((String) doc.get("product_name")); } returnlist.add(productModel); }return returnlist; } }