注:
- 文章只是记录操作步骤,不适合入门教学
- 直接利用内置的jetty,不使用tomcat
下载
- 从官网仓库下载zip包 : solr-6.3.0.zip , 解压到E:\solr-6.3.0。
配置jar包
- 在E:\solr-6.3.0\contrib下新建文件夹db\lib和ik\lib,将mysql数据库的驱动包mysql-connector-java-5.1.40.jar和ik中文分词器的jar包ik-analyzer-solr-6.3.0.jar分别拷贝到E:\solr-6.3.0\contrib\db\lib和E:\solr-6.3.0\contrib\ik\lib文件夹下。
创建core
- 在E:\solr-6.3.0\bin目录下,启动控制台,使用命令行solr create -c products -d basic_configs的创建一个名为products的core,创建的core在E:\solr-6.3.0\server\solr目录下
配置core
- 在E:\solr-6.3.0\server\solr\products\conf目录下
配置managed-schema
- 将如下内容注释掉
<field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false" />
- 搜索uniqueKey并把值改为product_id
<uniqueKey>product_id</uniqueKey>
- 在配置文件末尾(
</schema>
之前),添加如下配置
<!-- 配置ik中文分词器 -->
<fieldType name="text_ik" class="solr.TextField" positionIncrementGap="100">
<analyzer type="index">
<tokenizer class="org.wltea.analyzer.lucene.IKTokenizerFactory" useSmart="false"/>
<filter class="solr.LowerCaseFilterFactory"/>
</analyzer>
<analyzer type="query">
<tokenizer class="org.wltea.analyzer.lucene.IKTokenizerFactory" useSmart="true"/>
<filter class="solr.LowerCaseFilterFactory"/>
</analyzer>
</fieldType>
<!-- 属性 -->
<field name="product_id" type="string" indexed="true" stored="true" required="true" />
<field name="product_name" type="text_ik" indexed="true" stored="true" />
<field name="product_catalog" type="int" indexed="false" stored="true" />
<field name="product_catalog_name" type="string" indexed="true" stored="false" />
<field name="product_price" type="tfloat" indexed="true" stored="true" />
<field name="product_description" type="text_ik" indexed="true" stored="false" />
<field name="product_picture" type="string" indexed="false" stored="true" />
<field name="product_keywords" type="text_ik" indexed="true" stored="false" multiValued="true" />
<copyField source="product_name" dest="product_keywords" />
<copyField source="product_description" dest="product_keywords" />
配置data-config.xml
- 新建data-config.xml配置文件,内容如下:
<?xml version="1.0" encoding="UTF-8" ?>
<dataConfig>
<dataSource
driver="com.mysql.jdbc.Driver"
type="JdbcDataSource"
url="jdbc:mysql://localhost:3306/solr?useSSL=true"
user="root"
password="13061079"
/>
<document>
<entity name="product" query="SELECT pid,name,catalog,catalog_name,price,description,picture FROM products ">
<field column="pid" name="product_id"/>
<field column="name" name="product_name"/>
<field column="catalog" name="product_catalog"/>
<field column="catalog_name" name="product_catalog_name"/>
<field column="price" name="product_price"/>
<field column="description" name="product_description"/>
<field column="picture" name="product_picture"/>
</entity>
</document>
</dataConfig>
配置solrconfig.xml
- 在lib标签附近添加如下配置
<!-- 导入jar包 -->
<lib dir="${solr.install.dir:../../../..}/contrib/dataimporthandler/lib" regex=".*\.jar" />
<lib dir="${solr.install.dir:../../../..}/dist/" regex="solr-dataimporthandler-\d.*\.jar" />
<!-- 连接数据库jar包 -->
<lib dir="${solr.install.dir:../../../..}/contrib/db/lib" regex=".*\.jar" />
<!-- ik分词器jar包 -->
<lib dir="${solr.install.dir:../../../..}/contrib/ik/lib" regex=".*\.jar" />
- 在updateHandler之前添加如下配置:
<requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">
<lst name="defaults">
<str name="config">data-config.xml</str>
</lst>
</requestHandler>
启动
- 在E:\solr-6.3.0\bin目录下,启动控制台(cmd),通过命令solr start即可启动