Java项目实战 - solr发布搜索服务 Posted on 2018-07-22 | In Java | | 96 发布搜索服务供其它工程调用。 Dao要定义返回的POJO:12345678public class SearchResult { private List<Item> itemList; private long totalItemCount; private long totalPageCount; private long currentPageNumber; private long currentRowCount;} 并实现Dao:123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051@Repositorypublic class SearchDaoImpl implements SearchDao { @Autowired private SolrServer solrServer; @Override public SearchResult search(SolrQuery query) throws Exception { // return result SearchResult resultList = new SearchResult(); // solr search QueryResponse response = solrServer.query(query); SolrDocumentList solrDocumentList = response.getResults(); // highlight Map<String, Map<String, List<String>>> highlighting = response.getHighlighting(); //get item list List<Item> itemList = new ArrayList<>(); for (SolrDocument solrDocument : solrDocumentList) { Item item = new Item(); item.setId((String) solrDocument.get("id")); //get highlight 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); } resultList.setItemList(itemList); // get total item number resultList.setTotalItemCount(solrDocumentList.getNumFound()); return resultList; }} Service12345678910111213141516171819202122232425262728293031323334353637@Servicepublic class ServiceSearchImpl implements SearchService { @Autowired private SearchDao searchDao; @Override public SearchResult search(String query, int page, int rows) throws Exception { SolrQuery solrQuery = new SolrQuery(); solrQuery.setQuery(query); solrQuery.setStart((page -1) * rows); solrQuery.setRows(rows); solrQuery.set("df", "item_keywords"); solrQuery.setHighlight(true); solrQuery.addHighlightField("item_title"); solrQuery.setHighlightSimplePre("<em style=\"color:red\">"); solrQuery.setHighlightSimplePost("</em>"); SearchResult result = searchDao.search(solrQuery); long totalItemCount = result.getTotalItemCount(); long totalPageCount = totalItemCount / rows; if (totalItemCount % rows > 0){ totalPageCount++; } result.setTotalPageCount(totalPageCount); result.setCurrentPageNumber(page); result.setCurrentRowCount(rows); return result; }} Controller12345678910111213141516171819202122232425262728@Controllerpublic class SearchServiceController { @Autowired private SearchService searchService; @RequestMapping(value="/query", method=RequestMethod.GET) @ResponseBody public TaotaoResult search(@RequestParam("q")String query, @RequestParam(defaultValue="1")Integer page, @RequestParam(defaultValue="20")Integer rows){ //query is null if(StringUtils.isBlank(query)) return TaotaoResult.build(400, "Should contain query string"); SearchResult searchResult = null; try { query = new String(query.getBytes("iso8859-1"), "utf-8"); searchResult = searchService.search(query, page, rows); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return TaotaoResult.ok(searchResult); }} 欢迎您扫一扫上面的微信公众号,订阅我的博客! 分享创造价值,您的支持将鼓励我继续前行! Donate WeChat Pay Alipay Post author: 唐胡璐 Post link: http://izheyi.com/2018/07/22/Java项目实战-solr发布搜索服务/ Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.