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服务器

步骤说明:

  1. 采集数据
  2. 将数据转换成Solr文档
  3. 连接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
  1. public class Product {
  2. private Integer pid;
  3. private String name;
  4. private String categoryName;
  5. private Double price;
  6. private String description;
  7. private String picture;
  8. //省略 getter、setter、constructor、toString
  9. }
(2)创建 dao
  1. public class ProductDao {
  2. public List<Product> listAll() {
  3. List<Product> products = new ArrayList<>();
  4. //获取数据库连接
  5. Connection conn = JdbcUtils.getConnection();
  6. String sql = "select * from `products`";
  7. Statement statement = null;
  8. ResultSet resultSet = null;
  9. try {
  10. //创建 statement
  11. statement = conn.createStatement();
  12. //执行 sql 语句
  13. resultSet = statement.executeQuery(sql);
  14. //循环操作结果集
  15. while (resultSet.next()) {
  16. products.add(new Product(resultSet.getInt("pid"),
  17. resultSet.getString("name"),
  18. resultSet.getString("category_name"),
  19. resultSet.getDouble("price"),
  20. resultSet.getString("description"),
  21. resultSet.getString("picture")));
  22. }
  23. } catch (SQLException e) {
  24. e.printStackTrace();
  25. } finally {
  26. //关闭资源
  27. if (null != resultSet) {
  28. try {
  29. resultSet.close();
  30. } catch (SQLException e) {
  31. e.printStackTrace();
  32. } finally {
  33. if (null != statement) {
  34. try {
  35. statement.close();
  36. } catch (SQLException e) {
  37. e.printStackTrace();
  38. } finally {
  39. if (null != conn) {
  40. try {
  41. conn.close();
  42. } catch (SQLException e) {
  43. e.printStackTrace();
  44. }
  45. }
  46. }
  47. }
  48. }
  49. }
  50. }
  51. return products;
  52. }
  53. }
(3)将数据转换成 solr 文档, SolrInputDocument 对象

Solr是通过一个配置文件managed-schema,事先定义域的信息的,需要先定义再使用。

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

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

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

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

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

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

为 dao 添加 getDocuments 方法

  1. public List<SolrInputDocument> getDocuments(List<Product> products) {
  2. List<SolrInputDocument> documents = new ArrayList<>();
  3. products.forEach(product -> {
  4. SolrInputDocument document = new SolrInputDocument();
  5. document.addField("id", product.getPid());//对应solr的uniqueKey
  6. document.addField("product_name", product.getName());
  7. document.addField("product_price", product.getPrice());
  8. document.addField("product_category_name", product.getCategoryName());
  9. document.addField("product_picture", product.getPicture());
  10. document.addField("product_description", product.getDescription());
  11. documents.add(document);
  12. });
  13. return documents;
  14. }

创建索引库

  1. @Test
  2. public void createIndex() {
  3. //1.创建 HttpSolrClient.Builder 对象,通过它创建客户端通信
  4. HttpSolrClient.Builder builder = new HttpSolrClient.Builder("http://localhost:8983/solr");
  5. HttpSolrClient solrClient = builder.build();
  6. //2.通过 client 将 document 加入索引库
  7. ProductDao dao = new ProductDao();
  8. try {
  9. //参数1是 solr core 的名字
  10. solrClient.add("product", dao.getDocuments(dao.listAll()));
  11. solrClient.commit("product");
  12. System.out.println("创建索引库完成");
  13. } catch (SolrServerException e) {
  14. e.printStackTrace();
  15. } catch (IOException e) {
  16. e.printStackTrace();
  17. }
  18. }

导入成功后可以在后天控制页面看到结果

2.2.3.3 搜索索引

  1. @Test
  2. public void queryTest() {
  3. //1.创建 HttpSolrClient.Builder 对象,通过它创建客户端通信
  4. HttpSolrClient.Builder builder = new HttpSolrClient.Builder("http://localhost:8983/solr");
  5. HttpSolrClient solrClient = builder.build();
  6. //2.创建一个map封装搜索条件
  7. Map<String, String> queryMap = new HashMap<>();
  8. queryMap.put("q","音乐盒");//关键字
  9. queryMap.put("df", "product_name");//默认搜索域
  10. //queryMap.put("sort","id asc");//结果以 id 升序排列,默认以关联度排序
  11. queryMap.put("rows","20");//默认只有十条
  12. //3.使用map创建 MapSolrParams 对象
  13. SolrParams solrParams = new MapSolrParams(queryMap);
  14. try {
  15. //4.使用客户端进行查询
  16. QueryResponse response = solrClient.query("product", solrParams);
  17. //5.提取结果
  18. SolrDocumentList documents = response.getResults();
  19. System.out.println("一共查询到:" + documents.getNumFound() + "条结果");
  20. //6.循环输出
  21. documents.forEach(document ->{
  22. System.out.println("编号" + document.get("id") + ":" + document.get("product_name"));
  23. });
  24. } catch (SolrServerException e) {
  25. e.printStackTrace();
  26. } catch (IOException e) {
  27. e.printStackTrace();
  28. }
  29. }

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 文件,并编写以下内容

  1. <dataConfig>
  2. <dataSource type="JdbcDataSource"
  3. driver="com.mysql.cj.jdbc.Driver"
  4. url="jdbc:mysql://localhost:3306/solr?serverTimezone=UTC&amp;useUnicode=true&amp;characterEncoding=utf8&amp;useSSL=false"
  5. user="root"
  6. password="password"
  7. />
  8. <document>
  9. <entity name="product"
  10. query="SELECT * FROM products">
  11. <field column="pid" name="id"/>
  12. <field column="name" name="product_name"/>
  13. <field column="price" name="product_price"/>
  14. <field column="category_name" name="product_category_name"/>
  15. <field column="description" name="product_description"/>
  16. <field column="picture" name="product_picture"/>
  17. </entity>
  18. </document>
  19. </dataConfig>

(4)重启 solr 服务

3.2.3 测试

(1) 清空索引库

(2)导入索引库

我的博客即将同步至腾讯云+社区,邀请大家一同入驻:https://cloud.tencent.com/developer/support-plan?invite_code=9fsyys67r6lo

Solr 8.2 使用指南的更多相关文章

  1. Solr版本安装部署指南

    一.依赖包 1.  JDK 1.6以上 2.  solr-4.3.0.tgz 3.  Tomcat或者jetty(注意,solr包中本身就含有jetty的启动相关内容):apache-tomcat-7 ...

  2. [Solr] (源) Solr与MongoDB集成,实时增量索引

    一. 概述 大量的数据存储在MongoDB上,需要快速搜索出目标内容,于是搭建Solr服务. 另外一点,用Solr索引数据后,可以把数据用在不同的项目当中,直接向Solr服务发送请求,返回xml.js ...

  3. Solr与MongoDB集成,实时增量索引

    Solr与MongoDB集成,实时增量索引 一. 概述 大量的数据存储在MongoDB上,需要快速搜索出目标内容,于是搭建Solr服务. 另外一点,用Solr索引数据后,可以把数据用在不同的项目当中, ...

  4. Solr.NET快速入门(三)【高亮显示】

    此功能会"高亮显示"匹配查询的字词(通常使用标记),包括匹配字词周围的文字片段. 要启用高亮显示,请包括HighlightingParameters QueryOptions对象, ...

  5. Solr使用入门指南

    本文转自http://chuanliang2007.spaces.live.com/blog/cns!E5B7AB2851A4C9D2!499.entry?wa=wsignin1.0 由于搜索引擎功能 ...

  6. 企业级搜索引擎Solr使用入门指南

    由于搜索引擎功能在门户社区中对提高用户体验有着重在门户社区中涉及大量需要搜索引擎的功能需求,目前在实现搜索引擎的方案上有集中方案可供选择: 基于Lucene自己进行封装实现站内搜索. 工作量及扩展性都 ...

  7. Solr集群的搭建以及使用(内涵zookeeper集群的搭建指南)

    1   什么是SolrCloud SolrCloud(solr 云)是Solr提供的分布式搜索方案,当你需要大规模,容错,分布式索引和检索能力时使用 SolrCloud.当一个系统的索引数据量少的时候 ...

  8. Solr入门指南

    本文转自http://chuanliang2007.spaces.live.com/blog/cns!E5B7AB2851A4C9D2!499.entry?wa=wsignin1.0 因为搜索引擎功能 ...

  9. 全文检索引擎Solr 指南

    全文检索引擎Solr系列:第一篇:http://t.cn/RP004gl.第二篇:http://t.cn/RPHDjk7 .第三篇:http://t.cn/RPuJt3T

随机推荐

  1. codeforces gym #101161E - ACM Tax(lca+主席树)

    题目链接: http://codeforces.com/gym/101161/attachments 题意: 给出节点数为$n$的树 有$q$次询问,输出$a$节点到$b$节点路程中,经过的边的中位数 ...

  2. Atcoder ABC 139C

    Atcoder ABC 139C 题意: 有 $ n $ 个正方形,选择一个起始位置,使得从这个位置向右的小于等于这个正方形的高度的数量最多. 解法: 简单递推. CODE: #include< ...

  3. elasticsearch _bulk api

    https://www.elastic.co/guide/cn/elasticsearch/guide/current/bulk.htmlbulk API 允许在单个步骤中进行多次 create . ...

  4. Open Live Writer 显示不出来代码着色插件解决办法

    下载地址: Open Live Writer 插件更新 下载后要把下面这5个文件,全部解除锁定(右键属性打开) Memento.OLW.Plugins.dll OLWPlugins.css OpenL ...

  5. Maven版本问题导致的 unable to import maven project, see logs for details. 问题

    新电脑安装了基础环境后,jdk,maven也都安装好了,idea安装后,导入Java项目一切正常,但是idea中code一直导入import依赖包出现问题,错误提示:unable to import ...

  6. 笔记七(编写第一个UEFI程序)

    搭建好uefi开发环境之后,在MyWorkspace文件夹中建立一个文件夹ExamplePkg; ,然后在ExamplePkg文件夹中创建HelloWorld文件夹,Include文件夹,Exampl ...

  7. 网络时间协议 (SNTP)

    sntp是简单网络时间协议(Simple Network Protocol)的客户端,可以用来查询或修正NTP服务器的时间和本地的时差. sntp可以以非交互模式运行或运行一个计划任务的脚本. snt ...

  8. ubuntu 切换默认python版本

    现在的python项目都是基于python3的了,再用ubuntu的时候默认的版本是py2的,所以想切换到py3上: 打开终端: sudo update-alternatives --install ...

  9. 前端中关于HTML标签的属性for的理解

    First:<label>的说明:1.<label>标签为input元素定义标注(标识)2.label元素不会像用户呈现任何特殊的效果,仅作为显示扩展:不过,它为鼠标用户改进了 ...

  10. springboot之rabbitmq安装与实践

    环境:腾讯云centos7 注意:rabbitmq安装插件,可能会报错.本人是主机名的问题,所以修改了主机名. vim /etc/hosts vim /etc/hostname 修改这两个文件,并重启 ...