文章出處
文章列表
前期準備
搭建solr服務
參考上一篇,搭建solr搜索服務。
添加依賴
maven工程的話,添加如下依賴,
<!-- https://mvnrepository.com/artifact/org.apache.solr/solr-solrj --> <dependency> <groupId>org.apache.solr</groupId> <artifactId>solr-solrj</artifactId> <version>5.5.3</version> </dependency>
也可以自己導入jar包
在solr安裝目錄下,找到solr-5.5.3\dist\solrj-lib路徑,添加里面所有的jar包到自己的工程,別忘了在外面的文件夾還有個solr-solrj-5.5.3.jar包,一起拷貝。
編寫調用代碼
這里簡單給個示例(包含分頁),Spring mvc工程的:
@ResponseBody @RequestMapping(value = "/searchByKeyWord", method = { RequestMethod.GET }, produces = "application/json; charset=utf-8") @ApiOperation(value = "搜索接口", notes = "", httpMethod = "GET") public BaseResponse<Map<String, Object>> search(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "keyWord", defaultValue = "") String keyWord, @RequestParam(value = "pageSize", defaultValue = "10") long pageSize, @RequestParam(value = "pageOffset", defaultValue = "0") long pageOffset) { System.out.println(pageSize + "," + pageOffset); try { SolrClient solr = new HttpSolrClient(solrServiceUrl); SolrQuery query = new SolrQuery(); String kw = URLDecoder.decode(keyWord, "UTF-8"); query.set("q", "title:" + kw + " and content:" + kw); query.set("start", String.valueOf(pageOffset * pageSize)); query.set("rows", String.valueOf(pageSize)); query.addHighlightField("content"); query.setHighlight(true).setHighlightSnippets(1); query.setParam("hl.fl", "title,content"); query.setHighlightFragsize(300); QueryResponse queryResponse = solr.query(query); Map<String, Map<String, List<String>>> highlightMap = queryResponse.getHighlighting(); Map<String, Object> result = new HashMap<String, Object>(); List<SearchModel> rstList = queryResponse.getBeans(SearchModel.class); for (SearchModel obj : rstList) { Map<String, List<String>> hiStr = highlightMap.get(obj.getUrl()); List<String> contentList = hiStr.get("content"); if (contentList != null && contentList.size() > 0) obj.setContentHighlight(contentList.get(0)); List<String> titleList = hiStr.get("title"); if (titleList != null && titleList.size() > 0) obj.setTitleHighlight(titleList.get(0)); } result.put("success", true); result.put("key", URLDecoder.decode(keyWord, "UTF-8")); result.put("list", rstList); result.put("totalCount", queryResponse.getResults().getNumFound()); solr.close(); return BaseResponse.buildSuccessResponse(result); } catch (Exception e) { LOGGER.error(e.toString(), e); return BaseResponse .buildFailResponse(HttpStatus.INTERNAL_SERVER_ERROR.value(), "搜索異常."); } }
以下是SearchModel類,注意要與data-config.xml配置的字段對應起來,還有就是不要忘了在字段前面加上@Field注解(org.apache.solr.client.solrj.beans.Field)。
package com.cetiti.eppsas.model; import org.apache.solr.client.solrj.beans.Field; public class SearchModel { private String origin; private String title; private String content; private String linkUrl; private String keyWord; private long postTime; private String url; private String titleHighlight; private String contentHighlight; /** * @return the origin */ public String getOrigin() { return origin; } /** * @param origin the origin to set */ @Field("origin") public void setOrigin(String origin) { this.origin = origin; } /** * @return the title */ public String getTitle() { return title; } /** * @param title the title to set */ @Field("title") public void setTitle(String title) { this.title = title; } /** * @return the content */ public String getContent() { return content; } /** * @param content the content to set */ @Field("content") public void setContent(String content) { this.content = content; } /** * @return the linkUrl */ public String getLinkUrl() { return linkUrl; } /** * @param linkUrl the linkUrl to set */ @Field("linkUrl") public void setLinkUrl(String linkUrl) { this.linkUrl = linkUrl; } /** * @return the keyWord */ public String getKeyWord() { return keyWord; } /** * @param keyWord the keyWord to set */ @Field("keyWord") public void setKeyWord(String keyWord) { this.keyWord = keyWord; } /** * @return the url */ public String getUrl() { return url; } /** * @param url the url to set */ @Field("url") public void setUrl(String url) { this.url = url; } /** * @return the postTime */ public long getPostTime() { return postTime; } /** * @param postTime the postTime to set */ @Field("postTime") public void setPostTime(long postTime) { this.postTime = postTime; } /** * @return the titleHighlight */ public String getTitleHighlight() { return titleHighlight; } /** * @param titleHighlight the titleHighlight to set */ public void setTitleHighlight(String titleHighlight) { this.titleHighlight = titleHighlight; } /** * @return the contentHighlight */ public String getContentHighlight() { return contentHighlight; } /** * @param contentHighlight the contentHighlight to set */ public void setContentHighlight(String contentHighlight) { this.contentHighlight = contentHighlight; } }
前端示例
其它的根據業務需求具體擴展吧,在前端可以對查詢到的數據進行一些自定義展示(關鍵字標紅高亮,每條記錄分類,點擊跳轉到記錄詳情頁面)。
文章列表
全站熱搜