欢迎投稿

今日深度:

solr搜索,

solr搜索,


/**
 * 商品数据导入及从solr库中获取查询结果
 * @author wyh
 *2018年8月9日
 *
 */
@Service
public class SolrServiceImpl implements SolrService {

    @Autowired
    private SolrItemMapper sMapper;
    
    @Autowired
    private SolrServer solrServer;
    
    @Override
    public HwResult solrImport() {
        
        //先从数据库中获取数据
        List<ItemSolr> list = sMapper.getItemList();
        
        //将数据导入到solr中
        try {
            for(ItemSolr is : list){
                //创建文档对象
                SolrInputDocument sid = new SolrInputDocument();
                //给域赋值
                sid.addField("item_title", is.getTitle());
                sid.setField("id", is.getId());
                sid.setField("item_sell_point",is.getSell_point());
                sid.setField("item_image", is.getImage());
                sid.setField("item_price", is.getPrice());
                sid.setField("item_category_name", is.getCname());
                sid.setField("item_cid", is.getCid());
                //执行添加
                solrServer.add(sid);
                //提交
                solrServer.commit();
            }
            return HwResult.ok();

        } catch (Exception e) {
            return HwResult.build(500, "数据导入失败");
        }
    }

    
    
    @Autowired
    private SearchItemDao searchItemDao;
    
    //根据查询条件查询商品列表信息
    @Override
    public SearchItemResult searchItemsByKeywors(String keyword,
            Integer pageNum, Integer pageSize) throws Exception {
        // 查询条件在service层封装,所以在这里创建查询对象
        SolrQuery sq = new SolrQuery();
        //添加查询条件
        sq.set("q", keyword);
        //设置分页条件
        sq.setStart((pageNum-1)*pageSize);//从第几条开始
        sq.setRows(pageSize);//一页显示多少条
        //设置默认搜索域
        sq.set("df", "item_title");
        //开启高亮
        sq.setHighlight(true);
        //设置高亮显示的域
        sq.addHighlightField("item_title");//最好不要硬编码到代码中,应该写在配置文件中
        //设置前缀和后缀
        sq.setHighlightSimplePre("<em><font color='red'>");
        sq.setHighlightSimplePost("</font></em>");
        //设置好查询对象之后,调用dao
        SearchItemResult result = searchItemDao.searchItems(sq);
        //dao返回过来的有总记录数,但是没有总页数
        //获取总记录数
        Long total = result.getTotal();
        //计算总页数
        Long page = total/pageSize;
        result.setTaotalPage(total%pageSize!=0?page+1:page);
        return result;
    }

}
 

www.htsjk.Com true http://www.htsjk.com/solr/34888.html NewsArticle solr搜索, /**  * 商品数据导入及从solr库中获取查询结果  * @author wyh  *2018年8月9日  *  */ @Service public class SolrServiceImpl implements SolrService {     @Autowired     private SolrItemMapper sMapper;  ...
相关文章
    暂无相关文章
评论暂时关闭