前期准备
搭建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;
}
}
前端示例
其它的根据业务需求具体扩展吧,在前端可以对查询到的数据进行一些自定义展示(关键字标红高亮,每条记录分类,点击跳转到记录详情页面)。