天天看點

Solr全文搜尋及關鍵字高亮顯示

一、存儲内容到索引庫

1、mapper

Solr全文搜尋及關鍵字高亮顯示
Solr全文搜尋及關鍵字高亮顯示
Solr全文搜尋及關鍵字高亮顯示

2、service

Solr全文搜尋及關鍵字高亮顯示

3、controller

Solr全文搜尋及關鍵字高亮顯示

4、可以看到如下結果

Solr全文搜尋及關鍵字高亮顯示

二、搜尋服務的釋出

 1、dao

@Repository
public class SearchDaoImpl implements SearchDao {
	
	@Autowired 
	private SolrServer solrServer;
 
	@Override
	public SearchResult search(SolrQuery query) throws Exception {
		// 執行查詢
		QueryResponse response = solrServer.query(query);
		//取查詢結果
		SolrDocumentList solrDocumentList = response.getResults();
		List<SearchItem> itemList = new ArrayList<SearchItem>();
		for(SolrDocument solrDocument : solrDocumentList) {
			//建立一個SearchItem對象
			SearchItem item = new SearchItem();
			item.setCategory_name((String) solrDocument.get("item_category_name"));
			item.setId((String) solrDocument.get("id"));
			item.setImage((String) solrDocument.get("item_image"));
			item.setPrice((Long) solrDocument.get("item_price"));
			item.setSell_point((String) solrDocument.get("item_sell_point"));
			//取高亮顯示
			Map<String, Map<String, List<String>>> highlighting = response.getHighlighting();
			List<String> list = highlighting.get(solrDocument.get("id")).get("item_title");
			String itemTitle = "";
			if(list != null && list.size() > 0) {
				//取得高亮後的結果
				itemTitle = list.get(0);
			}else {
				itemTitle = (String) solrDocument.get("item_title");
			}
			item.setTitle(itemTitle);
			//添加到清單
			itemList.add(item);
		}
		//建立一個SearchResult對象
		SearchResult result = new SearchResult();
		result.setItemList(itemList);
		//查詢結果總數量
		result.setRecordCount(solrDocumentList.getNumFound());
		return result;
	}

}
           

2、service

@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_title");
		//設定高亮
		query.setHighlight(true);
		query.addHighlightField("item_title");
		query.setHighlightSimplePre("<font class=\"skcolor_ljg\">");
		query.setHighlightSimplePost("</font>");
		//執行查詢
		SearchResult searchResult = searchDao.search(query);
		//計算總頁數
		Long recordCount = searchResult.getRecordCount();
		int pageCount = (int) (recordCount / rows);
		if((pageCount % rows) > 0) {
			pageCount++;
		}
		searchResult.setPageCount(pageCount);
		searchResult.setCurPage(page);
		return searchResult;
	}

}
           

3、controller

@Controller
public class SearchController {
	
	@Autowired
	private SearchService searchService;

	/**
	 * 根據關鍵字進行查詢
	 * 
	 * @param keyword
	 * @param page
	 * @param rows
	 * @return
	 */
	@RequestMapping("/q")
	@ResponseBody
	public TaotaoResult search(@RequestParam(defaultValue="")String keyword, 
			@RequestParam(defaultValue="1")Integer page, 
			@RequestParam(defaultValue="30")Integer rows) {
		try {
			//poat亂碼在過濾器中解決了,此處會有get亂碼
			//轉換字元集
			keyword = new String(keyword.getBytes("iso8859-1"), "utf-8");
			SearchResult searchResult = searchService.search(keyword, page, rows);
			return TaotaoResult.ok(searchResult);
		} catch (Exception e) {
			e.printStackTrace();
			return TaotaoResult.build(500, ExceptionUtil.getStackTrace(e));
		}
	}
 
}
           

三、在potral中實作搜尋

1、service

Solr全文搜尋及關鍵字高亮顯示
Solr全文搜尋及關鍵字高亮顯示

2、controller

Solr全文搜尋及關鍵字高亮顯示

3、結果

Solr全文搜尋及關鍵字高亮顯示

繼續閱讀