前期准备
搭建solr服务
参考上一篇,搭建solr搜索服务。
添加依赖
maven工程的话,添加如下依赖,
org.apache.solr solr-solrj 5.5.3
也可以自己导入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
以下是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; }}
前端示例
其它的根据业务需求具体扩展吧,在前端可以对查询到的数据进行一些自定义展示(关键字标红高亮,每条记录分类,点击跳转到记录详情页面)。