1、配置solr环境java
一、 下载solr http://www.apache.org/dyn/closer.cgi/lucene/solr/mysql
此处用的是4.6 解压到D盘D:/solr/apache-solr-4.6.0web
二、修改tomcat conf/server.xmlsql
<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"URIEncoding="UTF-8" />数据库
添加编码的配置 URIEncoding="UTF-8" (如不添加,中文检索时由于乱码搜索不到)apache
三、 配置solr缓存
Tomcat conf/Catalina/localhost下添加solr.xmltomcat
内容:服务器
<Context docBase="D:/solr/apache-solr-4.6.0/dist/apache-solr-4.6.0.war" debug="0" crossContext="true" >app
<Environment name="solr/home" type="java.lang.String" value="D:/solr/apache-solr-4.6.0/example/solr" override="true" />
</Context>
四、 启动tomcat 输入http://localhost:8080/solr/ 出现欢迎界面表示成功
2、中文分词配置
一、 下载分词器: http://code.google.com/p/mmseg4j/
二、下载词库:http://code.google.com/p/mmseg4j/downloads/detail?name=data.zip&can=2&q
三、将解压后的mmseg4j-1.8.5目录下的mmseg4j-all-1.8.5.jar拷贝到Tomcat的webapps/solr/WEB-INF/lib目录下。
四、添加词库:在D:/solr/apache-solr-4.6.0/example/solr目录下新建dic文件夹,将解压后的data目录下的words.dic拷贝到D:/solr/apache-solr-4.6.0/example/solr/dic目录下。
五、 更改schema.xml(D:/solr/apache-solr-4.6.0/example/solr/conf/)文件,使分词器起到做用。
在schema.xml的<types>、<fields>和部分新增以下配置:
注:dicPath=" "是你的词库路径。
<!--mmseg4j field types-->
<fieldType name="textComplex" class="solr.TextField" positionIncrementGap="100" >
<analyzer>
<tokenizer class="com.chenlb.mmseg4j.solr.MMSegTokenizerFactory" mode="complex" dicPath="D:/solr/apache-solr-4.6.0/example/solr/dic"/>
<filter class="solr.LowerCaseFilterFactory"/>
</analyzer>
</fieldType>
<fieldType name="textMaxWord" class="solr.TextField" positionIncrementGap="100" >
<analyzer>
<tokenizer class="com.chenlb.mmseg4j.solr.MMSegTokenizerFactory" mode="max-word" dicPath="D:/solr/apache-solr-4.6.0/example/solr/dic"/>
<filter class="solr.LowerCaseFilterFactory"/>
</analyzer>
</fieldType>
<fieldType name="textSimple" class="solr.TextField" positionIncrementGap="100" >
<analyzer>
<tokenizer class="com.chenlb.mmseg4j.solr.MMSegTokenizerFactory" mode="simple" dicPath="D:/solr/apache-solr-4.6.0/example/solr/dic"/>
<filter class="solr.LowerCaseFilterFactory"/>
</analyzer>
</fieldType>
<!-- mmseg4j field -->
<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_mmseg4j" type="textMaxWord" indexed="true" stored="true" multiValued="true"/>
<!-- mmseg4j copyField -->
<copyField source="simple" dest="text_mmseg4j"/>
<copyField source="complex" dest="text_mmseg4j"/>
重启你的tomcat。
访问:http://localhost:8080/solr/admin/analysis.jsp能够看 mmseg4j 的分词效果。在 Field 的下拉菜单选择 name,而后在应用输入 complex。分词的结果,以下图:
好了,能够运行起来了,那就添加个文档试下,在 解压后的D:/solr/apache-solr-4.6.0/example/exampledocs目录下建立 mmseg4j-solr-demo-doc.xml 文档,内容以下:
<add>
<doc>
<field name="id">1</field>
<field name="text">高效、灵活的缓存功能,垂直搜索功能。</field>
</doc>
<doc>
<field name="id">2</field>
<field name="text"> Solr是一个高性能,采用Java5开发,基于Lucene的全文搜索服务器。</field>
</doc>
<doc>
<field name="id">3</field>
<field name="text"> 独立的企业级搜索应用服务器</field>
</doc>
</add>
注意:使用的字段name必须在solr的配置文件schem.xml增长
下(id、title)同样
而后在 cmd 下运行 post.jar,以下:
D:\solr\apache-solr-4.6.0\example\exampledocs>java -Durl=http://localhost:8080/
solr/update -Dcommit=yes -jar post.jar mmseg4j-solr-demo-doc.xml
注意:mmseg4j-solr-demo-doc.xml 要是 UTF-8 格式,否则提交后会乱码。还有在查询中文时须要把tomcat设置成URIEncoding="UTF-8";
查看是否有数据,访问:http://localhost:8080/solr/admin/ 在Query String: 中输入“高性能”
显示以下图所示:
3、Solr将数据库作成索引数据源(以mysql为例)
一、 在solrconfig.xml中配置导入数据功能(D:\solr\apache-solr-4.6.0\example\solr\conf下)
<requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">
<lst name="defaults">
<str name="config">D:\solr\apache-solr-4.6.0\example\solr\conf\db\db-data-config.xml</str>
</lst>
</requestHandler>
二、 添加一个数据源db-data-config.xml (D:\solr\apache-solr-4.6.0\example\solr\conf\db下)
<dataConfig>
<dataSource type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/solr" user="root" password="root"/>
<document name="messages">
<entity name="user" transformer="ClobTransformer" query="select * from t_user">
<field column="id" name="id" />
<field column="name" name="name"/>
<field column="title" name="title"/>
</entity>
</document>
</dataConfig>
CREATE TABLE t_user
(
id VARCHAR(255) PRIMARY KEY,
name VARCHAR(10) NOT NULL,
title VARCHAR(10)
);
三、放入mysql驱动包 (Tomcat的webapps/solr/WEB-INF/lib目录下)
四、建立索引
http://localhost:8080/solr/dataimport?command=full-import
五、 查看是否成功,访问:http://localhost:8080/solr/admin/ 在Query String: 中输入“sunshan”、“孙闪”
以下图:
注:保证与数据库链接正确
4、Solr多核(MultiCore)配置
一、 拷贝multicore(D:\solr\apache-solr-4.6.0\example下)下的core0、core一、exampledocs到solr(D:\solr\apache-solr-4.6.0\example\solr下)目录下
二、 更改solr.xml(D:\solr\apache-solr-4.6.0\example\solr下)配置
添加:
<core name="core0" instanceDir="core0" dataDir="D:/solr/apache-solr-4.6.0/example/solr/core0/data"/>
<core name="core1" instanceDir="core1" dataDir="D:/solr/apache-solr-4.6.0/example/solr/core1/data"/>
配置后:
<cores adminPath="/admin/cores">
<core name="collection1" instanceDir="." />
<core name="core0" instanceDir="core0" dataDir="D:/solr/apache-solr-4.6.0/example/solr/core0/data"/>
<core name="core1" instanceDir="core1" dataDir="D:/solr/apache-solr-4.6.0/example/solr/core1/data"/>
</cores>
三、 启动tomcat 访问:
http://localhost:8080/solr/
5、多核数据库索引:
同上配置
注:出现错误Error loading class 'org.apache.solr.handler.dataimport.DataImportHandler'
需配置(solrconfig.xml里)
<!-- start -->
<lib dir="D:/solr/apache-solr-4.6.0/contrib/extraction/lib" />
<lib dir="D:/solr/apache-solr-4.6.0/dist/" regex="apache-solr-cell-\d.*\.jar" />
<lib dir="D:/solr/apache-solr-4.6.0/dist/" regex="apache-solr-clustering-\d.*\.jar" />
<lib dir="D:/solr/apache-solr-4.6.0/dist/" regex="apache-solr-dataimporthandler-\d.*\.jar" />
<lib dir="D:/solr/apache-solr-3.3.0/contrib/clustering/lib/" />
<lib dir="/total/crap/dir/ignored" />
<!-- end -->
dir注意路径
重启tomcat
建立索引:http://localhost:8080/solr/core1/dataimport?command=full-import
http://localhost:8080/solr/dataimport?command=full-import 这 url 告诉 solr 作全量索引,作索引中会删除全部数据。固然也能够用 clean=false 参数来告诉它不删除,但也会删除相同id的(在 scheam.xml 的uniqueKey 声明的)。http://localhost:8080/solr/dataimport?command=full-import&clean=false
批量导入command=delta-import&commit=y
从新装载配置文件command=reload-config
终止导入command=abort