1、安装Solr的前期准备:
Solr是java开发。
需要安装jdk。
安装环境Linux。
需要安装Tomcat。
2、安装Solr服务
1、将solr的压缩包上传到Linux系统中。
2、解压solr 在/usr/local目录下解压
3、安装tomcat,解压缩即可,目录同上。
4、解压solr中的solr-4.10.3.war包,路径是:/usr/local/solr-4.10.3/dist
5、将war包放入tomcat的webapps的下。
6、解压war包,启动tomcat,然后关闭tomcat,进入webapps目录下,删除war包。
7、将/usr/local/solr-4.10.3/example/lib/ext 下的所有jar包,放到tomcat的example/solr/web-inf/lib下。
8、将tomcat下的example/solr 改名为solrhome 并复制到usr/local/solr下
9、关联solr及solrhome 需要改solr工程的web.xml文件
web.xml的文件路径:tomcat的example/solr/web-inf下
10、启动tomcat,http://10.211.55.7:8080/solr/ 访问路径,出现以下界面,即服务端配置成功。
3、安装分词器
1、将中文分词器IK Analyzer 2012FF_hf1放到linux上
2、把IKAnalyzer2012FF_u1.jar添加到solr工程的lib目录下
3、把扩展词典、配置文件放到solr工程的WEB-INF/classes目录下。
4、找到schema.xml文件,添加FiledType,xml文件的目录是:solrhome/collection1下的conf/
配置代码为:
|
<fieldType name="text_ik" class="solr.TextField"> <analyzer class="org.wltea.analyzer.lucene.IKAnalyzer"/> </fieldType> <field name="item_title" type="text_ik" indexed="true" stored="true"/> <field name="item_sell_point" type="text_ik" indexed="true" stored="true"/> <field name="item_price" type="long" indexed="true" stored="true"/> <field name="item_image" type="string" indexed="false" stored="true" /> <field name="item_category_name" type="string" indexed="true" stored="true" />
<field name="item_keywords" type="text_ik" indexed="true" stored="false" multiValued="true"/> <copyField source="item_title" dest="item_keywords"/> <copyField source="item_sell_point" dest="item_keywords"/> <copyField source="item_category_name" dest="item_keywords"/> |
5、重启tomcat,进入http://10.211.55.7:8080/solr/ 检验成果。
4、进行代码测试
1、在maven项目中的pom.xml中加入solrj的依赖
测试添加:
|
//测试添加到域中 @Test public void addDocument() throws Exception { //创建一个SolrServer对象,创建一个连接。参数solr服务的url SolrServer solrServer = new HttpSolrServer("http://10.211.55.7:8080/solr/collection1"); //创建一个文档对象SolrInputDocument SolrInputDocument document = new SolrInputDocument(); //向文档对象中添加域。文档中必须包含一个id域,所有的域的名称必须在schema.xml中定义。 document.addField("id", "doc01"); document.addField("item_title", "测试商品01"); document.addField("item_price", 1000); //把文档写入索引库 solrServer.add(document); //提交 solrServer.commit(); } |