Solr 8.2 使用指南
1 Solr简介
1.1 Solr是什么
Solr是一个基于全文检索的企业级应用服务器。可以输入一段文字,通过分词检索数据。它是单独的服务,部署在 tomcat。
1.2 为什么需要Solr
问题:我们已经学过Lucene,为什么还要学习solr?
Lucene是一个工具包,不能单独运行,需要导入到java代码中。Solr可以独立运行在tomcat容器中,通过http协议,以接口的方式对外提供服务,java代码只需要专注于业务的处理就可以。

1.3 Solr目录结构说明

- bin:solr的运行脚本
- contrib:solr的一些扩展jar包,用于增强solr的功能
- dist:该目录包含build过程中产生的jar文件,以及相关的依赖文件
- example:solr工程的例子目录
- licenses:solr相关的一些许可信息
2 入门示例
2.1 需求
将数据库的数据导入 solr 中,实现查询功能
2.2 配置步骤
2.2.1 启动 solr
进入 solr 解压路径下的 bin 目录,按 shift + 鼠标右键,选择在此次打开命令行工具

输入命令: .\solr start 启动 solr 服务

使用浏览器访问 localhost:8983 即可进入后台控制页面。

2.2.2 配置 solr core
继续使用命令工具创建一个 core,core 就相当于一个 solr 的项目实例。
命令:solr create -c <core_name>

成功创建后,可以在 solr-8.2.0/server/solr/<core_name> 目录下看到自动生成的默认配置文件

创建完成后,重新进入后台控制页面,可以查看到新建的 core

2.2.3 创建java程序访问solr服务器
步骤说明:
- 采集数据
- 将数据转换成Solr文档
- 连接solr服务器,将文档写入索引库
2.2.3.1 创建项目,导入 jar 包
需要导入的包有:
- Solrj 核心包:
solr-8.2.0\dist\solr-core-8.2.0.jar - Solrj 依赖包:
solr-8.2.0\dist\solrj-lib\目录下的所有包 - JDBC 驱动包:根据数据库版本而定,我这里拷的是 mysql 8 的驱动包
项目结构:

2.2.3.2 采集数据
需求采集的字段说明:
- 参与搜索的字段:名称、价格、商品类别、描述信息
- 参与结果展示的字段:商品id、图片
(1)创建 pojo
public class Product {
private Integer pid;
private String name;
private String categoryName;
private Double price;
private String description;
private String picture;
//省略 getter、setter、constructor、toString
}
(2)创建 dao
public class ProductDao {
public List<Product> listAll() {
List<Product> products = new ArrayList<>();
//获取数据库连接
Connection conn = JdbcUtils.getConnection();
String sql = "select * from `products`";
Statement statement = null;
ResultSet resultSet = null;
try {
//创建 statement
statement = conn.createStatement();
//执行 sql 语句
resultSet = statement.executeQuery(sql);
//循环操作结果集
while (resultSet.next()) {
products.add(new Product(resultSet.getInt("pid"),
resultSet.getString("name"),
resultSet.getString("category_name"),
resultSet.getDouble("price"),
resultSet.getString("description"),
resultSet.getString("picture")));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
//关闭资源
if (null != resultSet) {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (null != statement) {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (null != conn) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
}
}
return products;
}
}
(3)将数据转换成 solr 文档, SolrInputDocument 对象
Solr是通过一个配置文件managed-schema,事先定义域的信息的,需要先定义再使用。

配置文件里面事先定义好了各种 <dynamicField>,能够根据命名动态的指定域的类型,也就是 type 属性。

而域的类型也在此做了定义,用的是 <fieldType> 标签。(可对比 lucene 理解)

其中,text-general 指定了分词器,以及一些拓展配置文件

我们可以根据需要,按照上述例子,手动的声明几个域,并使用中文分词。先将 lucene 中的 SmartChineseAnalyzer 的 jar 包拷入文件夹中

再修改 managed-schema 配置文件,添加以下内容

重启服务器后,可以看到效果

为 dao 添加 getDocuments 方法
public List<SolrInputDocument> getDocuments(List<Product> products) {
List<SolrInputDocument> documents = new ArrayList<>();
products.forEach(product -> {
SolrInputDocument document = new SolrInputDocument();
document.addField("id", product.getPid());//对应solr的uniqueKey
document.addField("product_name", product.getName());
document.addField("product_price", product.getPrice());
document.addField("product_category_name", product.getCategoryName());
document.addField("product_picture", product.getPicture());
document.addField("product_description", product.getDescription());
documents.add(document);
});
return documents;
}
创建索引库
@Test
public void createIndex() {
//1.创建 HttpSolrClient.Builder 对象,通过它创建客户端通信
HttpSolrClient.Builder builder = new HttpSolrClient.Builder("http://localhost:8983/solr");
HttpSolrClient solrClient = builder.build();
//2.通过 client 将 document 加入索引库
ProductDao dao = new ProductDao();
try {
//参数1是 solr core 的名字
solrClient.add("product", dao.getDocuments(dao.listAll()));
solrClient.commit("product");
System.out.println("创建索引库完成");
} catch (SolrServerException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
导入成功后可以在后天控制页面看到结果

2.2.3.3 搜索索引
@Test
public void queryTest() {
//1.创建 HttpSolrClient.Builder 对象,通过它创建客户端通信
HttpSolrClient.Builder builder = new HttpSolrClient.Builder("http://localhost:8983/solr");
HttpSolrClient solrClient = builder.build();
//2.创建一个map封装搜索条件
Map<String, String> queryMap = new HashMap<>();
queryMap.put("q","音乐盒");//关键字
queryMap.put("df", "product_name");//默认搜索域
//queryMap.put("sort","id asc");//结果以 id 升序排列,默认以关联度排序
queryMap.put("rows","20");//默认只有十条
//3.使用map创建 MapSolrParams 对象
SolrParams solrParams = new MapSolrParams(queryMap);
try {
//4.使用客户端进行查询
QueryResponse response = solrClient.query("product", solrParams);
//5.提取结果
SolrDocumentList documents = response.getResults();
System.out.println("一共查询到:" + documents.getNumFound() + "条结果");
//6.循环输出
documents.forEach(document ->{
System.out.println("编号" + document.get("id") + ":" + document.get("product_name"));
});
} catch (SolrServerException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
3 solr管理控制台
3.1 查询界面说明

可以根据查询界面各个关键字,设置上述代码 queryMap,实现复杂的查询功能。key 对应的就是关键字,value 就是输入框内的值。
3.2 安装DataImport插件
3.2.1 Dataimport插件说明
使用该插件后,可以在管理界面直接从数据库导入数据到索引库。(即:一个插件解决入门示例中,创建索引的全部操作)
3.2.2 安装步骤
(1)拷贝相关 jar 包到文件夹

(2)修改 \solr-8.2.0\server\solr\product\conf\solrconfig.xml 文件,增加以下代码

(3)在 \solr-8.2.0\server\solr\product\conf\ 目录下新建 DIHconfig.xml 文件,并编写以下内容
<dataConfig>
<dataSource type="JdbcDataSource"
driver="com.mysql.cj.jdbc.Driver"
url="jdbc:mysql://localhost:3306/solr?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=false"
user="root"
password="password"
/>
<document>
<entity name="product"
query="SELECT * FROM products">
<field column="pid" name="id"/>
<field column="name" name="product_name"/>
<field column="price" name="product_price"/>
<field column="category_name" name="product_category_name"/>
<field column="description" name="product_description"/>
<field column="picture" name="product_picture"/>
</entity>
</document>
</dataConfig>
(4)重启 solr 服务

3.2.3 测试
(1) 清空索引库

(2)导入索引库

我的博客即将同步至腾讯云+社区,邀请大家一同入驻:https://cloud.tencent.com/developer/support-plan?invite_code=9fsyys67r6lo
Solr 8.2 使用指南的更多相关文章
- Solr版本安装部署指南
一.依赖包 1. JDK 1.6以上 2. solr-4.3.0.tgz 3. Tomcat或者jetty(注意,solr包中本身就含有jetty的启动相关内容):apache-tomcat-7 ...
- [Solr] (源) Solr与MongoDB集成,实时增量索引
一. 概述 大量的数据存储在MongoDB上,需要快速搜索出目标内容,于是搭建Solr服务. 另外一点,用Solr索引数据后,可以把数据用在不同的项目当中,直接向Solr服务发送请求,返回xml.js ...
- Solr与MongoDB集成,实时增量索引
Solr与MongoDB集成,实时增量索引 一. 概述 大量的数据存储在MongoDB上,需要快速搜索出目标内容,于是搭建Solr服务. 另外一点,用Solr索引数据后,可以把数据用在不同的项目当中, ...
- Solr.NET快速入门(三)【高亮显示】
此功能会"高亮显示"匹配查询的字词(通常使用标记),包括匹配字词周围的文字片段. 要启用高亮显示,请包括HighlightingParameters QueryOptions对象, ...
- Solr使用入门指南
本文转自http://chuanliang2007.spaces.live.com/blog/cns!E5B7AB2851A4C9D2!499.entry?wa=wsignin1.0 由于搜索引擎功能 ...
- 企业级搜索引擎Solr使用入门指南
由于搜索引擎功能在门户社区中对提高用户体验有着重在门户社区中涉及大量需要搜索引擎的功能需求,目前在实现搜索引擎的方案上有集中方案可供选择: 基于Lucene自己进行封装实现站内搜索. 工作量及扩展性都 ...
- Solr集群的搭建以及使用(内涵zookeeper集群的搭建指南)
1 什么是SolrCloud SolrCloud(solr 云)是Solr提供的分布式搜索方案,当你需要大规模,容错,分布式索引和检索能力时使用 SolrCloud.当一个系统的索引数据量少的时候 ...
- Solr入门指南
本文转自http://chuanliang2007.spaces.live.com/blog/cns!E5B7AB2851A4C9D2!499.entry?wa=wsignin1.0 因为搜索引擎功能 ...
- 全文检索引擎Solr 指南
全文检索引擎Solr系列:第一篇:http://t.cn/RP004gl.第二篇:http://t.cn/RPHDjk7 .第三篇:http://t.cn/RPuJt3T
随机推荐
- 第2组 Alpha冲刺(3/4)
17-材料-黄智(252342126) 22:10:46 队名:十一个憨批 组长博客 作业博客 组长黄智 过去两天完成的任务:写博客,复习C语言 GitHub签入记录 接下来的计划:构思游戏实现 还剩 ...
- c实现单向链表
实现一个单向链表的:创建.插入.删除.排序(冒泡).逆向.搜索中间节点 #include <iostream> #include <stdio.h> #include < ...
- 内存管理2-set方法的内存管理
1.对象之间的内存管理: 每个学生都有一本书 book类 @price 学生类 @age @book -------------------- #import "book.h" ...
- static关键字的用法小结
static:是一个修饰符,用于修饰成员(成员变量,成员函数). 当成员被静态修饰后,就多了一个调用方式,除了可以被对象调用外,还可以直接被类名调用,写法:类名.静态成员 static特点: 1.随着 ...
- mysql启动关闭脚本
#!/bin/sh mysql_port= mysql_username="root" mysql_password="" function_start_mys ...
- go区分操作系统
package main import ( "fmt" "runtime" ) func main() { fmt.Println("Go runs ...
- 使用PyMySQL连接MySQL错误
使用PyMySQL连接MySQL错误 之前写了一个小项目,今天突然想起来,准备优化一下,但是原本好好的项目竟然跑不起来了 emmm....我真的啥都没干呀 具体错误是这样的: Traceback (m ...
- arcgis python获得别名
import arcpy # Create a Describe object from the GDB table. # desc = arcpy.Describe(r"C:\Users\ ...
- hadoop查看文件大小
hadoop fs -du /yj/input/ 列出input下所有文件的大小,以B为单位 #!/bin/sh #echo "hadoop fs -du /" hadoop fs ...
- 在基于Android以及Jetson TK平台上如何写32位的Thumb-2指令
由于Android以及Jetson TK的编译工具链中的汇编器仍然不支持大部分的32位Thumb-2指令,比如add.w,因此我们只能通过手工写机器指令码来实现想要的指令.下面我将简单地介绍如何在AR ...