1 配置中文分词器

1.1 准备IK中文分词器

(1) 复制IK解压目录中的jar包: IKAnalyzer2012FF_u1.jar. 可以在 我的GitHub 中下载, 文件是IK Analyzer 2012FF_hf1.zip.

(2) 粘贴到tomcat/webapps/solr/WEB-INF/lib目录.

(3) 复制IK解压目录中的配置文件:

(4) 粘贴到tomcat/webapps/solr/WEB-INF/classes目录.

1.2 配置schema.xml文件

(1) 加入使用IK分词器的域类型

<!--加入使用ik分词器的域类型-->
<fieldType name="text_ik" class="solr.TextField">
<analyzer class="org.wltea.analyzer.lucene.IKAnalyzer" />
</fieldType>

(2) 加入使用IK分词器的域

<!--加入使用ik分词器的域-->
<field name="content_ik" type="text_ik"
indexed="false" stored="true" multiValued="true"/>

1.3 重启Tomcat并测试

选择任意Core, 然后在菜单栏里选择[Analysis], 输入中文语句, 进行分词测试:

2 配置业务域

需求引入: 假设现在要使用Solr完成电商网站商品数据的搜索, 需要将保存在关系数据库中的商品数据导入到Solr索引库中.

2.1 准备商品数据

DROP DATABASE IF EXISTS `solr`;
CREATE DATABASE `solr`;
USE `solr`; SET FOREIGN_KEY_CHECKS=0; DROP TABLE IF EXISTS `products`;
CREATE TABLE `products` (
`pid` int(11) NOT NULL AUTO_INCREMENT COMMENT '商品编号',
`name` varchar(255) DEFAULT NULL COMMENT '商品名称',
`catalog` int(11) DEFAULT NULL COMMENT '商品分类ID',
`catalog_name` varchar(50) DEFAULT NULL COMMENT '商品分类名称',
`price` double DEFAULT NULL COMMENT '价格',
`number` int(11) DEFAULT NULL COMMENT '数量',
`description` longtext COMMENT '商品描述',
`picture` varchar(255) DEFAULT NULL COMMENT '图片名称',
`release_time` datetime DEFAULT NULL COMMENT '上架时间',
PRIMARY KEY (`pid`)
) ENGINE=InnoDB AUTO_INCREMENT=6126 DEFAULT CHARSET=utf8;

测试数据较大, 具体的SQL文件可以在 我的GitHub 中下载, 文件是Solr使用IK分词器的表数据.sql.

2.2 配置商品业务域

说明: 分析商品数据库表, 确定哪些字段需要在Solr中建立索引和存储.

字段: pid, name, catalog, catalog_name, price, description, picture

(1) 商品Id(直接使用Solr的id域):

<field name="id" type="string" indexed="true" stored="true" required="true" multiValued="true"/>

(2) 商品名称(若要用于高亮显示, 必须设置stored="true"):

<field name="product_name" type="text_ik" indexed="true" stored="true" />

(3) 商品分类id:

<field name="product_catalog" type="int" indexed="true" stored="true" />

(4) 商品分类名称(String类型, 表示整体匹配, 不作分词):

<field name="product_catalog_name" type="string" indexed="true" stored="true" />

(5) 商品价格:

<field name="product_price" type="double" indexed="true" stored="true" />

(6) 商品描述:

<field name="product_description" type="text_ik" indexed="true" stored="false" />

(7) 商品图片:

<field name="product_picture" type="string" indexed="false" stored="true" />

(8) 配置商品复制域(stored="true", 实际开发中multiValued="true"的field不需要存储, 这里存储便于观察效果):

<!-- 加入商品搜索复制域, 即将商品名、商品类型和描述都作为搜索关键词提供搜索 -->
<field name="product_keywords" type="text_ik" indexed="true" stored="true" multiValued="true"/>
<copyField source="product_name" dest="product_keywords"/>
<copyField source="product_catalog_name" dest="product_keywords"/>
<copyField source="product_description" dest="product_keywords"/>

2.3 配置schema.xml文件

注意: 这里id使用Solr默认的id域(一定要有主键, 没有则需要将默认的id域删除, 也可更改id生成策略. 尝试过未在库中设置主键而此文件中的id域未删除也未重写, 此时可以建立索引, 却无法检索到结果(⊙﹏⊙)):

2.4 重新启动Tomcat并查看配置

选中任意一个core, 选择Analysis, 在 [Fieldname / FieldType] 处查看, 观察配置是否成功:

版权声明

作者: 马瘦风

出处: 博客园 马瘦风的博客

您的支持是对博主的极大鼓励, 感谢您的阅读.

本文版权归博主所有, 欢迎转载, 但请保留此段声明, 并在文章页面明显位置给出原文链接, 否则博主保留追究相关人员法律责任的权利.

最新文章

  1. Spark的DataFrame的窗口函数使用
  2. div模态层示例
  3. 【8-23】node.js学习笔记
  4. Intel OIT demo
  5. C# 创建一个日志文件
  6. 分享9款用HTML5/CSS3制作的动物人物动画
  7. eclipse 修改设置Ctrl+Shift+F长度
  8. caret彻底的理解css的三角形【通过border】
  9. 一种比较简单的在USB U盘中访问nandflash的方法
  10. 使用Dockerfile制作自己的Docker镜像
  11. Unsupervised Learning and Text Mining of Emotion Terms Using R
  12. python编码错误
  13. C#中消息的工作流程
  14. Java tomcat Several ports (8005, 8080, 8009) required by Tomcat v9.0 Server at localhost
  15. 写面向对象的新Process
  16. Cocos Creator 使用计时器(官方文档摘录)
  17. chrome&#39;s developer console
  18. ffmpeg m3u8 转 MP4
  19. 使用SpringBoot Admin监控SpringCloud微服务
  20. Code Signal_练习题_stringsRearrangement

热门文章

  1. Centos给文件设置了777权限仍不能访问解决方案
  2. 图论之最短路径floyd算法
  3. Python-写文件
  4. Bphero-UWB 基站0 和 电脑串口数据格式定义
  5. 用python写一个非常简单的QQ轰炸机
  6. CMD 中常见命令
  7. 做个流量站-聚茶吧, 汇聚&quot;茶&quot;的地方
  8. [error] - Build path is incomplete. Cannot find class file for org/aspectj/weaver/refl
  9. JVM之垃圾回收
  10. MySQL 分区建索引