solr搜索服务器 性能,搜索学习--Solr全文搜索服务器的基本使用(一)——SolrJ的使用...

作为一般的Lucene使用者,我们直接用Lucene虽然可以实现很多自己想要的自定义功能,但是对于一般的项目,为了方便开发和维护,我们通常会使用现成的搜索服务器。现在常用的有Solr和ElasticSearch。

对于Solr服务器如何搭建,Solr服务器的搭建,之前已经整理过,这次,我将使用SolrJ来调用Solr服务器。

在Solr中配置Core

在managed-schema中,主要的配置为:

blogId

依赖

org.apache.solr

solr-solrj

6.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);//设置连接超时时间(单位毫秒) 1000

server.setSoTimeout(60000); 设置读数据超时时间(单位毫秒) 1000

server.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 {

@Field

private String blogId;

@Field

private String blogTitle;

@Field

private String blogContent;

@Field

private Date createTime;

@Field

private 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;

}

@Override

public 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(Listentitys) 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 Listquery() throws Exception {

SolrQuery query = new SolrQuery();

query.setQuery("*:*");

query.setStart(0);//开始记录数

query.setRows(10000);//总条数

QueryResponse queryResponse = server.query(query);

Listresults = queryResponse.getBeans(BlogCore.class);

return results;

}

//搜索keywords

public ListqueryByKeyWords(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);//每个分片的最大长度,默认为100

query.setStart(0);//开始记录数

query.setRows(100);//总条数

QueryResponse queryResponse = server.query(query);

long sum = queryResponse.getResults().getNumFound();

Listresults = queryResponse.getBeans(BlogCore.class);

//输出高亮

Map>> highlighting = queryResponse.getHighlighting();

for (BlogCore result : results) {

Map> map = highlighting.get(result.getBlogId()+"");

Listlist = 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 {

@Test

public 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);

}

@Test

public void testFindAll() throws Exception {

BlogSolrDao blogSolrDao = new BlogSolrDao();

Listlist = blogSolrDao.query();

for (BlogCore blogCore : list) {

System.out.println(blogCore);

}

}

@Test

public void testSearch() throws Exception {

BlogSolrDao blogSolrDao = new BlogSolrDao();

Listlist = 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'}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值