一.ProductResult结果集
private long totalPages;
private long recordCount;
private List<ProductSearch> productSearchList;
二.搜索结果服务层service
根据查询对象在索引库进行查询
SolrServer solrServer=new HttpSolrServer("http://192.168.0.128:8080/solr/collection1");
//根据query对象进行查询
QueryResponse response = solrServer.query(query);
//取查询结果
SolrDocumentList solrDocumentList = response.getResults();
//取查询结果总记录数
long numFound = solrDocumentList.getNumFound();
ProductResult productResult=new ProductResult();
productResult.setRecordCount(numFound);
List<ProductSearch> itemList = new ArrayList<>();
//把查询结果封装到SearchItem对象中
for (SolrDocument solrDocument : solrDocumentList) {
ProductSearch item = new ProductSearch();
item.setProduct_type((String) solrDocument.get("product_type"));
item.setProduct_id((String) solrDocument.get("id"));
item.setProduct_img((String) solrDocument.get("product_img"));
item.setProduct_price((long) solrDocument.get("product_price"));
item.setProduct_sale(Integer.parseInt((String) solrDocument.get("product_sale")));
item.setProduct_name((String)solrDocument.get("product_name"));
// item.setProduct_number(Integer.parseInt((String)solrDocument.get("product_number")));
//取高亮显示
Map<String, Map<String, List<String>>> highlighting = response.getHighlighting();
List<String> list = highlighting.get(solrDocument.get("id")).get("product_name");
System.out.println(list);
String title = "";
if (list != null && list.size() > 0) {
title = list.get(0);
} else {
title = (String) solrDocument.get("product_name");
}
System.out.println(title);
item.setProduct_name(title);
//添加到商品列表
itemList.add(item);
}
//把结果添加到SearchResult中
productResult.setProductSearchList(itemList);
//返回
return productResult;
三.计算总条数,总页数,写入结果集合
//根据查询条件拼装查询对象
//创建一个SolrQuery对象
SolrQuery query = new SolrQuery();
//设置查询条件
query.setQuery(queryString);
//设置分页条件
if (page < 1) page =1;
query.setStart((page - 1) * rows);
if (rows < 1) rows = 10;
query.setRows(rows);
//设置默认搜索域
query.set("df", "product_keywords");
//设置高亮显示
query.setHighlight(true);
query.addHighlightField("product_name");
query.setHighlightSimplePre("<font color='pink'>");
query.setHighlightSimplePost("</font>");
//调用dao执行查询
ProductResult productResult=this.searchquery(query);
//计算查询结果的总页数
long recordCount = productResult.getRecordCount();
long pages = recordCount / rows;
if (recordCount % rows > 0) {
pages++;
}
productResult.setTotalPages(pages);
//返回结果
return productResult;
四.将结果集通过控制层传到前台
mast = new String(mast.getBytes("iso8859-1"),"UTF-8");
System.out.println("用户要搜索的是"+productSearch.getMast());
ProductResult productResult = null;
try {
productResult = this.productSearch.search(productSearch.getMast(), page, rows);
System.out.println(productResult);
} catch (Exception e) {
e.printStackTrace();
}
//把结果传递给页面
model.addAttribute("query", mast);
model.addAttribute("totalPages", productResult.getTotalPages());
model.addAttribute("itemList", productResult.getProductSearchList());
model.addAttribute("page", page);
model.addAttribute("query", productResult);
return "result";