solr(1):简介与配置
一、简介
1.1、solr是什么
Solr 是Apache下的一个顶级开源项目,采用Java开发,它是基于Lucene的全文搜索服务器。Solr提供了比Lucene更为丰富的查询语言,同时实现了可配置、可扩展,并对索引、搜索性能进行了优化。
Solr是一个全文检索服务器,只需要进行配置就可以实现全文检索服务。
二、配置
1、在solr中默认是中文分析器,需要手工配置。配置一个FieldType,在FieldType中指定中文分析器。
2、Solr中的字段必须是先定义后使用。
2.1、配置中文分析器
1、使用IK-Analyzer。把分析器的文件夹上传到服务器。
2、把分析器的jar包添加到solr工程中
1
|
[root @localhost IK Analyzer 2012FF_hf1] # cp IKAnalyzer2012FF_u1.jar /opt/tomcat/webapps/solr/WEB-INF/lib
|
3、需要把IKAnalyzer需要的扩展词典及停用词词典、配置文件复制到solr工程的classpath
1
2
3
4
5
|
[root @localhost WEB-INF] # cd /opt/tomcat/webapps/solr/WEB-INF/
[root @localhost WEB-INF] # mkdir classes
[root @localhost WEB-INF] # cd /opt/tar/IK\ Analyzer\ 2012FF_hf1/
[root @localhost IK Analyzer 2012FF_hf1] # cp IKAnalyzer.cfg.xml ext_stopword.dic mydict.dic /opt/tomcat/webapps/solr/WEB-INF/classes/
[root @localhost IK Analyzer 2012FF_hf1] #
|
注意:扩展词典及停用词词典的字符集必须是utf-8。不能使用windows记事本编辑。
4、配置fieldType。需要在solrhome/collection1/conf/schema.xml中配置
1
|
[root @localhost IK Analyzer 2012FF_hf1] # vi /opt/solr/solrhome/collection1/conf/schema.xml
|
在其末尾添加如下:
1
2
3
|
<fieldType name= "text_ik" class= "solr.TextField" >
<analyzer class= "org.wltea.analyzer.lucene.IKAnalyzer" />
</fieldType> |
2.2、配置业字段
业务字段判断标准:
1、在搜索时是否需要在此字段上进行搜索。例如:商品名称、商品的卖点、商品的描述
2、后续的业务是否需要用到此字段。例如:商品id。
如:商品id、商品title、卖点、价格、商品图片、商品分类名称、商品描述
Solr中的业务字段:
1、id——》商品id,其他的字段与对应字段参考此。
1
|
[root @localhost IK Analyzer 2012FF_hf1] # vi /opt/solr/solrhome/collection1/conf/schema.xml
|
在其末尾添加如下:
1
2
3
4
5
6
7
8
9
10
11
|
<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_desc" type= "text_ik" indexed= "true" stored= "false" />
<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" />
<copyField source= "item_desc" dest= "item_keywords" />
|
2、重启tomcat
1、添加测试数据
2、查询
查询条件:
查所有:*:*
根据指定域(字段)查:字段名:字段值,如:item_title:测试
返回的域:
返全部:默认
返指定的:域名称,多个用逗号隔开,如:id,item_price
默认搜索的域:
如果根据指定域查的时候,本来要写字段名:字段值,结果却只写了字段值,那么会搜索不到,如果给定了默认搜索的域则可以
高亮显示:
hl.fl:高亮显示的域
hl.simple.pre:开始标签
hl.simple.post:结束标签
2.3、维护索引库
添加:添加一个json格式的文件就可以,参考上面
修改:添加一个新的文档,要求文档id和被修改文档的id一致(原理是先删除后添加)
删除:使用xml格式,有两种方法
1、根据id删除:
2、根据查询删除
本文转自我爱大金子博客51CTO博客,原文链接http://blog.51cto.com/1754966750/1931892如需转载请自行联系原作者
我爱大金子