作者:我是你的鹏鹏鹏爷 | 来源:互联网 | 2023-07-12 16:41
作为一般的Lucene使用者,我们直接用Lucene虽然可以实现很多自己想要的自定义功能,但是对于一般的项目,为了方便开发和维护,我们通常会使用现成的搜索服务器。现在常用的有Solr和ElasticSearch。
对于Solr服务器如何搭建,Solr服务器的搭建,之前已经整理过,这次,我将使用SolrJ来调用Solr服务器。
在Solr中配置Core
在managed-schema中,主要的配置为:
blogId
依赖
org.apache.solrsolr-solrj6.5.1
SolrServer.Java
package top.yuyufeng.learn.lucene.solr;import org.apache.solr.client.solrj.impl.HttpSolrClient;/*** @author yuyufeng* @date 2017/12/6*/
public class SolrServer {private static HttpSolrClient server = null;private final static String solrServerUrl = "http://127.0.0.1:8983/solr/blog";public static HttpSolrClient getServer() {if (server == null) {server = new HttpSolrClient(solrServerUrl);server.setDefaultMaxConnectionsPerHost(1000);server.setMaxTotalConnections(10000);//最大连接数server.setConnectionTimeout(60000);//设置连接超时时间(单位毫秒) 1000server.setSoTimeout(60000); 设置读数据超时时间(单位毫秒) 1000server.setFollowRedirects(false);//遵循从定向server.setAllowCompression(true);//允许压缩}return server;}public static void main(String[] args) {HttpSolrClient client = getServer();System.out.println(client);}
}
BlogCore.java
package top.yuyufeng.learn.lucene.solr.core;import org.apache.solr.client.solrj.beans.Field;import java.util.Date;/*** @author yuyufeng* @date 2017/12/6*/
public class BlogCore {@Fieldprivate String blogId;@Fieldprivate String blogTitle;@Fieldprivate String blogContent;@Fieldprivate Date createTime;@Fieldprivate String keywords;public String getBlogId() {return blogId;}public void setBlogId(String blogId) {this.blogId = blogId;}public String getBlogTitle() {return blogTitle;}public void setBlogTitle(String blogTitle) {this.blogTitle = blogTitle;}public String getBlogContent() {return blogContent;}public void setBlogContent(String blogContent) {this.blogContent = blogContent;}public Date getCreateTime() {return createTime;}public void setCreateTime(Date createTime) {this.createTime = createTime;}public String getKeywords() {return keywords;}public void setKeywords(String keywords) {this.keywords = keywords;}@Overridepublic String toString() {return "BlogCore{" +"blogId='" + blogId + '\'' +", blogTitle='" + blogTitle + '\'' +", blogContent='" + blogContent + '\'' +", createTime=" + createTime +", keywords='" + keywords + '\'' +'}';}
}
BlogSolrDao.java
package top.yuyufeng.learn.lucene.solr.dao;import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.client.solrj.response.UpdateResponse;
import top.yuyufeng.learn.lucene.solr.SolrServer;
import top.yuyufeng.learn.lucene.solr.core.BlogCore;import java.awt.print.Pageable;
import java.util.List;
import java.util.Map;/*** @author yuyufeng* @date 2017/12/6*/
public class BlogSolrDao{HttpSolrClient server;public BlogSolrDao() {server = SolrServer.getServer();}/*** 按Bean 添加/修改 索引** @throws Exception*/public int addIndex(BlogCore entity) throws Exception {server.addBean(entity);UpdateResponse updateResponse = server.commit();return updateResponse.getStatus();}/*** 按Bean 添加/修改 索引** @throws Exception*/public int addIndexList(List entitys) throws Exception {server.addBeans(entitys);UpdateResponse updateResponse = server.commit();return updateResponse.getStatus();}/*** 删除索引 按查询** @throws Exception*/public int deleteAll() throws Exception {String query = "*:*";server.deleteByQuery(query);server.commit();UpdateResponse updateResponse = server.commit();return updateResponse.getStatus();}/*** 删除索引 按id** @throws Exception*/public int deleteByQuery(Long id) throws Exception {server.deleteById(id + "");server.commit();UpdateResponse updateResponse = server.commit();return updateResponse.getStatus();}//查询所有public List query() throws Exception {SolrQuery query = new SolrQuery();query.setQuery("*:*");query.setStart(0);//开始记录数query.setRows(10000);//总条数QueryResponse queryResponse = server.query(query);List results = queryResponse.getBeans(BlogCore.class);return results;}//搜索keywordspublic List queryByKeyWords(String keywords) throws Exception {SolrQuery query = new SolrQuery();query.set("q", "keywords:" + keywords);//*通配多个字符
// query.set("sort", "product_price desc");//======高亮设置===//开启高亮query.setHighlight(true);//高亮域query.addHighlightField("blogContent");query.addHighlightField("blogTitle");//前缀query.setHighlightSimplePre("");//后缀query.setHighlightSimplePost("");//query.setHighlightSnippets(1);//结果分片数,默认为1//query.setHighlightFragsize(1000);//每个分片的最大长度,默认为100query.setStart(0);//开始记录数query.setRows(100);//总条数QueryResponse queryResponse = server.query(query);long sum = queryResponse.getResults().getNumFound();List results = queryResponse.getBeans(BlogCore.class);//输出高亮Map>> highlighting = queryResponse.getHighlighting();for (BlogCore result : results) {Map> map = highlighting.get(result.getBlogId()+"");List list = map.get("blogTitle");if (list != null && list.size() > 0) {result.setBlogTitle(list.get(0));}list = map.get("blogContent");if (list != null && list.size() > 0) {result.setBlogContent(list.get(0));}}return results;}
}
SolrTest.java
package top.yuyufeng.learn.lucene.solr;import org.junit.Test;
import top.yuyufeng.learn.lucene.solr.core.BlogCore;
import top.yuyufeng.learn.lucene.solr.dao.BlogSolrDao;import java.util.Date;
import java.util.List;/*** @author yuyufeng* @date 2017/12/6*/
public class SolrTest {@Testpublic void testIndex() throws Exception {BlogSolrDao blogSolrDao = new BlogSolrDao();BlogCore blog = new BlogCore();blog.setBlogId("2");blog.setBlogTitle("达摩院超越业界龙头");blog.setBlogContent("达摩院一定也必须要超越英特尔,必须超越微软,必须超越IBM,因为我们生于二十一世纪,我们是有机会后发优势的。");blog.setCreateTime(new Date());blogSolrDao.addIndex(blog);}@Testpublic void testFindAll() throws Exception {BlogSolrDao blogSolrDao = new BlogSolrDao();List list = blogSolrDao.query();for (BlogCore blogCore : list) {System.out.println(blogCore);}}@Testpublic void testSearch() throws Exception {BlogSolrDao blogSolrDao = new BlogSolrDao();List list = blogSolrDao.queryByKeyWords("达摩院");for (BlogCore blogCore : list) {System.out.println(blogCore);}}
}
运行结果:
启动Solr服务器后,我建立了一些索引,然后我执行Search方法
BlogCore{blogId='2', blogTitle='达摩院超越业界龙头', blogContent='达摩院一定也必须要超越英特尔,必须超越微软,必须超越IBM,因为我们生于二十一世纪,我们是有机会后发优势的。', createTime=Wed Dec 06 13:38:12 CST 2017, keywords='null'}
BlogCore{blogId='1', blogTitle='马云表达愿景', blogContent='10月11日杭州云栖大会上,马云表达了对新建成的阿里巴巴全球研究院—阿里巴巴达摩院的愿景,希望达摩院二十年内成为世界第一大经济体,服务世界二十亿人,创造一亿个工作岗位。', createTime=Wed Dec 06 12:03:56 CST 2017, keywords='null'}