[关闭]
@tsihedeyo 2014-09-10T10:40:58.000000Z 字数 5273 阅读 4319

Solr 当前的基本使用

solr egeio


当前Solr环境配置

samba登陆solr服务器

用户名:yyj
密码: yyj
服务器IP:115.29.173.222
samba配置搜wiki

登陆后2个文件夹 Share/Solr/, Share/third_party_services_bak为同步的git目录,Solr/为服务器上运行的Solr目录,更改Solr的配置直接在Solr/下更改。

solr安装目录

安装目录为: /opt/solr ,为软链接,

`solr -> /home/yyj/share/third_party_services_bak/solr/solr-4.6.0/`

solr git目录结构

 git地址: `https://github.com/yuancheng26/third_party_services.git`
 git文件夹结构:
    进入git目录下solr/,
    其中
       solr-4.6.0/ : solr运行的目录
       start_solr_as_a_service : solr服务启动、停止脚本

solr目录结构

即分析solr-4.6.0目录结构。

比较重要的几个目录列举如下:

solr_4.6.0/example/solr/collection1 : 当前solr的core,所有当前核的配置都在其下的conf/下,索引文件在其data/下。
solr-4.6.0\example\solr-webapp\webapp : jetty下运行的war解压文件;要更改solr引用的插件库,如增加中文分词IK库,则将IK分词库到该目录下WEB-INF/lib
solr-4.6.0\examplestart.jar : 脚本中启动服务用的jar包,启动过程可见git目录下的启动、停止脚本
* solr-4.6.0\example\webapps\solr.war : jetty运行的war文件,一般不改, 直接改 solr-4.6.0\example\solr-webapp\webapp貌似就可以了。


启动、停止、访问控制台、索引清空

启动:service solr start
停止:service solr stop
访问控制台:
http://115.29.173.222:8983/solr/
admin使用:solr 控制台基本使用

清空索引: 清空索引链接
http://115.29.173.222:8983/solr/collection1/update?stream.body=%3Cdelete%3E%3Cquery%3E*:*%3C/query%3E%3C/delete%3E&commit=true
更改了索引结构的时候清空索引。


schema.xml配置

schema.xml地址:solr_4.6.0/example/solr/collection1/conf/schema.xml

示例:

<field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false" />
<field name="item_type" type="string" indexed="false" stored="false"/>
<field name="name" type="text_ik" indexed="true" stored="true"/>

field: 字段名,对solr来说每个上传的文档都是各种字段(类似Key-value)的组合。

name:字段名
type:字段类型,决定了分析方式,比如string类型在匹配时只有完全匹配上才认为匹配上了,而text_ik则被定义为要按照各种分词以及过滤去进行分析,可支持分词匹配。
indexed:是否为索引字段,用户是否会在查询中用到该字段,会则true,否则false。如上面用户不大可能内部的doc_id进行查询,但会依据name来进行查询。
stored:是否存储该字段原文,若需要返回给用户该原文,则true,否则false。如公司查询中一般只返回doc_id(file_id),则id中store为true,其他都应为false。但是在highlight中需要返回相关的高亮字段,如name,user_name,text等,则相应设置为false。

uniqueKey,唯一标识符

copyField

用于将某个字段复制到另一个字段中,示例

<copyField source="content" dest="text"/>
<copyField source="description" dest="text"/>

content和description基本可以无差别对待,因为将他们copy到同一字段text中去,text设置为 multiValued="true"。

types 字段类型

字段类型集合,系统自带以及自定义。现在用的基本都是text_ik,自定义示例

<fieldType name="text_ik" class="solr.TextField" >
    <analyzer type="index">
    <tokenizer class="org.wltea.analyzer.lucene.IKAnalyzerTokenizerFactory" useSmart="false"/>
    <filter class="solr.StopFilterFactory" ignoreCase="true" words="lang/stopwords_en_ch.txt" enablePositionIncrements="true" />
    <!-- dogs to dog; helped to help -->
    <filter class="solr.SnowballPorterFilterFactory" language="English" />
    <!-- enterprise cut into en ent enter , min1 because for chinese name ,the IK always cut into singel word if min>1 , you can never find out by the name-->
    <filter class="solr.EdgeNGramFilterFactory" minGramSize="1" maxGramSize="25"/>
    <filter class="solr.RemoveDuplicatesTokenFilterFactory"/>
  </analyzer>
  <analyzer type="query">
    <tokenizer class="org.wltea.analyzer.lucene.IKAnalyzerTokenizerFactory" useSmart="true"/>
    <filter class="solr.SnowballPorterFilterFactory" language="English" />
    <filter class="solr.RemoveDuplicatesTokenFilterFactory"/>
    <filter class="solr.SynonymFilterFactory" synonyms="synonyms.txt" ignoreCase="true" expand="false"/>
  </analyzer>
</fieldType>

一个字段类型可对于构建索引和查询字段的分析进行不同的行为设定。
如text_ik对于构建索引处理:

<analyzer type="index">
    <tokenizer class="org.wltea.analyzer.lucene.IKAnalyzerTokenizerFactory" useSmart="false"/>
    <filter class="solr.StopFilterFactory" ignoreCase="true" words="lang/stopwords_en_ch.txt" enablePositionIncrements="true" />
    <!-- dogs to dog; helped to help -->
    <filter class="solr.SnowballPorterFilterFactory" language="English" />
    <!-- enterprise cut into en ent enter , min1 because for chinese name ,the IK always cut into singel word if min>1 , you can never find out by the name-->
    <filter class="solr.EdgeNGramFilterFactory" minGramSize="1" maxGramSize="25"/>
    <filter class="solr.RemoveDuplicatesTokenFilterFactory"/>
  </analyzer>

解释:

  1. tokenizer,分词器设定为IK
  2. filter,过滤器分别经过,停止字过滤、snowball过滤(英文时态处理dogs->dog,helped->help)、Ngram过滤(enterprise -> en ent enter ),RemoveDuplicatesToken

可通过设定类型分词方式时可用控制台ananlysis进行分析。http://115.29.173.222:8983/solr/#/collection1/analysis

中文分词IK的加入参见 IK中文分词


中文分词IK

中文分词IK的加入参见 IK中文分词
库加入solr-4.6.0\example\solr-webapp\webapp\WEB-INF\lib


查询 standard/dismax/edismax

dismax支持多个字段查询,且可设定字段匹配的权重。
使用示例

defType = dismax
mm = 50%
qf = features^2 name^3  (即权重比feature:name = 2:3)
q = +"apache solr" search server

edismax是dismax的扩展。现在功能不多,因而在search中使用的是dismax。

详细看 solr dismax,对dismax的详细解释。

统计频率(term frequency)

统计关键字在单个文档中出现的频率,要统计关键字在所有文档中出现的频率,可使用facet.

term frequency实现使用了function query.
在返回的时候进行计算统计,即在返回参数fl中设置为fl=id,freq:termfreq(text,'测试')
termfreq(text,'测试'):统计'测试'在该文档的text字段内容里出现的次数。
对于多个term的Or查询,用sum进行统计,termfreq不支持phrase的统计。
即统计'测试 OR java',使用 freq:sum(termfreq(text,'测试'),termfreq(text,'java'))

其他可参见terfreq


高亮查询参数

hl.fl 
hl.fl是说明你要关键字的摘要在那个field中取,我们一般是content字段。

hl.useFastVectorHighlighter
该参数很重要,如果你不看代码,是很难发现他的好处,默认是false,即文本段的划分是按每50个字符来划分,然后在这个50个字符中取关键字相关的摘要,摘要长度为100,参考后面的参数(hf.fragsize),如果我们在参数中指定值为true,那么SOLR会根据关键词的在文本中的偏移量来计算摘要信息,前提是你的field要加上 termPositions="true" termOffsets="true"这两项。

hl.snippets
hl.snippets参数是返回高亮摘要的段数,因为我们的文本一般都比较长,含有搜索关键字的地方有多处,如果hl.snippets的值大于1的话,会返回多个摘要信息,即文本中含有关键字的几段话,默认值为1,返回含关键字最多的一段描述。solr会对多个段进行排序。

hl.fragsize
hl.fragsize参数是摘要信息的长度。默认值是100,这个长度是出现关键字的位置向前移6个字符,再往后100个字符,取这一段文本。

可参见高亮参数1高亮参数2

---

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注