Search in sources :

Example 1 with SearchResult

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;
}
Also used : SolrServerException(org.apache.solr.client.solrj.SolrServerException) ArrayList(java.util.ArrayList) TbItemSerach(cn.e3mall.common.vo.TbItemSerach) SearchResult(cn.e3mall.common.vo.SearchResult) SolrDocumentList(org.apache.solr.common.SolrDocumentList) SolrDocument(org.apache.solr.common.SolrDocument) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) Map(java.util.Map)

Example 2 with 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;
}
Also used : SearchResult(cn.e3mall.common.vo.SearchResult) SolrQuery(org.apache.solr.client.solrj.SolrQuery)

Example 3 with 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";
}
Also used : SearchResult(cn.e3mall.common.vo.SearchResult) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Aggregations

SearchResult (cn.e3mall.common.vo.SearchResult)3 TbItemSerach (cn.e3mall.common.vo.TbItemSerach)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 SolrQuery (org.apache.solr.client.solrj.SolrQuery)1 SolrServerException (org.apache.solr.client.solrj.SolrServerException)1 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)1 SolrDocument (org.apache.solr.common.SolrDocument)1 SolrDocumentList (org.apache.solr.common.SolrDocumentList)1 GetMapping (org.springframework.web.bind.annotation.GetMapping)1