最近考虑用lucene替代mysql的800W数据的Like语句。安装下lucene成型的Solr框架尝试下。
一、下载tomcat 7.0 和Solr-4.6.0 并解压
window32位:http://tomcat.apache.org/download-70.cgi
http://www.gtlib.gatech.edu/pub/apache/lucene/solr/4.6.1/solr-4.6.1.zip
C:\Users\wzou\Tools\Tomcat7
C:\Users\wzou\Tools\Solr\solr-4.6.1
二、新建一个solr文件夹,我的是:C:\Users\wzou\Tools\Solr\SolrHome。
将解压后C:\Users\wzou\Tools\Solr\solr-4.6.1\example\solr下面的所有文件放入到C:\Users\wzou\Tools\Solr\SolrHome。
将solr包里面的:C:\Users\wzou\Tools\Solr\solr-4.6.1\dist\solr-4.6.1.war复制到tomcat里面的webapps文件夹下面,改名为solr.war。
三、在tomcat的conf/Catalina/localhost下添加solr.xml文件,文件内容如下:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Context docBase="C:\Users\wzou\Tools\Tomcat7\webapps\solr.war" debug="0" crossContext="true" >
<Environment name="solr/home" type="java.lang.String" value="C:\Users\wzou\Tools\Solr\SolrHome" override="true" />
</Context>
四:启动Tomcat7\bin下的startup.bat,这是tomcat会报错!
[SetContextPropertiesRule]{Context} Setting property ‘debug’ to ’0′ did not find a matching property.
2014-02-25 12:23:10 org.apache.catalina.core.StandardContext startInternal
严重: Error filterStart
2014-02-25 12:23:10 org.apache.catalina.core.StandardContext startInternal
严重: Context [/solr] startup failed due to previous errors
在tomcat的logs文件夹下面查日志 localhost.2014-02-25.log
2014-02-25 12:23:10 org.apache.catalina.core.StandardContext filterStart
严重: Exception starting filter SolrRequestFilter
org.apache.solr.common.SolrException: Could not find necessary SLF4j logging jars. If using Jetty, the SLF4j logging jars need to go in the jetty lib/ext directory. For other containers, the corresponding directory should be used. For more information, see: http://wiki.apache.org/solr/SolrLogging
at org.apache.solr.servlet.SolrDispatchFilter.<init>(SolrDispatchFilter.java:111)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
少jar包,
把solr-4.6.1\example\lib\ext这个路径下的所有slf4j相关的jar包拷进了Tomcat7\webapps\solr\WEB-INF\lib这个文件夹
重启,部署成功。http://localhost:8080/solr
配置Importdata
五:solr-4.6.1\dist文件夹下的solr-dataimporthandler-4.6.0.jar和solr-dataimporthandler-extras-4.6.0.jar放入Tomcat7\webapps\solr\WEB-INF\lib
六:修改C:\Users\wzou\Tools\Solr\SolrHome\collection1\conf\solrconfig.xml,添加下面这段代码
<requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">
<lst name="defaults">
<str name="config">data-config.xml</str>
</lst>
</requestHandler>
七:还是在本当前目录下新建data-config.xml文件,注意&等符号编码问题:
<?xml version="1.0" encoding="UTF-8" ?>
<dataConfig>
<dataSource type="JdbcDataSource"
driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://<host>:3306/test?useUnicode=true&characterEncoding=UTF-8"
user="test"
password="test"
stream="true"/>
<document name="content" >
<entity name="solr_test" pk="id" query="select id,name from solr_test"></entity>
</document>
</dataConfig>
八:
修改schema.xml,因为我测试用的两个字段 id和name 已经有了。所以没新增
<field name="_version_" type="long" indexed="true" stored="true"/>
<!-- points to the root document of a block of nested documents. Required for nested
document support, may be removed otherwise
-->
<field name="_root_" type="string" indexed="true" stored="false"/>
<field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false" />
<field name="name" type="string" indexed="true" stored="true"/>
注意,_root_和_version_是必备的,不能少,少了就报错!
九:重启
http://localhost:8080/solr/#/collection1/dataimport//dataimport
执行Execute。右边出现五条记录索引被创建成功。
注意:期间遇到点Execute没反应的情况。查看日志得知data-config.xml配置里的com.mysql.jdbc.Driver驱动包缺失,正好我另一个项目里有。遂将mysql-connector-java-5.1.17.jar 拷到Tomcat7\webapps\solr\WEB-INF\lib下。
下面还会配置定时创建索引 分词器等功能。待续....
参考资料:
http://www.blogjava.net/xiaohuzi2008/archive/2012/12/03/392373.html
http://www.kankanews.com/ICkengine/archives/97741.shtml