博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java调用solrj5.5.3接口,查询数据
阅读量:6824 次
发布时间:2019-06-26

本文共 5562 字,大约阅读时间需要 18 分钟。

前期准备

搭建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
> 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
>> highlightMap = queryResponse.getHighlighting(); Map
result = new HashMap
(); List
rstList = queryResponse.getBeans(SearchModel.class); for (SearchModel obj : rstList) { Map
> hiStr = highlightMap.get(obj.getUrl()); List
contentList = hiStr.get("content"); if (contentList != null && contentList.size() > 0) obj.setContentHighlight(contentList.get(0)); List
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;    }}

前端示例

其它的根据业务需求具体扩展吧,在前端可以对查询到的数据进行一些自定义展示(关键字标红高亮,每条记录分类,点击跳转到记录详情页面)。

转载于:https://www.cnblogs.com/chenpi/p/6055061.html

你可能感兴趣的文章
Java 锁
查看>>
7、索引在什么情况下遵循最左前缀的规则?
查看>>
c#中委托与事件
查看>>
mysql数据库备份之主从同步配置
查看>>
angularJs(1)指令篇
查看>>
自定义Xadmin
查看>>
jsp页面表单的遍历要怎么写
查看>>
循环引用,看我就对了
查看>>
软件工程——第一周作业
查看>>
ubuntu14.04安装vmware workstation
查看>>
ArcGIS API for Silverlight部署本地地图服务
查看>>
小知识点
查看>>
python mongodb MapReduce
查看>>
python-数据类型
查看>>
Google MapReduce/GFS/BigTable三大技术的论文中译版
查看>>
Linux atop监控工具部署
查看>>
struts2请求过程源码分析
查看>>
效率比较--集合
查看>>
jmeter IF控制器学习--使用实例
查看>>
memory prefix retro,re out 2
查看>>