Solr学习笔记(3) —— SolrJ管理索引库&集群
一、什么是SolrJ
solrj是访问Solr服务的java客户端,提供索引和搜索的请求方法,SolrJ通常嵌入在业务系统中,通过SolrJ的API接口操作Solr服务,如下图:

二、SolrJ的基本操作
2.1 添加文档
- 实现步骤:
第一步:创建一个java工程
第二步:导入相关jar包

第三步:和Solr服务器建立连接。HttpSolrServer对象建立连接
第四步:创建一个SolrInputDocument对象,然后添加域
第五步:将SolrInputDocument添加到索引库
第六步:提交
- 代码实现
//向索引库中添加索引
@Test
public void addDocument() throws Exception {
//和solr服务器创建连接
//参数:solr服务器的地址
SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr");
//创建一个文档对象
SolrInputDocument document = new SolrInputDocument();
//向文档中添加域
//第一个参数:域的名称,域的名称必须是在schema.xml中定义的
//第二个参数:域的值
document.addField("id", "c0001");
document.addField("title_ik", "使用solrJ添加的文档");
document.addField("content_ik", "文档的内容");
document.addField("product_name", "商品名称");
//把document对象添加到索引库中
solrServer.add(document);
//提交修改
solrServer.commit();
}
2.2 删除文档
根据id删除
//删除文档,根据id删除
@Test
public void deleteDocumentByid() throws Exception {
//创建连接
SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr");
//根据id删除文档
solrServer.deleteById("c0001");
//提交修改
solrServer.commit();
}根据查询删除
//根据查询条件删除文档
@Test
public void deleteDocumentByQuery() throws Exception {
//创建连接
SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr");
//根据查询条件删除文档
solrServer.deleteByQuery("*:*");
//提交修改
solrServer.commit();
}
2.3 修改文档
在solrJ中修改没有对应的update方法,只有add方法,只需要添加一条新的文档,和被修改的文档id一致就,可以修改了。本质上就是先删除后添加。
2.4 查询文档
- 简单查询
//查询索引
@Test
public void queryIndex() throws Exception {
//创建连接
SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr");
//创建一个query对象
SolrQuery query = new SolrQuery();
//设置查询条件
query.setQuery("*:*");
//执行查询
QueryResponse queryResponse = solrServer.query(query);
//取查询结果
SolrDocumentList solrDocumentList = queryResponse.getResults();
//共查询到商品数量
System.out.println("共查询到商品数量:" + solrDocumentList.getNumFound());
//遍历查询的结果
for (SolrDocument solrDocument : solrDocumentList) {
System.out.println(solrDocument.get("id"));
System.out.println(solrDocument.get("product_name"));
System.out.println(solrDocument.get("product_price"));
System.out.println(solrDocument.get("product_catalog_name"));
System.out.println(solrDocument.get("product_picture"));
}
} - 复杂查询(其中包含查询、过滤、分页、排序、高亮显示等处理)
//复杂查询索引
@Test
public void queryIndex2() throws Exception {
//创建连接
SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr");
//创建一个query对象
SolrQuery query = new SolrQuery();
//设置查询条件
query.setQuery("钻石");
//过滤条件
query.setFilterQueries("product_catalog_name:幽默杂货");
//排序条件
query.setSort("product_price", ORDER.asc);
//分页处理
query.setStart(0);
query.setRows(10);
//结果中域的列表
query.setFields("id","product_name","product_price","product_catalog_name","product_picture");
//设置默认搜索域
query.set("df", "product_keywords");
//高亮显示
query.setHighlight(true);
//高亮显示的域
query.addHighlightField("product_name");
//高亮显示的前缀
query.setHighlightSimplePre("<em>");
//高亮显示的后缀
query.setHighlightSimplePost("</em>");
//执行查询
QueryResponse queryResponse = solrServer.query(query);
//取查询结果
SolrDocumentList solrDocumentList = queryResponse.getResults();
//共查询到商品数量
System.out.println("共查询到商品数量:" + solrDocumentList.getNumFound());
//遍历查询的结果
for (SolrDocument solrDocument : solrDocumentList) {
System.out.println(solrDocument.get("id"));
//取高亮显示
String productName = "";
Map<String, Map<String, List<String>>> highlighting = queryResponse.getHighlighting();
List<String> list = highlighting.get(solrDocument.get("id")).get("product_name");
//判断是否有高亮内容
if (null != list) {
productName = list.get(0);
} else {
productName = (String) solrDocument.get("product_name");
} System.out.println(productName);
System.out.println(solrDocument.get("product_price"));
System.out.println(solrDocument.get("product_catalog_name"));
System.out.println(solrDocument.get("product_picture")); }
}
三、使用SolrJ管理集群
3.1 添加文档
使用步骤:
第一步:把solrJ相关的jar包添加到工程中。
第二步:创建一个SolrServer对象,需要使用CloudSolrServer子类。构造方法的参数是zookeeper的地址列表。
第三步:需要设置DefaultCollection属性。
第四步:创建一SolrInputDocument对象。
第五步:向文档对象中添加域
第六步:把文档对象写入索引库。
第七步:提交。
@Test
public void testSolrCloudAddDocument() throws Exception {
// 第一步:把solrJ相关的jar包添加到工程中。
// 第二步:创建一个SolrServer对象,需要使用CloudSolrServer子类。构造方法的参数是zookeeper的地址列表。
// 参数是zookeeper的地址列表,使用逗号分隔
CloudSolrServer solrServer = new CloudSolrServer("192.168.25.135:2181,192.168.25.135:2182,192.168.25.135:2183");
// 第三步:需要设置DefaultCollection属性。
solrServer.setDefaultCollection("collection2");
// 第四步:创建一SolrInputDocument对象。
SolrInputDocument document = new SolrInputDocument();
// 第五步:向文档对象中添加域
document.addField("id", "test01");
document.addField("item_title", "测试商品");
document.addField("item_price", "100");
// 第六步:把文档对象写入索引库。
solrServer.add(document);
// 第七步:提交。
solrServer.commit();
}
3.2 查询文档
创建一个CloudSolrServer对象,其他处理和单机版一致。
@Test
public void testSolrCloudQueryDocument() throws Exception {
CloudSolrServer cloudSolrServer = new CloudSolrServer("192.168.25.135:2181,192.168.25.135:2182,192.168.25.135:2183");
cloudSolrServer.setDefaultCollection("collection2");
// 创建一个query对象
SolrQuery query = new SolrQuery();
// 设置查询条件
query.setQuery("*:*");
// 执行查询
QueryResponse queryResponse = cloudSolrServer.query(query);
// 取查询结果
SolrDocumentList solrDocumentList = queryResponse.getResults();
// 共查询到商品数量
System.out.println("共查询到商品数量:"+solrDocumentList.getNumFound());
// 遍历查询结果
for (SolrDocument solrDocument : solrDocumentList) {
System.out.println(solrDocument.get("id"));
System.out.println(solrDocument.get("item_title"));
System.out.println(solrDocument.get("item_price"));
}
}
四、把搜索功能切换到集群版
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx4.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util4.2.xsd"> <!-- 单机版solr服务配置 -->
<!-- <bean id="httpSolrServer" class="org.apache.solr.client.solrj.impl.HttpSolrServer">
<constructor-arg name="baseURL" value="http://192.168.25.135:8080/solr"></constructor-arg>
</bean> -->
<!-- 集群版solr服务 -->
<bean id="cloudSolrServer" class="org.apache.solr.client.solrj.impl.CloudSolrServer">
<constructor-arg name="zkHost" value="192.168.25.135:2181,192.168.25.135:2182,192.168.25.135:2183"></constructor-arg>
<property name="defaultCollection" value="collection2"></property>
</bean>
</beans>
Solr学习笔记(3) —— SolrJ管理索引库&集群的更多相关文章
- JAVAEE——Solr:安装及配置、后台管理索引库、 使用SolrJ管理索引库、仿京东的电商搜索案例实现
1 学习回顾 1. Lucene 是Apache开源的全文检索的工具包 创建索引 查询索引 2. 遇到问题? 文件名 及文件内容 顺序扫描法 全文检索 3. 什么是全文检索? 这种先创建索引 再 ...
- solrj管理索引库
solrj管理索引库 1.1. 什么是solrJ solrj是访问Solr服务的java客户端,提供索引和搜索的请求方法,SolrJ通常在嵌入在业务系统中,通过SolrJ的API接口操作Solr服务, ...
- Dubbo入门到精通学习笔记(十七):FastDFS集群的安装、FastDFS集群的配置
文章目录 FastDFS集群的安装 FastDFS 介绍(参考:http://www.oschina.net/p/fastdfs) FastDFS 上传文件交互过程: FastDFS 下载文件交互过程 ...
- Docker 与 K8S学习笔记(二十三)—— Kubernetes集群搭建
小伙伴们,好久不见,这几个月实在太忙,所以一直没有更新,今天刚好有空,咱们继续k8s的学习,由于我们后面需要深入学习Pod的调度,所以我们原先使用MiniKube搭建的实验环境就不能满足我们的需求了, ...
- ELK学习笔记之简单适用的ES集群监控工具cerebro安装使用
安装指导及使用简介 1. 下载安装包: https://github.com/lmenezes/cerebro/releases/download/v0.7.3/cerebro-0.7.3. ...
- Nginx网络架构实战学习笔记(六):服务器集群搭建、集群性能测试
文章目录 服务器集群搭建 Nginx---->php-fpm之间的优化 302机器 202机器 压力测试 搭建memcached.mysql(数据准备) 今晚就动手-.- 集群性能测试 服务器集 ...
- Dubbo入门到精通学习笔记(十三):ZooKeeper集群的安装、配置、高可用测试、升级、迁移
文章目录 ZooKeeper集群的安装.配置.高可用测试 ZooKeeper 与 Dubbo 服务集群架构图 1. 修改操作系统的/etc/hosts 文件,添加 IP 与主机名映射: 2. 下载或上 ...
- 【Kubernetes学习笔记】-kubeadm 手动搭建kubernetes 集群
目录 K8S 组件构成 环境准备 (以ubuntu系统为例) 1. kubernetes集群机器 2. 安装 docker. kubeadm.kubelet.kubectl 2.1 在每台机器上安装 ...
- Windows系统环境下Solr之Java实战(三)使用solrJ管理索引库
https://www.cnblogs.com/zhuxiaojie/p/5764680.html https://www.cnblogs.com/xieyupeng/p/9317158.html
随机推荐
- 问题:sqlserver 跨服务器连接;结果:Sql Server 跨服务器连接
Sql Server 跨服务器连接 用openrowset连接远程SQL或插入数据 --如果只是临时访问,可以直接用openrowset --查询示例 select * from openrowset ...
- xcode中用pods管理第三方库<转>
安装pods :http://www.cnblogs.com/wangluochong/p/5567082.html 史上最详细的CocoaPods安装教程 --------------------- ...
- javaScript之动态样式
动态添加样式可以实现更好的视觉效果和交互效果,下面就介绍一下如何动态和删除样式: 方法一.使用obj.className来修改样式表的类名 obj.className = “style1”; ...
- CentOS6.5 增加交换空间
在CentOS 6.5安装Oracle 11g的时候,提示交换空间不足,百度来下,找到来增加交换空间的方法,特此记录一下 增加交换空间有两种方法: 严格的说,在系统安装完后只有一种方法可以增加swap ...
- 【274】Python 相关问题
一.中文编码 参考:Python 中文编码 Python中默认的编码格式是 ASCII 格式,在没修改编码格式时无法正确打印汉字,所以在读取中文时会报错. 解决方法为只要在文件开头加入如下代码,任 ...
- @SuppressWarnings("unused")注解的作用
JDK5.0后的新特性,你在使用IDE如eclipse的时候,当你定义了一个变量如int a=0;但是你后面根本就没有使用到这个变量,这一行的前面会有一个黄色的警告标志,你将鼠标移动到上面会提示“这个 ...
- JAVA基础知识总结12(多线程)
进程:正在进行中的程序.其实进程就是一个应用程序运行时的内存分配空间. 线程:其实就是进程中一个程序执行控制单元,一条执行路径.进程负责的是应用程序的空间的标示.线程负责的是应用程序的执行顺序. 一个 ...
- python笔记--4--面向对象
面向对象 Python中对象的概念很广泛,Python中的一切内容都可以称为对象,除了数字.字符串.列表.元组.字典.集合.range对象.zip对象等等,函数也是对象,类也是对象. 在Python中 ...
- Oracle pl/sql 显示游标和隐式游标
显示游标 一.定义语法: CURSOR <游标名> IS <SELECT 语句> [FOR UPDATE | FOR UPDATE ...
- 解析IFC数据并存储到关系型数据库
即系IFC数据并存储到关系型数据库中,目前解析的IFC文件是两亿多行,构件数量120万