一.solr简介
Solr是基于lucene为内核的独立的企业级搜索应用服务器,他对外提供类似于web-service的接口。用户可以通过http请求,向搜索引擎服务器提交一定格式的xml文件。
生成索引:也可以通过http get 操作提出查找请求,并得到xml格式的返回结果。
特点:solr是一个高性能,采用java5开发,基于Lucene的全文搜索服务器。同时对其进行了扩展,提供了比lucene更为丰富的查询语言,同时实现了可配置,可扩展并对查询性能进行了优化,提供了一个完善的功能管理界面,是一款非常优秀的搜索引擎。
二. 工作方式
文档通过http利用xml加到一个搜索集合中。查询集合也是通过http收到一个xml/json响应来实现。它的主要特征包括: 高效,灵活的缓存功能,垂直搜索功能,高亮显示搜索结果,通过索引复制来提高可用性,提供一套强大Data Schema来定义字段,类型和设置文本分析,提供基于web的管理界面等。
三.solr安装配置(solr-5.5.4 只有tomcat 8 以上支持)
- 下载solr安装包:solr所有版本(http://archive.apache.org/dist/lucene/solr/)
下载tomcat : https://tomcat.apache.org/ - 安装丶解压 ( 以solr-5.5.4 / apache-tomcat-8.5.24为例 )
- 配置:将解压包中 solr-5.5.4/server/solr-webapp 下的 webapp 复制到 apache-tomcat-8.5.24/webapps 目录下 改名为 solr。
这时候启动 tomcat 直接访问,会报404。原因是缺少 SLF4J包,这时候应该去solr解压包solr-5.5.4/server/lib/ext/下找到 slf4j-api-1.7.7jar包(可全部复制,防止出错)并且复制到 apache-tomcat-8.5.24/webapps/solr/WEB-INF/lib 目录下,然后重启 tomcat 继续访问。
(版本不一致会出现java.lang.NoSuchMethodError: javax.servlet.ServletInputStream.isFinished()……的错误)
如果继续报错,是因为需要配置solrhome和 solrhome的配置环境
四. 配置solrhome
编辑apache-tomcat-8.5.24/webapps/solr/WEB-INF/web.xml文件,找到以下内容,去掉注释,修改行
/本地任意目录位置,如:(solrhome)/ 保存。
<env-entry>
<env-entry-name>solr/home</env-entry-name>
<env-entry-value>D:\learn\solr-5.5.4\home</env-entry-value>
<env-entry-type>java.lang.String</env-entry-type>
</env-entry>
将 solr-5.5.4/server/solr 目录下的所有文件拷贝到web.xml指定的 solrhome 目录下,重启tomcat 访问:
http://localhost:8080/solor/index.html 或者 http://localhost:8080/solr/admin.html
五. 配置core
- 类似于数据库可以插入多个document(数据库表行)每个document拥有多个 field 数据库的列.
- solrhome下新建一个core目录 比如mycore
- 拷贝 solr-5.5.4/server/solr/configsets/basic_configs/conf 到新建目录mycore中
- 进入solr管理网页 点击 core admin 添加该core
点击Add core后 成功后 检查 mycore目录 发现多了 core.properties 和 data两个资源.
登录solr管理网站,查看是否已经添加成功
六. 配置文件理解
core/conf目录下的两个配置文件非常重要
managed-schema 主要用于配置 可以提交到该core的所有field定义,field的类型定义,唯一标识符等
常用配置如下
定义字段 _version_ 类型为long indexed="true" 会进行分词索引 stored="true"表示存储到磁盘
<field name="_version_" type="long" indexed="true" stored="true"/>
定义字段 id required="true" 表示所有的document必须添加id字段 multiValued="false" 表示是否是多值字段
<field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false" />
定义动态字段 所以_i结尾的字段都可以写入到当前的core
<dynamicField name="*_i" type="int" indexed="true" stored="true"/>
定义唯一标识符的字段
<uniqueKey>id</uniqueKey>
定义字段类型的别名
<fieldType name="string" class="solr.StrField" sortMissingLast="true" />
solrconfig.xml 主要用于配置solor的主要配置信息 比如lucene版本 缓存 数据目录 请求路径映射 等
表示lucene版本
<luceneMatchVersion>5.5.4</luceneMatchVersion>
表示数据目录 默认是data目录
<dataDir>${solr.data.dir:}</dataDir>
自动提交配置
<autoCommit>
当超过15000ms后自动提交所有数据
<maxTime>${solr.autoCommit.maxTime:15000}</maxTime>
是否马上就可以查询到
<openSearcher>false</openSearcher>
</autoCommit>
表示当路径为 /select时查询所有的数据
<requestHandler name="/select" class="solr.SearchHandler">
<!-- default values for query parameters can be specified, these
will be overridden by parameters in the request
-->
<lst name="defaults">
<str name="echoParams">explicit</str>
<int name="rows">10</int>
</lst>
</requestHandler>
七. 尝试在界面上添加数据和查询数据
添加数据
查询:参数列表
q表示查询的条件 字段名:”值” 的格式(title_s:”hello”)
fq表示filter query 过滤条件 和q是and的关系支持各种逻辑运算符 (参考https://cwiki.apache.org/confluence/display/solr/The+Standard+Query+Parser)
sort表示排序 的字段 字段名 asc|desc
start 表示从第几行开始 rows表示查询的总行数
fl表示查询显示的列 比如只需要查询 name_s,sex_i 这两列 使用,隔开
df表示默认的查询字段 一般不设置
Raw Query Parameters表示原始查询字段 可以使用 start=0&rows=10这种url的方式传入参数
wt(write type)表示写入的格式 可以使用json和xml
shards 多核同时搜索 solrhome拷贝mycore为mycore1 管理平台添加core 设置参数为 路径,路径来设置需要搜索的核
其他参考(https://cwiki.apache.org/confluence/display/solr/Common+Query+Parameters)
八. 配置中文分词器
默认solr 是没有使用中文分词器的,在搜索中文时以整个句子为关键词进行搜索的,因此我们需要配置中文的分词器。
目前比较好用的分词器 是IK 2012年停更 只支持到 Lucene4.7 所有 solr5.5 需要lucene5支持 需要修改部分源码来支持solr5.5。
修改步骤:
1. 创建一个任意的maven项目,应用lucene5 和 ik
<dependencies>
<dependency>
<groupId>com.janeluo</groupId>
<artifactId>ikanalyzer</artifactId>
<version>2012_u6</version>
<!-- 排除4.7.2的所有jar包 -->
<exclusions>
<exclusion>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-core</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-queryparser</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-analyzers-common</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 添加版本 -->
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-core</artifactId>
<version>5.5.4</version>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-queryparser</artifactId>
<version>5.5.4</version>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-analyzers-common</artifactId>
<version>5.5.4</version>
</dependency>
</dependencies>
2.在项目中,添加完整的包名和类名,必须和IK中的一致
3. 重写IKAnalyzer类中的方法和IKTokenizer类中的构造方法
IKAnalyzer类
重写后
IKTokenizer类
重写后
4. 将编译好的class文件文件替换成原始的jar包中的class文件,这时就成了一个支持solr5.5的jar包
5. 使用时直接拖入/mysoft/apache-tomcat-8.5.24/webapps/solr/WEB-INF/lib目录即可
配置分词器
将solrhome下配置文件/solrhome/mycore/conf/managed-schena中添加一个字段类型,使用ik分词器
<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>
然后将对应需要进行中文分词器的字段使用 text_ik 字段类型
<dynamicField name="*_s" type="text_ik" indexed="true" stored="true" />
重启,或者在cloud环境下重新生成collection插入数据即可实现中文分词。