您的位置:首页 > 其它

solr 的安装与使用、中文分词器的配置

2015-08-10 17:29 507 查看
主要分四部分:solr的基本配置,多核配置,dataimporthander使用mysql数据集的配置,以及mmseg4j和KAnalyzer中文分词器的配置。

Solr基本配置

1、 安装jdk和tomcat,配置java环境变量。

2、把解压后的solr/example/solr/dist/ apache-solr-3.6.2.war复制到tomcat/webapps目录下,并重命名为solr.war。

启动tomcat,就可以解压solr.war,可以发现在webapps目录下多了一个solr目录。

3、修改/tomcat/conf/server.xml

1
2
3

<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" URIEncoding="UTF-8”/>

增加URIEncoding=”UTF-8”,防止出现乱码。

4、JNDI设置severlet运行参数:在tomcat/conf/Catalina/localhost/目录下创建solr.xml文件

添加内容

1
2
3

<Context docBase="/usr/local/tomcat/webapps/solr.war" debug="0" crossContext="true">
<Environment name="solr/home" type="java.lang.String" value="/usr/local/solr/work/solr" override="true"/>
</Context>

docBase=”/usr/local/tomcat/webapps/solr.war”指定Tomcat运行的serverlet;

value=”/usr/local/solr/work/solr”指定solr的工作目录,即solor.home。

5、在源码目录下面的example有已经配置好单实例(solr)和多实例(multicore)的模板,可以直接修改后使用。

需要把example/lib/ext/*下面的jre包放入到tomcat/webapps/solr/WEB-INF/lib/目录下面,否侧提示catilina.out日志中显示错误信息“SEVERE:
Error filterStart”。注意查看catalina.out和localhost.year-month-day.log两个日志。

cp /usr/local/solr/example/lib/ext/* /usr/local/tomcat/webapps/solr/WEB-INF/lib/

Solr多核配置

example目录下面已经存在core0和core1两个核,复制模板并修改相应的参数。

1、cp –rf core1 testcore

2、修改配置文件在multicore下面的solr.xml文件

添加:

<core name=”test” instanceDir=”testcore” />

因而一共有三个core:core0、core1、testcore

1
2
34
5

<cores adminPath="/admin/cores">
<core name="core0" instanceDir="core0" />
<core name="core1" instanceDir="core1" />
<core name="test"  instanceDir="testcore" />
</cores>

也可以通过指定索引数据的存放目录,默认的存放目录为test/core/data/index/。通过下面的选项可以修改数据目录,”.”为当前目录,可以修改为指定目录。

1
2
3

<core name="test" instanceDir="testcore ">
<property name="dataDir" value=". " />
</core>

solr.xml文件中的另外两个配置选项,定义为:

persistent: Save changes made via the API to this file

sharedLib: path to a lib directory that will be shared across all cores

persistent:字面意思“持久”,该选项决定在solr运行时通过API和Admin UI(web管理页面)修改的数据是否保存。persistent=”false”,任何运行时修改的数据将不会保存,即下次启动solr时这些将会失效。

shareLib:设置jar共享目录,如分词器、jdbc驱动。该目录下的jar包,可以被任一核使用,该路径可以是相对 solr home 的,也可以是绝对的。

使用mysql作为Solr源数据集(dataimporthander的使用)

1、 下载mysql的jdbc驱动,放入tomcat/webapps/solr/WEB-INF/lib目录中(mmseg4j分词器的jar包也在该目录中)。

2、 将下载的solr源文件中dist目录中dataimporthander的jar文件复制到solrconfig.xml指定的“lib dir”指定的目录中。

1
2

<lib dir="/usr/local/solr/dist/" regex="apache-solr-dataimporthandler-\d.*\.jar" />
<lib dir="/usr/local/solr/contrib/dataimporthandler/lib/" regex=".*\.jar" />

regex=”apache-solr-dataimporthandler-\d.*\.jar”需要注意实际dataimporthandler的jre包的名字。

3、 在core/conf下添加数据源连接文件mysql.xml

1
2
34
5
6
7
8
9
10
11

<dataConfig>
<dataSource type="JdbcDataSource"
driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://192.168.3.90/test"
user="sylar"
password="sylar"/>

<document name="test">
<entity name="uid" pk="uid" query="select user.uid,user.uname,user.title from user"> </entity>
</document>
</dataConfig>

4、 修改solrconfig.xml文件,添加一下内容。

DHI设置,其中指定mysql.xml文件作为数据源配置文件。

1
2
34
5

<requestHandler name="/dataimport">
<lst name="defaults">
<str name="config">mysql.xml</str>
</lst>
</requestHandler>

5、 修改schemal.xml配置文件

1
2
34
5

<fields>
<field name="uid"     type="int"      indexed="true"  stored="true"  multiValued="false" />
<field name="uname"  type="string"    indexed="true"  stored="true"  multiValued="false" />
<field name="title"    type="string"    indexed="true"  stored="true" />
</fields>

当solr不能正常工作时查看tomcat的catalina.out和localhost.year-month-day.log两个日志,注意里面的错误信息。

添加mmsegj4中文分词器

1、 解压mmsegj4-1.8.5.zip的压缩包,把解压后的mmseg4j-all-1.8.5.jar复制到tomcat/webapps/solr/WEB_INF/lib/目录下。

2、 在tomcat/webapps/solr/目录下建立dict,存放词典。把解压后的data目录中的四个文件(chars.dic 、units.dic 、words.dic 、words-my.dic)复制到dict目录下。在下一步中分词器(analyzer)将会指向这个词典的路径。

3、 cp /usr/local/solr/example/lib/ext/* /usr/local/tomcat/webapps/solr/WEB-INF/lib/

4、 修改tomcat/webapps/solr/conf/schema.xml文件。

1
2
34
5
6
7
8
9
10
1112
13
14
15
16
17
18

<types></types>域中添加:
<fieldtype name="textComplex" positionIncrementGap="100">
<analyzer>
<tokenizer mode="complex" dicPath="/usr/local/tomcat/webapps/solr/dict"></tokenizer>
</analyzer>
</fieldtype>

<fieldtype name="textMaxWord" positionIncrementGap="100">
<analyzer>
<tokenizer mode="max-word" dicPath="/usr/local/tomcat/webapps/solr/dict"></tokenizer>
</analyzer>
</fieldtype>

<fieldtype name="textSimple" positionIncrementGap="100">
<analyzer>
<tokenizer mode="simple" dicPath="/usr/local/tomcat/webapps/solr/dict"></tokenizer>
</analyzer>
</fieldtype>

在<fields></fields>域中添加

1
2
3

<field name="simple" type="textSimple" indexed="true" stored="true" multiValued="true"/>
<field name="complex" type="textComplex" indexed="true" stored="true" multiValued="true"/>
<field name="text_cn" type="textMaxWord" indexed="true" stored="true" multiValued="true"/>

添加copyField

1
2

<copyField source="simple" dest="text_cn"/>
<copyField source="complex" dest="text_cn"/>

5、 重新启动Tomcat,测试http://IP:8080/solr/admin/analysis.jsp页面。

添加IKAnalyzer中文分词器

1、 解压得到的IKAnalyzer2012.jar文件拷贝到 tomcat/webapps/solr/WEB-INF/lib/下。

2、 修改tomcat/webapps/solr/conf/schema.xml文件

在Types域添加

1
2
34
5
6
7
8
9
10
1112
13
14
15
16
17
18
19
20
21

<fieldType name="textIKA" positionIncrementGap="100">
<analyzer type="index">
<tokenizer class = "org.wltea.analyzer.solr.IKTokenizerFactory" isMaxWordLength="false" />
<filter ignoreCase="true" words="stopwords.txt" enablePositionIncrements="true" />
<filter generateWordParts="1" generateNumberParts="1" catenateWords="1" catenateNumbers="1" catenateAll="0" splitOnCaseChange="1" />
<filter />

<filter protected="protwords.txt" />
<filter />
</analyzer>
<analyzer type="query">
<tokenizer class = "org.wltea.analyzer.solr.IKTokenizerFactory" isMaxWordLength="true" />
<filter synonyms="synonyms.txt" ignoreCase="true" expand="true" />
<filter ignoreCase="true" words="stopwords.txt" />
<filter generateWordParts="1" generateNumberParts="1" catenateWords="0" catenateNumbers="0" catenateAll="0" splitOnCaseChange="1" />
<filter />

<filter protected="protwords.txt" />
<filter />
</analyzer>
</fieldType>

在fields域添加

1

<field name="text_ika" type="textIKA" indexed="true" stored="true" required="true" />

3、 重启Tomcat,测试
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: