第一步:上传安装包:
1.apache-tomcat-7.0.47.tar.gz
2.IKAnalyzer2012FF_hf1.zip
3.solr-4.10.3.tar
第二步:在根目录上建立文Solr目录
(1)解压tomcat到solr目录:
(2)解压IK分词器
由于该压缩包没有打包,因此解压前需要先建立包:
解压:
(3)解压solr-xx.tar
第三步:查看solr目录结构:
进入example目录查看
第四步:配置环境:
4.1.复制solr.war 到 tomcat/webapps 目录
4.2.复制支持包到 /tomcat/lib目录下:(注意:只需要ext目录中的就够了)
4.3.指定solr家的位置:
(1)将复制到tomcat/webapps路径下的solr.war先解压(先创建solr文件夹)
(2).修改 solr/WEB_INF/web.xml
第五步:启动tomcat服务器:
浏览器访问:
http://192.168.200.128:8080/solr
//服务器中数据索引所在目录:
/solr/solr-4.10.3/example/solr/collection1/data/index
此时发分词效果:
第六步.IK分词器配置:
6.1.查看目录结构
6.2.复制 jar 包到 /tomcat/solr/WEB-INF/lib
#cp /solr/IK/IKAnalyzer2012FF_u1.jar /solr/apache-tomcat-7.0.47/webapps/solr/WEB-INF/lib
6.3.复制 IKAnalyzer.cfg.xml 和 stopword.dic 到/tomcat/webapps/solr/WEB-INF/classes下(先创建classes目录)
[root@hostname IK]# cp IKAnalyzer.cfg.xml stopword.dic /solr/apache-tomcat-7.0.47/webapps/solr/WEB-INF/classes
6.4.先 自定义类(类型支持IK分词) 再自定义字段所属类型
配置 schema.xml配置文件
6.4.1.配置自定义类型:
<!-- 自定义的数据类型 支持IKAnalyzer--> <fieldType name="text_ik" class="solr.TextField"> <analyzer type="index" isMaxWordLength="false" class="org.wltea.analyzer.lucene.IKAnalyzer"/> <analyzer type="query" isMaxWordLength="true" class="org.wltea.analyzer.lucene.IKAnalyzer"/> </fieldType> |
6.4.2.配置自定义字段:
<!-- 自定义字段 name_ik 网站查询的关键词字段 --> <field name="name_ik" type="text_ik" indexed="true" stored="true" /> |
第七步:重新启动Tomcat服务器
第八步:浏览器重新访问:http://192.168.200.128:8080/solr
此时的分词效果:
第九步.配置扩展词:
1.修改
/solr/apache-tomcat-7.0.47/webapps/solr/WEB-INF/classes/IKAnalyzer.cfg.xml 配置文件
将 <entry key="ext_dict">ext.dic;</entry> 的注释去除
2.创建ext.dic文件(就在classes路径下)
3.在ext.dic中输入需要添加的词(一个词占用一行)
第十步.配置停止词
/solr/apache-tomcat-7.0.47/webapps/solr/WEB-INF/classes/stopword.dic
在输入停止的词。保存退出
第十一步.重启tomcat服务器
java接口测试:
@Test public void testSolr() throws IOException, SolrServerException { String baseUrl="http://192.168.200.128:8080/solr"; SolrServer solrServer=new HttpSolrServer(baseUrl); SolrInputDocument doc=new SolrInputDocument(); doc.addField("id",2); doc.addField("name","老王"); solrServer.add(doc); solrServer.commit(); } |
整合 Spring:
solr.xml:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!--solr 配置--> <bean id="solrServer" class="org.apache.solr.client.solrj.impl.HttpSolrServer"> <constructor-arg value="http://192.168.200.128:8080/solr" /> </bean> </beans> |
测试:
@Autowired private SolrServer solrServer; @Test public void testSolr() throws IOException, SolrServerException { SolrInputDocument doc=new SolrInputDocument(); doc.addField("id",3); doc.addField("name","大头儿子"); solrServer.add(doc); solrServer.commit(); } |