Solr文档学习--Solrj的使用
2016-09-11 22:19
316 查看
首先启动solr
![](https://img-blog.csdn.net/20160911213623087)
![](https://img-blog.csdn.net/20160911215034039)
SolrClient有4个实现类
CloudSolrClient
SolrJ client class to communicate with SolrCloud. Instances of this class communicate with Zookeeper to discover Solr endpoints for SolrCloud collections, and then use the {@link BHttpSolrClient} to issue requests.
用来连接到SolrCloud的
ConcurrentUpdateSolrClient
ConcurrentUpdateSolrClient buffers all added documents and writes them into open HTTP connections. This class is thread safe. Params from {@link UpdateRequest} are converted to http request parameters. When params change between UpdateRequests a new HTTP request is started.
线程安全的SolrClient
LBHttpSolrClient
LBHttpSolrClient or “LoadBalanced HttpSolrClient” is a load balancing wrapper around {@link HttpSolrClient}. This is useful when you have multiple Solr servers and the requests need to be Load Balanced among them.
负载均衡的HttpSolrClient
HttpSolrClient
A SolrClient implementation that talks directly to a Solr server via HTTP
通过HTTP直接连接到Solr服务器
There are two ways to use an HttpSolrClient:
1) Pass a URL to the constructor that points directly at a particular core
SolrClient client = new HttpSolrClient("http://my-solr-server:8983/solr/core1");
QueryResponse resp = client.query(new SolrQuery(":"));
In this case, you can query the given core directly, but you cannot query any other cores or issue CoreAdmin requests with this client.
2) Pass the base URL of the node to the constructor
In this case, you must pass the name of the required core for all queries and updates, but you may use the same client for all cores, and for CoreAdmin requests.
![](https://img-blog.csdn.net/20160911220213701)
在连接url中指出collection
管理员客户端的查询结果
![](https://img-blog.csdn.net/20160911220914329)
看一下程序的运行结果
顺利连接到服务器
![](https://img-blog.csdn.net/20160912225104988)
如果id相同则是修改操作
根据id删除
结果
![](https://img-blog.csdn.net/20160912225540899)
Solr还有很多高级功能。
目前存在的问题是只能是字符串,而且是字符串数组。
还有自定义schema好好研究一下。
http://101.110.118.72/archive.apache.org/dist/lucene/solr/ref-guide/apache-solr-ref-guide-6.1.pdf
solr.cmd start
SolrClient
主要通过SolrClient来连接到Solr服务器SolrClient有4个实现类
CloudSolrClient
SolrJ client class to communicate with SolrCloud. Instances of this class communicate with Zookeeper to discover Solr endpoints for SolrCloud collections, and then use the {@link BHttpSolrClient} to issue requests.
用来连接到SolrCloud的
ConcurrentUpdateSolrClient
ConcurrentUpdateSolrClient buffers all added documents and writes them into open HTTP connections. This class is thread safe. Params from {@link UpdateRequest} are converted to http request parameters. When params change between UpdateRequests a new HTTP request is started.
线程安全的SolrClient
LBHttpSolrClient
LBHttpSolrClient or “LoadBalanced HttpSolrClient” is a load balancing wrapper around {@link HttpSolrClient}. This is useful when you have multiple Solr servers and the requests need to be Load Balanced among them.
负载均衡的HttpSolrClient
HttpSolrClient
A SolrClient implementation that talks directly to a Solr server via HTTP
通过HTTP直接连接到Solr服务器
SolrClient
javaDoc解释There are two ways to use an HttpSolrClient:
1) Pass a URL to the constructor that points directly at a particular core
SolrClient client = new HttpSolrClient("http://my-solr-server:8983/solr/core1");
QueryResponse resp = client.query(new SolrQuery(":"));
In this case, you can query the given core directly, but you cannot query any other cores or issue CoreAdmin requests with this client.
2) Pass the base URL of the node to the constructor
SolrClient client = new HttpSolrClient("http://my-solr-server:8983/solr"); QueryResponse resp = client.query("core1", new SolrQuery(":"));
In this case, you must pass the name of the required core for all queries and updates, but you may use the same client for all cores, and for CoreAdmin requests.
正确的实现方式
在查询中指出collection(我的collection为mycollections)String url = "http://localhost:8983/solr"; SolrClient client = new HttpSolrClient.Builder(url).build(); QueryResponse resp = client.query("mycollections", new SolrQuery("*:*"));
在连接url中指出collection
String url = "http://localhost:8983/solr/mycollections"; SolrClient client = new HttpSolrClient.Builder(url).build(); QueryResponse resp = client.query(new SolrQuery("*:*"));
管理员客户端的查询结果
看一下程序的运行结果
{ responseHeader = { status = 0, QTime = 0, params = { q = * : * , wt = javabin, version = 2 } }, response = { numFound = 1, start = 0, docs = [SolrDocument { id = 123456, info = [我爱北京天安门], _version_ = 1544602190025326592 } ] } }
顺利连接到服务器
添加/更新
定义一个文章实体,包括id,标题,时间,作者,内容import java.util.Arrays; import java.util.Date; import org.apache.solr.client.solrj.beans.Field; import org.springframework.data.annotation.Id; public class Article { @Id @Field private String id; @Field private String[] title; @Field private Date[] time; @Field private String[] author; @Field private String[] info; // 省略getter和setter }
初始化
private static SolrClient client; private static String url; static { url = "http://localhost:8983/solr/mycollections"; client = new HttpSolrClient.Builder(url).build(); }
添加/更新方法
/** * 保存或者更新solr数据 * * @param res */ public static boolean saveSolrResource(Article article) { DocumentObjectBinder binder = new DocumentObjectBinder(); SolrInputDocument doc = binder.toSolrInputDocument(article); try { client.add(doc); System.out.println(client.commit()); } catch (Exception e) { e.printStackTrace(); return false; } return true; }
测试
Article article = new Article(); article.setId("123456"); article.setTitle(new String[] {"测试solr"}); article.setAuthor(new String[]{"程高伟"}); article.setTime(new Date[]{new Date()}); article.setInfo(new String[]{"The Files screen lets you browse & view the various configuration files"}); saveSolrResource(article);
如果id相同则是修改操作
删除
/** * 删除solr 数据 * * @param id */ public static boolean removeSolrData(String id) { try { client.deleteById(id); client.commit(); } catch (Exception e) { e.printStackTrace(); return false; } return true; }
根据id删除
查询
SolrQuery query = new SolrQuery(); query.setQuery("程高伟"); QueryResponse rsp = client.query(query); List<Article> articleList = rsp.getBeans(Article.class); System.out.println(articleList);
结果
Solr还有很多高级功能。
目前存在的问题是只能是字符串,而且是字符串数组。
还有自定义schema好好研究一下。
参考文献
http://wiki.apache.org/solr/Solrj#Reading_Data_from_Solrhttp://101.110.118.72/archive.apache.org/dist/lucene/solr/ref-guide/apache-solr-ref-guide-6.1.pdf
相关文章推荐
- Solr学习笔记四--solrj的使用(更新文档)
- Solr学习笔记五--solrj的使用(查询文档)
- solr学习笔记(3)--使用solrj调用solr服务
- Solr学习笔记---部署Solr到Tomcat上,可视化界面的介绍和使用,Solr的基本内容介绍,SolrJ的使用
- solr学习笔记 -- day03 使用solrJ客户端
- Solrj 7.0.1 学习总结(一)使用solrj进行文档提交
- 搜索学习--Solr全文搜索服务器的基本使用(一)——SolrJ的使用
- 学习淘淘商城第五十课(使用SolrJ管理Solr集群)
- solr学习笔记 -- day05 solrJ的使用
- mysql使用学习的帮助文档
- 使用solrj和EasyNet.Solr进行原子更新
- 使用Solr构建企业级的全文检索(四)---------写入文档
- 使用solrj和EasyNet.Solr进行原子更新
- 使用solrj和EasyNet.Solr进行原子更新
- 使用solrj操作solr
- 韩顺平_PHP程序员玩转算法公开课(第一季)10_使用栈完成高级计算器(2)_学习笔记_源代码图解_PPT文档整理
- 韩顺平_php从入门到精通_视频教程_第13讲_选择器使用细节_块元素和行内元素_盒子模型_盒子模型经典应用①_学习笔记_源代码图解_PPT文档整理
- 如何使用官方文档--一项很重要的能力【不断学习】
- 韩顺平_PHP程序员玩转算法公开课(第一季)07_使用数组实现堆栈_学习笔记_源代码图解_PPT文档整理
- 学习使用solr时遇到的一些问题,记录