use of cn.e3mall.common.vo.SearchResult in project e3mall by colg-cloud.
the class TbItemServiceImpl method search.
@Override
public SearchResult search(SolrQuery solrQuery) {
// 根据solrQuery查询索引库
QueryResponse query = null;
try {
query = solrServer.query(solrQuery);
} catch (SolrServerException e) {
e.printStackTrace();
LOGGER.error("SolrServer 通信/解析异常!");
}
// 取查询结果
SolrDocumentList results = query.getResults();
// 取查询结果总记录数
long recourdCount = results.getNumFound();
// 取得商品列表,需要取高亮显示
Map<String, Map<String, List<String>>> highlighting = query.getHighlighting();
List<TbItemSerach> itemList = new ArrayList<>();
for (SolrDocument doc : results) {
TbItemSerach itemSerach = new TbItemSerach();
itemSerach.setId((String) doc.get("id"));
itemSerach.setSellPoint((String) doc.get("item_sell_point"));
itemSerach.setPrice((Long) doc.get("item_price"));
itemSerach.setImage((String) doc.get("item_image"));
itemSerach.setCategoryName((String) doc.get("item_category_name"));
// 取高亮显示
List<String> list = highlighting.get(doc.get("id")).get("item_title");
String title;
if (ListUtil.isNotEmpty(list)) {
title = list.get(0);
} else {
title = (String) doc.get("item_title");
}
itemSerach.setTitle(title);
// 添加到商品列表
itemList.add(itemSerach);
}
// 封装到SearchResult
SearchResult searchResult = new SearchResult();
searchResult.setRecourdCount(recourdCount);
searchResult.setItemSerachs(itemList);
// 返回结果
return searchResult;
}
use of cn.e3mall.common.vo.SearchResult in project e3mall by colg-cloud.
the class TbItemServiceImpl method search.
@Override
public SearchResult search(String keyword, Integer page, Integer rows) {
// 创建一个SolrQuery对象
SolrQuery query = new SolrQuery();
// 设置查询条件
query.setQuery(keyword);
// 设置分页条件
Integer start = ((page <= 0 ? 1 : page) - 1) * rows;
query.setStart(start).setRows(rows);
// 设置默认搜索域
query.set("df", "item_title");
// 开启高亮显示
query.addHighlightField("item_title").setHighlightSimplePre("<em style=\"color: red;\">").setHighlightSimplePost("</em>");
// 执行查询
SearchResult searchResult = this.search(query);
// 计算总页数
Long recourdCount = searchResult.getRecourdCount();
Integer totalPages = (int) ((recourdCount + rows - 1) / rows);
searchResult.setTotalPages(totalPages);
// 返回结果
return searchResult;
}
use of cn.e3mall.common.vo.SearchResult in project e3mall by colg-cloud.
the class TbItemSearchController method searchItemList.
/**
* 搜索
*
* @param keyword
* @param page
* @param model
* @return
*/
@GetMapping("/search")
public String searchItemList(String keyword, @RequestParam(defaultValue = "1") Integer page, Model model) {
// get请求,乱码,更改tomcat编码,或者转码
// keyword = StringUtil.iso2Utf(keyword);
// 查询商品列表
SearchResult searchResult = tbItemService.search(keyword, page, SEARCH_RESULT_ROWS);
// 把结果传递给页面
model.addAttribute("query", keyword);
model.addAttribute("totalPages", searchResult.getTotalPages());
model.addAttribute("page", page);
model.addAttribute("recourdCount", searchResult.getRecourdCount());
model.addAttribute("itemList", searchResult.getItemSerachs());
// 返回逻辑视图
return "search";
}
Aggregations