Solr6 +mmseg4j+IK-Analyzer + SQLserver +DIH 完全配置

如今做任何一个系统都有搜索,而搜索界有著名的三剑客: solr/elasticsearch/sphinx

solr/elasticsearch 为同一类的,都是基于lucene开发的产品,本人也早在几年前用过solr做过类似中关村的产品搜索,faceting功能非常好用.

近期手头上又有个项目要搭建搜索,由于几年没摸过Solr,如今再次打开官网已觉得很陌生,不仅主页换漂亮了,版本更是到了6.1 ,还有了 solr cloud的概念!!

废话不多说,首先来介绍下环境配置:

1. 去 http://www.apache.org/dyn/closer.lua/lucene/solr/6.1.0  下载zip,解压放到你想放的位置, 我放到了E盘根目录

2. 设置环境变量PATH: E:solr-6.1.0in , 这是为了方便在命令行里面可以直接找到 solr 命令

3. 任意位置创建一个目录,该目录包含以下内容

   1). IK+mmseg4j的字典

   2).sqlserver jdbc 驱动

   3).最新版的 ik+mmseg4j 的 jar包

然后在目录下创建一个 1.start-solr.cmd (你喜欢的名字),内容如下:

solr start -h localhost -p 58983 -m 1g -s "%~dp0solr_home" -noprompt -V

  双击运行(如果目录是需要管理员权限的, 有可能需要管理方式运行)

下面来说说具体配置

solrconfig.xml (solr_homeproductsconfsolrconfig.xml)

该配置文件是放到core里面的,我新建一个products的core

把所有依赖的jar包配置进去

<lib dir="${solr.install.dir:../../../..}/dist/" regex="solr-dataimporthandler-.*.jar" />
 <!--同时兼容mmseg4j+ik中文分词器-->
  <lib dir="${solr.solr.home}/../tokenizers/mmseg4j" regex=".*.jar" />
  <lib dir="${solr.solr.home}/../tokenizers/IK-Analyzer" regex=".*.jar" />
  <lib dir="${solr.solr.home}/../sqlserver-jdbc" regex=".*.jar" />

启用DIH,这里要注意这个功能依赖上面的  solr-dataimporthandler-xx.jar 配置

<!--启用DIH数据导入-->
  <requestHandler name="/dataimport" class="solr.DataImportHandler">
    <lst name="defaults">
      <str name="config">db-data-config.xml</str>
    </lst>
  </requestHandler>

以下是solr6里面,如果要手动修改schame.xml配置时要替换的,详见注释链接

<!-- 这里需要配置这个,https://cwiki.apache.org/confluence/display/solr/Schema+Factory+Definition+in+SolrConfig -->
<schemaFactory class="ClassicIndexSchemaFactory"/>

schema.xml 配置

<!--以下定义中文分词器及各自的词典配置-->
  <fieldtype name="mmseg4jComplex" class="solr.TextField" positionIncrementGap="100">
    <analyzer>
      <tokenizer class="com.chenlb.mmseg4j.solr.MMSegTokenizerFactory" mode="complex"  dicPath="${solr.solr.home:}/../dict/mmseg4j" />
    </analyzer>
  </fieldtype>
  <fieldtype name="mmseg4jMaxWord" class="solr.TextField" positionIncrementGap="100">
    <analyzer>
      <tokenizer class="com.chenlb.mmseg4j.solr.MMSegTokenizerFactory" mode="max-word"  dicPath="${solr.solr.home:}/../dict/mmseg4j"  />
    </analyzer>
  </fieldtype>
  <fieldtype name="mmseg4jSimple" class="solr.TextField" positionIncrementGap="100">
    <analyzer>
      <tokenizer class="com.chenlb.mmseg4j.solr.MMSegTokenizerFactory" mode="simple"  dicPath="${solr.solr.home:}/../dict/mmseg4j" />
    </analyzer>
  </fieldtype>

  <fieldType name="text_ik" class="solr.TextField">
    <!--索引时候的分词器-->
    <analyzer type="index">
      <tokenizer class="org.wltea.analyzer.util.IKTokenizerFactory" useSmart="true"/>
    </analyzer>
    <!--查询时候的分词器-->
    <analyzer type="query">
      <tokenizer class="org.wltea.analyzer.util.IKTokenizerFactory" useSmart="false"/>
    </analyzer>
  </fieldType>

db-data-config.xml

<dataConfig>
  <dataSource type="JdbcDataSource"
          driver="com.microsoft.sqlserver.jdbc.SQLServerDriver"
          url="jdbc:sqlserver://127.0.0.1:1433;databaseName=xxdb;"
          user="dev"
          password="111111"
          batchSize="100" />
  <document>
    <entity name="product" query="select [id],[name],[brief],[description] from [products]"
            deltaQuery="select id from [products] where [lastmodificationtime] > '${dataimporter.last_index_time}'">
      <field column="name" name="name" />
      <field column="brief" name="brief" />
      <field column="description" name="description" />
    </entity>
  </document>
</dataConfig>

以上配置内包含的路径均没有写死, 使用的占位符, 可用的占位符可以在 solr admin ui 的 dashboard JVM 栏看到

!!!!!!!!!!!!!!!!!前方高能!!!!!!附件说明!!!!!!!!!!

点我下载附件,双击运行,如有报错,欢迎来喷(^_^)!

(一定要先配置PATH环境变量后再双击)

原文地址:https://www.cnblogs.com/cabbage/p/Solr6-mmseg4j-IK-Analyzer.html