solr的配置以及流程

spring 配置文件中

<!-- 配置SolrServer对象 -->

     <!-- 单机版 -->

     <bean id="httpSolrServer" class="org.apache.solr.client.solrj.impl.HttpSolrServer">

          <constructor-arg name="baseURL" value="${SOLR.SERVER.URL}"></constructor-arg>

     </bean>


properties文件中


SOLR.SERVER.URL=localhost:8080/solr


pojo

public classSearchResult {

 

     //商品列表

     private List<Item> itemList;

     //总记录数

     private long recordCount;

     //总页数

     private long pageCount;

     //当前页

     private long curPage;

}


daoImpl

@Repository

public class SearchDaoImpl implements SearchDao {

    

     @Autowired

     private SolrServer solrServer;

 

     @Override

     public SearchResult search(SolrQuery query) throws Exception {

          //返回值对象

          SearchResult result = new SearchResult();

          //根据查询条件查询索引库

          QueryResponse queryResponse = solrServer.query(query);

          //取查询结果

          SolrDocumentList solrDocumentList = queryResponse.getResults();

          //取查询结果总数量

          result.setRecordCount(solrDocumentList.getNumFound());

          //商品列表

          List<Item> itemList = new ArrayList<>();

          //取高亮显示

          Map<String, Map<String, List<String>>> highlighting = queryResponse.getHighlighting();

          //取商品列表

          for (SolrDocument solrDocument : solrDocumentList) {

               //创建一商品对象

               Item item = new Item();

               item.setId((String) solrDocument.get("id"));

               //取高亮显示的结果

               List<String> list = highlighting.get(solrDocument.get("id")).get("item_title");

               String title = "";

               if (list != null && list.size()>0) {

                    title = list.get(0);

               } else {

                    title = (String) solrDocument.get("item_title");

               }

               item.setTitle(title);

               item.setImage((String) solrDocument.get("item_image"));

               item.setPrice((long) solrDocument.get("item_price"));

               item.setSell_point((String) solrDocument.get("item_sell_point"));

               item.setCategory_name((String) solrDocument.get("item_category_name"));

               //添加的商品列表

               itemList.add(item);

          }

          result.setItemList(itemList);

          return result;

     }

 

}


serviceImpl

@Service

public class SearchServiceImpl implements SearchService {

 

     @Autowired

     private SearchDao searchDao;

     @Override

     public SearchResult search(String queryString, int page, int rows) throws Exception {

          //创建查询对象

          SolrQuery query = new SolrQuery();

          //设置查询条件

          query.setQuery(queryString);

          //设置分页

          query.setStart((page - 1) * rows);

          query.setRows(rows);

          //设置默认搜素域

          query.set("df", "item_keywords");

          //设置高亮显示

          query.setHighlight(true);

          query.addHighlightField("item_title");

          query.setHighlightSimplePre("<em style=\"color:red\">");

          query.setHighlightSimplePost("</em>");

          //执行查询

          SearchResult searchResult = searchDao.search(query);

          //计算查询结果总页数

          long recordCount = searchResult.getRecordCount();

          long pageCount = recordCount / rows;

          if (recordCount % rows > 0) {

               pageCount++;

          }

          searchResult.setPageCount(pageCount);

          searchResult.setCurPage(page);

         

          return searchResult;

     }

 

}


controller

@Controller

public class SearchController {

 

     @Autowired

     private SearchService searchService;

    

     @RequestMapping(value="/query", method=RequestMethod.GET)

     @ResponseBody

     public Result search(@RequestParam("q")String queryString,

               @RequestParam(defaultValue="1")Integer page,

               @RequestParam(defaultValue="60")Integer rows) {

          //查询条件不能为空

          if (StringUtils.isBlank(queryString)) {

               return TaotaoResult.build(400, "查询条件不能为空");

          }

          SearchResult searchResult = null;

          try {

//解决乱码

queryString = new String(queryString.getBytes("ios8859-1"),"utf-8");

               searchResult = searchService.search(queryString, page, rows);

          } catch (Exception e) {

               e.printStackTrace();

               return Result.build(500, ExceptionUtil.getStackTrace(e));

          }

          return Result.ok(searchResult);

         

     }

    

}


以上为搜索发布

商品要经过

导入商品数据到索引库

查出商品列表


在serviceImpl中

@Autowired
private Mapper mapper;

@Autowired
private SolrServer solrServer;

@Override
public Result importAll() {
try {

//查询商品列表
List<I> list = mapper.getList();
//把商品信息写入索引库
for (I i : list) {
//创建一个SolrInputDocument对象
SolrInputDocument document = new SolrInputDocument();
document.setField("id", i.getId());
document.setField("item_title", i.getTitle());
document.setField("item_sell_point", i.getSell_point());
document.setField("item_price", i.getPrice());
document.setField("item_image", i.getImage());
document.setField("item_category_name", i.getCategory_name());
document.setField("item_desc", i.getItem_des());
//写入索引库
solrServer.add(document);
}
//提交修改
solrServer.commit();
} catch (Exception e) {
e.printStackTrace();
return TaotaoResult.build(500, ExceptionUtil.getStackTrace(e));
}
return Result.ok();
}


以上代码均为看过黑马视频后自己编写

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

乐观的Terry

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值