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();
}
以上代碼均為看過黑馬視訊後自己編寫