一、准备环境
1、win10下 准备好JDK1.8、Tomcat8.5环境。
2、下载solr http://mirrors.hust.edu.cn/apache/lucene/solr/7.4.0/
3、下载HanLP http://hanlp.linrunsoft.com/services.html
二、整合Tomcat
1、 将solr-7.4.0\server\solr-webapp\webapp文件夹,复制到tomcat的webapps文件夹下,修改webapp名称为solr
2、 将solr-7.4.0\server\lib\ext目录下的所有jar包,以及solr-7.4.0\server\lib目录下以metrics开头的jar、gmetric4j-1.0.7.jar复制到上一步所建好的solr文件夹中的WEB-INF下的lib文件夹中(Tomcat安装目录\webapps\solr\WEB-INF\lib)
3、 复制solr-7.4.0/server/resources下的log4j2.xml日志配置文件,到apache-tomcat/webapps/solr/WEB-INF/下的classes文件夹中。(classes文件夹如果没有需要自己创建)
4、 在任意地方创建文件夹solr_home,把solr-7.4.1\server下solr下的所有文件复制到这个solr_home下
5、修改配置文件Tomcat\webapps\solr\WEB-INF\web.xml,将下面配置的注释放开,黑体部分更改为你的SolrHome地址。
并注释,如果不注释,启动tomcat也访问不了solr,因为这里有权限设置
6、复制solr-7.4.1下contrib和dist文件夹至solr_home目录下。
7、在solr_home目录下,新建logs文件夹,作为solr日志存放地址。修改Tomcat\bin下的catalina.bat,增加solr.log.dir系统变量(log4j2.xml中使用了solr.log.dir变量)
set "JAVA_OPTS=%JAVA_OPTS% -Dsolr.log.dir=F:\02_Solr\solr_zhjw\solr_home\logs"
8、在solr_home目录下新建zhjw_core文件夹
并复制 solr-7.4.0\example\example-DIH\solr\db目录下conf文件夹至solr_home\zhjw_core下
9、修改zhjw_core\conf\solrconfig.xml
<!--
<lib dir="${solr.install.dir:../../../..}/contrib/extraction/lib" regex=".*\.jar" />
<lib dir="${solr.install.dir:../../../..}/dist/" regex="solr-cell-\d.*\.jar" />
<lib dir="${solr.install.dir:../../../..}/contrib/clustering/lib/" regex=".*\.jar" />
<lib dir="${solr.install.dir:../../../..}/dist/" regex="solr-clustering-\d.*\.jar" />
<lib dir="${solr.install.dir:../../../..}/contrib/langid/lib/" regex=".*\.jar" />
<lib dir="${solr.install.dir:../../../..}/dist/" regex="solr-langid-\d.*\.jar" />
<lib dir="${solr.install.dir:../../../..}/contrib/velocity/lib" regex=".*\.jar" />
<lib dir="${solr.install.dir:../../../..}/dist/" regex="solr-velocity-\d.*\.jar" />
-->
<lib dir="${solr.install.dir:../}/contrib/extraction/lib" regex=".*\.jar" />
<lib dir="${solr.install.dir:../}/dist/" regex="solr-cell-\d.*\.jar" />
<lib dir="${solr.install.dir:../}/contrib/clustering/lib/" regex=".*\.jar" />
<lib dir="${solr.install.dir:../}/dist/" regex="solr-clustering-\d.*\.jar" />
<lib dir="${solr.install.dir:../}/contrib/langid/lib/" regex=".*\.jar" />
<lib dir="${solr.install.dir:../}/dist/" regex="solr-langid-\d.*\.jar" />
<lib dir="${solr.install.dir:../}/contrib/velocity/lib" regex=".*\.jar" />
<lib dir="${solr.install.dir:../}/dist/" regex="solr-velocity-\d.*\.jar" />
<lib dir="${solr.install.dir:../}/dist/" regex="ojdbc\d.*\.jar" />
<lib dir="${solr.install.dir:../}/dist/" regex="solr-dataimporthandler\d.*\.jar" />
10、启动Tomcat,访问http://localhost:8080/solr/index.html 新建core
三、整合HanLP
1、将下载好的HanLP文件解压到F:\02_Solr\solr_zhjw\HanLP目录
2、将HanLP\hanlp-1.6.8-release下的hanlp-1.6.8.jar 和hanlp-lucene-plugin-1.1.6.jar复制到 tomcat下的solr\WEB-INF\lib中
3、将HanLP\hanlp-1.6.8-release下的hanlp.properties 复制到solr\WEB-INF\classes下
4、修改hanlp.properties文件,配置root地址
5、修改solr_home\zhjw_core\conf\managed-schema 文件,配置HanLP分词器
<fieldType name="text_HanLP" class="solr.TextField">
<analyzer type="index">
<tokenizer class="com.hankcs.lucene.HanLPTokenizerFactory" enableIndexMode="true"/>
</analyzer>
<analyzer type="query">
<!-- 切记不要在query中开启index模式 -->
<tokenizer class="com.hankcs.lucene.HanLPTokenizerFactory" enableIndexMode="false"/>
</analyzer>
</fieldType>
其他语言的fieldType 可以直接删除!
6、通过solr管理页面,查看HanLP分词器效果
四、同步mysql数据到Solr
参考:https://wiki.apache.org/solr/DataImportHandler
https://blog.csdn.net/jiyiqinlovexx/article/details/24913853
http://iamyida.iteye.com/blog/2214600
1、将java连接mysql数据库jar包(mysql-connector-java-5.1.46.jar)及solr-7.4.0\dist下的solr-dataimporthandler-7.4.0.jar、solr-dataimporthandler-extras-7.4.0.jar 复制到tomcat 下solr\WEB-INF\lib中
2、配置solr_home\zhjw_core\conf\managed-schema(配置和数据库对应的字段)data_id ------到----data_time
<?xml version="1.0" encoding="UTF-8" ?>
<schema name="default-config" version="1.6">
<!--默认字段,必须有-->
<field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false" />
<field name="_version_" type="plong" indexed="false" stored="false"/>
<field name="_root_" type="string" indexed="true" stored="false" docValues="false" />
<field name="text" type="text_HanLP" indexed="true" stored="false" multiValued="true"/>
<field name="data_id" type="string" indexed="true" stored="false"/>
<field name="data_title" type="text_HanLP" indexed="true" stored="true" />
<field name="data_text" type="text_HanLP" indexed="true" stored="true" />
<field name="data_author" type="string" indexed="true" stored="false" />
<field name="data_time" type="string" indexed="true" stored="false" />
<!-- 唯一键对应字段 -->
<uniqueKey>id</uniqueKey>
<!-- The StrField type is not analyzed, but indexed/stored verbatim. -->
<fieldType name="string" class="solr.StrField" sortMissingLast="true" docValues="true" />
<fieldType name="strings" class="solr.StrField" sortMissingLast="true" multiValued="true" docValues="true" />
<!-- boolean type: "true" or "false" -->
<fieldType name="boolean" class="solr.BoolField" sortMissingLast="true"/>
<fieldType name="booleans" class="solr.BoolField" sortMissingLast="true" multiValued="true"/>
<!--
Numeric field types that index values using KD-trees.
Point fields don't support FieldCache, so they must have docValues="true" if needed for sorting, faceting, functions, etc.
-->
<fieldType name="pint" class="solr.IntPointField" docValues="true"/>
<fieldType name="pfloat" class="solr.FloatPointField" docValues="true"/>
<fieldType name="plong" class="solr.LongPointField" docValues="true"/>
<fieldType name="pdouble" class="solr.DoublePointField" docValues="true"/>
<fieldType name="pints" class="solr.IntPointField" docValues="true" multiValued="true"/>
<fieldType name="pfloats" class="solr.FloatPointField" docValues="true" multiValued="true"/>
<fieldType name="plongs" class="solr.LongPointField" docValues="true" multiValued="true"/>
<fieldType name="pdoubles" class="solr.DoublePointField" docValues="true" multiValued="true"/>
<fieldType name="random" class="solr.RandomSortField" indexed="true"/>
<!-- KD-tree versions of date fields -->
<fieldType name="pdate" class="solr.DatePointField" docValues="true"/>
<fieldType name="pdates" class="solr.DatePointField" docValues="true" multiValued="true"/>
<!--Binary data type. The data should be sent/retrieved in as Base64 encoded Strings -->
<fieldType name="binary" class="solr.BinaryField"/>
<!-- HanLP分词器配合 -->
<fieldType name="text_HanLP" class="solr.TextField">
<analyzer type="index">
<tokenizer class="com.hankcs.lucene.HanLPTokenizerFactory" enableIndexMode="true"/>
</analyzer>
<analyzer type="query">
<!-- 切记不要在query中开启index模式 -->
<tokenizer class="com.hankcs.lucene.HanLPTokenizerFactory" enableIndexMode="false"/>
</analyzer>
</fieldType>
</schema>
3、配置solr_home\zhjw_core\conf\db-data-config.xml(数据库同步sql文件)
<?xml version="1.0" encoding="utf-8"?>
<dataConfig>
<!--数据源相关配置-->
<dataSource
name="zhjwDataSource"
type="JdbcDataSource"
driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/zhjw_ag"
user="root"
password="12456789"
batchSize="-1" />
<document>
<entity
name="yl_data"
query="select * from yl_data" >
<field column="data_id" name="id"/>
<field column="data_id" name="data_id"/>
<field column="data_title" name="data_title"/>
<field column="data_text" name="data_text"/>
<field column="data_author" name="data_author"/>
<field column="data_time" name="data_time"/>
<field column="data_title" name="data_titles"/>
<field column="data_text" name="data_texts"/>
</entity>
</document>
</dataConfig>
4、通过solr控制台全量导入数据
五、设置定时同步
solr6.1.0版本jar已经不再提供读取自动索引配置文件路径的方法,因此apache-solr-dataimportscheduler-1.0内调用该方法的所有操作都无法实现于是需要下载apache-solr-dataimportscheduler-1.0-with-source.jar并修改源码重新编译生成符合我们需求的apache-solr-dataimportscheduler-1.4 (网上都是这么说的)
1、直接在CSDN上下载别人修改好的jar,把下载好的apache-solr-dataimportscheduler-1.4.jar 复制到tomcat 下solr\WEB-INF\lib中
2、在solr_home下新建conf文件夹,在conf文件夹下新建dataimport.properties
#################################################
# #
# dataimport scheduler properties #
# #
#################################################
# to sync or not to sync
# 是否开启自动同步
syncEnabled=1
# which cores to schedule
# in a multi-core environment you can decide which cores you want syncronized
# leave empty or comment it out if using single-core deployment
# 需要自动更新的core名称,可以是多个,用逗号隔开
syncCores=zhjw
# solr server name or IP address
# [defaults to localhost if empty]
server=localhost
# solr server port
# [defaults to 80 if empty]
port=8080
# application name/context
# [defaults to current ServletContextListener's context (app) name]
webapp=solr
# URL params [mandatory]
# remainder of URL
# 增量更新的请求参数
params=/dataimport?command=delta-import&clean=false&commit=true
# schedule interval
# number of minutes between two runs
# [defaults to 30 if empty]
# 这里配置的是2min一次
interval=2
# 重做索引的时间间隔,单位分钟,默认7200,即5天;
# 为空,为0,或者注释掉:表示永不重做索引
reBuildIndexInterval=7200
# 重做索引的参数
reBuildIndexParams=/dataimport?command=full-import&clean=true&commit=true
# 重做索引时间间隔的计时开始时间,第一次真正执行的时间=reBuildIndexBeginTime+reBuildIndexInterval*60*1000;
# 两种格式:2012-04-11 03:10:00 或者 03:10:00,后一种会自动补全日期部分为服务启动时的日期
reBuildIndexBeginTime=03:10:00
3、修改solr\WEB-INF\web.xml文件,添加listener节点
<listener>
<listener-class>
org.apache.solr.handler.dataimport.scheduler.ApplicationListener
</listener-class>
</listener>
4、修改定时同步sql
<?xml version="1.0" encoding="utf-8"?>
<dataConfig>
<!--数据源相关配置-->
<dataSource
name="zhjwDataSource"
type="JdbcDataSource"
driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://192.168.10.14:3306/zhjw_ag"
user="root"
password="root"
batchSize="-1" />
<!-- transformer 格式转化:HTMLStripTransformer 索引中忽略HTML标签 -->
<!-- query:查询数据库表符合记录数据 -->
<!-- deltaQuery:增量索引查询主键ID 注意这个只能返回ID字段 -->
<!-- deltaImportQuery:增量索引查询导入的数据 -->
<!-- deletedPkQuery:增量索引删除主键ID查询 注意这个只能返回ID字段-->
<document>
<entity name="yl_data" pk="data_id"
query="select * from yl_data"
deltaQuery = "select data_id from yl_data where data_time > '${dataimporter.last_index_time}'"
deltaImportQuery = "select * from yl_data where data_id = '${dih.delta.data_id}'"
>
<field column="data_id" name="id"/>
<field column="data_id" name="data_id"/>
<field column="data_title" name="data_title"/>
<field column="data_text" name="data_text"/>
<field column="data_author" name="data_author"/>
<field column="data_time" name="data_time"/>
<field column="data_title" name="data_titles"/>
<field column="data_text" name="data_texts"/>
</entity>
</document>
</dataConfig>