开发环境之Solr

查看更多开发环境配置,请点击《开发环境配置大全》

玖章:Solr安装教程

1)官网下载Solr安装包

http://lucene.apache.org/solr/downloads.html

 

2)安装Solr5以前版本

2.1 安装Solr

选择无中文目录,解压缩Sorl安装包即安装完成;

 

2.2 复制Solr的war包到Tomcat

将D:solrsolr-4.10.3solr-4.10.3examplewebapps路径下的war包放入到Tomcat下的D:apache-tomcat-8.5.4-windows-x64apache-tomcat-8.5.4webapps里面,启动tomcat,自动解压缩该war包,解压之后就可以删除该war包;

 

2.3 复制Solr的jar包到Tomcat下的Sorl

复制D:solrsolr-4.10.3solr-4.10.3examplelibext下的所有jar包,将它们放入到D:apache-tomcat-8.5.4-windows-x64apache-tomcat-8.5.4webappssolrWEB-INFlib里面;

复制D:solrsolr-4.10.3solr-4.10.3example esources下的log4j.properties,将它放入到D:apache-tomcat-8.5.4-windows-x64apache-tomcat-8.5.4webappssolrWEB-INFclasses目录下,没有classes目录则创建;

复制D:solrsolr-4.10.3solr-4.10.3dist下的solr-dataimporthandler-4.10.3.jar和solr-dataimporthandler-extras-4.10.3.jar,将它们放入到D:apache-tomcat-8.5.4-windows-x64apache-tomcat-8.5.4webappssolrWEB-INFlib下;

 

2.4 配置solrhome

将D:solrsolr-4.10.3solr-4.10.3examplesolr文件夹复制到无中文目录下,改名为solrhome;

找到D:apache-tomcat-8.5.4-windows-x64apache-tomcat-8.5.4webappssolrWEB-INF下的web.xml,配置solrhome的路径:

<env-entry>
  <env-entry-name>solr/home</env-entry-name>
  <env-entry-value>D:solrsolrhome</env-entry-value>
  <env-entry-type>java.lang.String</env-entry-type>
</env-entry>

3)下载IK分词器

github下载地址:https://github.com/medcl/elasticsearch-analysis-ik/releases

将下载好的IKAnalyzer2012FF_u1.jar放入到D:apache-tomcat-8.5.4-windows-x64apache-tomcat-8.5.4webappssolrWEB-INFlib下;

 

4)IK分词器设置

4.1 在D:solrsorlhomecollection1conf中的schema.xml配置文件中添加:

<!-- IKAnalyzer(IK分词器配置) -->
<fieldType name="text_ik" class="solr.TextField">
  <analyzer class="org.wltea.analyzer.lucene.IKAnalyzer"/>
</fieldType>

4.2 添加词汇与禁用分词

在D:apache-tomcat-8.5.4-windows-x64apache-tomcat-8.5.4webappssolrWEB-INFclasses目录下添加几个文件:

ext.dic:该文件存放新增词汇,比如我是高富帅,就可以把高富帅写进去分词

stopword.dic:该文件存放停止词汇,比如禁止我,你等分词

IKAnalyzer.cfg.xml,分词配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
  <comment>IK Analyzer 扩展配置</comment>
  <!--用户可以在这里配置自己的扩展字典 -->
  <entry key="ext_dict">ext.dic;</entry>
  <!--用户可以在这里配置自己的扩展停止词字典-->
  <entry key="ext_stopwords">stopword.dic;</entry>
</properties>

5)配置域

方式一:

找到D:solrsolrhomecollection1conf下的solrconfig.xml,在最后面加上:

<requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">
  <lst name="defaults">
    <str name="config">data-config.xml</str>
  </lst>
</requestHandler>

在solrconfig.xml同级目录下创建data-config.xml,内容配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<dataConfig>
<dataSource type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/lucene" user="root" password="admins"/>
<document>
  <entity name="product" query="SELECT pid,name,catalog_name,price,description,picture FROM products">
    <field column="pid" name="id"/>
    <field column="name" name="product_name"/>
    <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>

方式二:

修改同目录下的schema.xml文件,在里面添加数据库对应的域,知识点比较多,可以百度配置教程。

 

6)启动

启动Tomcat,浏览器输入http:/localhost:8080/solr即可访问。

 

7)安装Solr5以后版本

在solr5以前solr的启动都有tomcat作为容器,但是从solr5以后solr内部集成jetty服务器,可以通过bin目录中脚本直接启动,就是从solr5以后跟solr4最大的区别是被发布成一个独立的应用。

选择无中文目录,解压缩Sorl安装包即安装完成;

cmd进入到安装包的bin目录下,执行命令solr start,即启动完成,默认端口8983,浏览器输入http:/localhost:8983即可访问。

 

原文地址:https://www.cnblogs.com/xdzy/p/10992812.html