Search in sources :

Example 1 with TbItemSerach

use of cn.e3mall.common.vo.TbItemSerach 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 TbItemSerach

use of cn.e3mall.common.vo.TbItemSerach in project e3mall by colg-cloud.

the class TbItemServiceImpl method importAllItems.

@Override
public E3Result importAllItems() {
    try {
        // 查询所有商品列表
        List<TbItemSerach> list = tbItemMapper.getItemList();
        // 遍历商品列表
        for (TbItemSerach serach : list) {
            // 创建文档对象
            SolrInputDocument doc = new SolrInputDocument();
            // 向文档对象中添加域
            doc.addField("id", serach.getId());
            doc.addField("item_title", serach.getTitle());
            doc.addField("item_sell_point", serach.getSellPoint());
            doc.addField("item_price", serach.getPrice());
            doc.addField("item_image", serach.getImage());
            doc.addField("item_category_name", serach.getCategoryName());
            // 把文档对象写入索引库
            solrServer.add(doc);
        }
        // 提交
        solrServer.commit();
        // 返回导入成功
        return E3Result.ok();
    } catch (SolrServerException e) {
        e.printStackTrace();
        return E3Result.fail(500, "SolrServer 通信/解析异常!");
    } catch (IOException e) {
        e.printStackTrace();
        return E3Result.fail(500, "SolrServer I/O异常!");
    }
}
Also used : SolrInputDocument(org.apache.solr.common.SolrInputDocument) SolrServerException(org.apache.solr.client.solrj.SolrServerException) TbItemSerach(cn.e3mall.common.vo.TbItemSerach) IOException(java.io.IOException)

Example 3 with TbItemSerach

use of cn.e3mall.common.vo.TbItemSerach in project e3mall by colg-cloud.

the class TbItemServiceImpl method addDocument.

@Override
public E3Result addDocument(Long id) {
    // 根据商品id查询商品信息
    TbItemSerach serach = tbItemMapper.getItemById(id);
    if (serach != null) {
        // 创建一个文档对象
        SolrInputDocument doc = new SolrInputDocument();
        // 向文档对象中添加域
        doc.addField("id", serach.getId());
        doc.addField("item_title", serach.getTitle());
        doc.addField("item_sell_point", serach.getSellPoint());
        doc.addField("item_price", serach.getPrice());
        doc.addField("item_image", serach.getImage());
        doc.addField("item_category_name", serach.getCategoryName());
        // 把文档写入索引库
        try {
            solrServer.add(doc);
            // 提交
            solrServer.commit();
        } catch (SolrServerException e) {
            e.printStackTrace();
            return E3Result.fail(500, "SolrServer 通信/解析异常!");
        } catch (IOException e) {
            e.printStackTrace();
            return E3Result.fail(500, "SolrServer I/O异常!");
        }
    }
    return E3Result.ok();
}
Also used : SolrInputDocument(org.apache.solr.common.SolrInputDocument) SolrServerException(org.apache.solr.client.solrj.SolrServerException) TbItemSerach(cn.e3mall.common.vo.TbItemSerach) IOException(java.io.IOException)

Aggregations

TbItemSerach (cn.e3mall.common.vo.TbItemSerach)3 SolrServerException (org.apache.solr.client.solrj.SolrServerException)3 IOException (java.io.IOException)2 SolrInputDocument (org.apache.solr.common.SolrInputDocument)2 SearchResult (cn.e3mall.common.vo.SearchResult)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)1 SolrDocument (org.apache.solr.common.SolrDocument)1 SolrDocumentList (org.apache.solr.common.SolrDocumentList)1