前面讲过solr在windows和linux下的安装了,但是并没有安装中文分析器,这里补充吧。 安装中文分词器需要用到solrhome\collection1\conf下的schema.xml文件。所以有必要先说一下这个xml文件。
打开schema.xml后主要看到以下的几个标签: 1、fieldType:域类型定义 如:
<fieldType name="text_general" class="solr.TextField" positionIncrementGap="100"> <analyzer type="index"> <tokenizer class="solr.StandardTokenizerFactory"/> <filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt" /> <filter class="solr.LowerCaseFilterFactory"/> </analyzer> <analyzer type="query"> <tokenizer class="solr.StandardTokenizerFactory"/> <filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt" /> <filter class="solr.SynonymFilterFactory" synonyms="synonyms.txt" ignoreCase="true" expand="true"/> <filter class="solr.LowerCaseFilterFactory"/> </analyzer> </fieldType>class:是solr自己封装的类型,可以根据该类型来创建数据跟查询。 analyzer表示分析器,由分词器(包括索引分词器跟查询分词器)跟过滤器组成。
2、field:业务域定义 在该标签内定义具体的业务域,注意,域的类型必须是在fieldType中定义过的name,否则添加索引的时候会报错。如:
<field name="name" type="text_general" indexed="true" stored="true"/> <field name="cat" type="string" indexed="true" stored="true" multiValued="true"/>indexed:是否被索引 stored:是否被存储 multivalued:是否可存储多个值 solr其实已经帮我们定义好了很多的field,比如name,id,cat等,用不用的看就看情况了。
3、copyField:动态字段 如:
<dynamicField name="*_i" type="int" indexed="true" stored="true"/> <dynamicField name="*_is" type="int" indexed="true" stored="true" multiValued="true"/>不解释了,肯定能看懂的。
4、copyField:复制域 如;
<copyField source="title" dest="text"/> <copyField source="name" dest="text"/>这就表示会将定义好的title和name两个业务域映射(复制)到text域上,那么进行关键字查询text域中的信息的时候就相当域同时查了title和name两个域中信息,而不用进行两次查询,从而进行了统一的检索提高了效率。
好了,随便介绍了下不懂也没事。等到案例中用起来的时候就自然明白了,当然案例在这不讲了,接下来介绍中文分析器的安装,安装完了那么solr才算真正意义上的安装配置完了。
windows就不说了会linux下的配置更会windos下的配置。
第一步: 先看一下ik分析器文件夹吧,网上能找到: 将IK Analyzer 2012FF_hf1上传到linux(我的还是放在/usr目录下的) 复制IKAnalyzer2012FF_u1.ja到 /usr/local/solr/tomcat/webapps/solr/WEB-INF/lib(这个在介绍安装配置solr的时候创建的)下。
[root@itheima32 usr]# cd IK\ Analyzer\ 2012FF_hf1 [root@itheima32 IK Analyzer 2012FF_hf1]# 总用量 2004 drwxr-xr-x. 5 root root 4096 6月 29 2017 doc -rw-r--r--. 1 root root 168 6月 29 2017 ext_stopword.dic -rw-r--r--. 1 root root 1165908 6月 29 2017 IKAnalyzer2012FF_u1.jar -rw-r--r--. 1 root root 419 6月 29 2017 IKAnalyzer.cfg.xml -rw-r--r--. 1 root root 841268 6月 29 2017 IKAnalyzer中文分词器V2012_FF使用手册.pdf -rw-r--r--. 1 root root 17778 6月 29 2017 LICENSE.txt -rw-r--r--. 1 root root 34 6月 29 2017 mydict.dic -rw-r--r--. 1 root root 278 6月 29 2017 NOTICE.txt [root@itheima32 IK Analyzer 2012FF_hf1]# cp IKAnalyzer2012FF_u1.jar /usr/local/solr/tomcat/webapps/solr/WEB-INF/lib [root@itheima32 IK Analyzer 2012FF_hf1]# cd第二步: 在/usr/local/solr/tomcat/webapps/solr/WEB-INF下创建classes文件夹,将扩展词典、配置文件放到solr工程的WEB-INF/classes目录下
[root@itheima32 IK Analyzer 2012FF_hf1]# cd /usr/local/solr/tomcat/webapps/solr/WEB-INF [root@itheima32 WEB-INF]# ll 总用量 16 drwxr-xr-x. 2 root root 4096 6月 29 19:36 lib -rw-r--r--. 1 root root 1210 12月 1 2014 weblogic.xml -rw-r--r--. 1 root root 7044 6月 29 16:43 web.xml 2、把扩展词典、配置文件放到solr工程的WEB-INF/classes目录下。 [root@itheima32 WEB-INF]# mkdir /usr/local/solr/tomcat/webapps/solr/WEB-INF/classes [root@itheima32 WEB-INF]# ll 总用量 20 drwxr-xr-x. 2 root root 4096 6月 29 19:38 classes drwxr-xr-x. 2 root root 4096 6月 29 19:36 lib -rw-r--r--. 1 root root 1210 12月 1 2014 weblogic.xml -rw-r--r--. 1 root root 7044 6月 29 16:43 web.xml [root@itheima32 WEB-INF]# cd /usr/IK\ Analyzer\ 2012FF_hf1 [root@itheima32 IK Analyzer 2012FF_hf1]# ll 总用量 2004 drwxr-xr-x. 5 root root 4096 6月 29 2017 doc -rw-r--r--. 1 root root 168 6月 29 2017 ext_stopword.dic -rw-r--r--. 1 root root 1165908 6月 29 2017 IKAnalyzer2012FF_u1.jar -rw-r--r--. 1 root root 419 6月 29 2017 IKAnalyzer.cfg.xml -rw-r--r--. 1 root root 841268 6月 29 2017 IKAnalyzer中文分词器V2012_FF使用手册.pdf -rw-r--r--. 1 root root 17778 6月 29 2017 LICENSE.txt -rw-r--r--. 1 root root 34 6月 29 2017 mydict.dic -rw-r--r--. 1 root root 278 6月 29 2017 NOTICE.txt [root@itheima32 IK Analyzer 2012FF_hf1]# cp ext_stopword.dic IKAnalyzer.cfg.xml mydict.dic /usr/local/solr/tomcat/webapps/solr/WEB-INF/classes [root@itheima32 IK Analyzer 2012FF_hf1]# ll /usr/local/solr/tomcat/webapps/solr/WEB-INF/classes 总用量 12 -rw-r--r--. 1 root root 168 6月 29 19:40 ext_stopword.dic -rw-r--r--. 1 root root 419 6月 29 19:40 IKAnalyzer.cfg.xml -rw-r--r--. 1 root root 34 6月 29 19:40 mydict.dic第三步: 配置一个fieldType,指定使用IKAnalyzer(在schema.xml中编辑即可) 修改schema.xml,添加内容如下:
<fieldType name="text_ik" class="solr.TextField"> <analyzer class="org.wltea.analyzer.lucene.IKAnalyzer"/> </fieldType>第四步: 配置业务域,需要用到中文检索的那么type要指定上面自定义的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"/>注:这个添加的field是根据业务需要添加的,因为我后面要写solr在商城案例中的使用,那么全文搜索时,需要用到商品标题,卖点,分类等等相关检索信息,所以我这里才配置了这么几个表示商品标题,卖点,分类等的field。
启动tomcat查看:
查看商品标题分析值
到这里枯燥的配置总算搞完了,即使不懂也没事,到后面讲到的实际应用就都明白了。接下来的就是后台界面进行增删改,查(重点)的操作,以及java代码来实现增删改,查(重点)了。