微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

solr学习(3.2)-Solr配置文件schema.xml和solrconfig.xml分析

一、字段配置(schema)

schema.xml位于solr/conf/目录下,类似于数据表配置文件

定义了加入索引的数据的数据类型,主要包括type、fields和其他的一些缺省设置。

1、先来看下type节点,这里面定义FieldType子节点,包括name,class,positionIncrementGap等一些参数。

  • name:就是这个FieldType的名称
  • class:指向org.apache.solr.analysis包里面对应的class名称,用来定义这个类型的行为。
[html] view plain copy
  1. <schemaname="example"version="1.2">
  2. types>
  3. fieldTypename="string"class="solr.StrField"sortMissingLast="true"omitnorms="true"/>
  4. fieldTypename="boolean"class="solr.BoolField"sortMissingLast="true"omitnorms="true"/>
  5. fieldtypename="binary"class="solr.BinaryField"fieldTypename="int"class="solr.TrieIntField"precisionStep="0"omitnorms="true"
  6. positionIncrementGap="0"fieldTypename="float"class="solr.TrieFloatField"precisionStep="0"omitnorms="true"
  7. fieldTypename="long"class="solr.TrieLongField"precisionStep="0"omitnorms="true"
  8. fieldTypename="double"class="solr.TrieDoubleField"precisionStep="0"omitnorms="true"
  9. ...
  10. </schema>

[xhtml] copy
    >

必要的时候fieldType还需要自己定义这个类型的数据在建立索引和进行查询的时候要使用的分析器analyzer,包括分词和过滤,如下:

copy
    fieldTypename="text_ws"class="solr.TextField"positionIncrementGap="100"analyzertokenizerclass="solr.WhitespacetokenizerFactory"fieldTypefieldTypename="text"class="solr.TextField"positionIncrementGap="100"analyzertype="index" <!--这个分词包是空格分词,在向索引库添加text类型的索引时,Solr会首先用空格进行分词
  1. 然后把分词结果依次使用指定的过滤器进行过滤,最后剩下的结果,才会加入到索引库中以备查询
  2. 注意:Solr的analysis包并没有带支持中文的包,需要自己添加中文分词器,google下。
  3. -- <!--inthisexample,wewillonlyusesynonymsatquerytime
  4. filterclass="solr.SynonymFilterFactory"synonyms="index_synonyms.txt"
  5. ignoreCase="true"expand="false" -- <!--Caseinsensitivestopwordremoval.
  6. addenablePositionIncrements=trueinboththeindexandquery
  7. analyzerstoleavea'gap'formoreaccuratephrasequeries.
  8. filterclass="solr.StopFilterFactory"
  9. ignoreCase="true"
  10. words="stopwords.txt"
  11. enablePositionIncrements="true"
  12. filterclass="solr.WordDelimiterFilterFactory"generateWordParts="1"
  13. generateNumberParts="1"catenateWords="1"catenateNumbers="1"
  14. catenateall="0"splitOnCaseChange="1"filterclass="solr.LowerCaseFilterFactory"filterclass="solr.snowballPorterFilterFactory"language="English"
  15. protected="protwords.txt"analyzertype="query"filterclass="solr.SynonymFilterFactory"synonyms="synonyms.txt"ignoreCase="true"
  16. expand="true" generateNumberParts="1"catenateWords="0"catenateNumbers="0"
  17. >


2、再来看下fields节点内定义具体的字段(类似数据库的字段)

含有以下属性

  • name:字段名
  • type:之前定义过的各种FieldType
  • indexed:是否被索引
  • stored:是否被存储(如果不需要存储相应字段值,尽量设为false)
  • multiValued:是否有多个值(对可能存在多值的字段尽量设置为true,避免建索引时抛出错误

copy
    fieldsfieldname="id"type="integer"indexed="true"stored="true"required="true"fieldname="name"type="text"indexed="true"stored="true"fieldname="summary"type="text"indexed="true"stored="true"fieldname="author"type="string"indexed="true"stored="true"fieldname="date"type="date"indexed="false"stored="true"fieldname="content"type="text"indexed="true"stored="false"fieldname="keywords"type="keyword_text"indexed="true"stored="false"multiValued="true" <!--拷贝字段-->
  1. fieldname="all"type="text"indexed="true"stored="false"multiValued="true">


3、建议建立一个拷贝字段,将所有的 全文本 字段复制到一个字段中,以便进行统一的检索:

以下是拷贝设置:

copy
    copyFieldsource="name"dest="all"copyFieldsource="summary"dest="all"/>

4、动态字段,没有具体名称的字段,用dynamicField字段

如:name为*_i,定义它的type为int,那么在使用这个字段的时候,任务以_i结果的字段都被认为符合这个定义。如name_i,school_i

copy
    dynamicFieldname="*_i"type="int"indexed="true"stored="true"dynamicFieldname="*_s"type="string"indexed="true"stored="true"dynamicFieldname="*_l"type="long"indexed="true"stored="true"dynamicFieldname="*_t"type="text"indexed="true"stored="true"dynamicFieldname="*_b"type="boolean"indexed="true"stored="true"dynamicFieldname="*_f"type="float"indexed="true"stored="true"dynamicFieldname="*_d"type="double"indexed="true"stored="true"dynamicFieldname="*_dt"type="date"indexed="true"stored="true"/>


二. schema.xml文档注释中的信息

1、为了改进性能,可以采取以下几种措施:

2、schema名字

<schemaname="example"version="1.2">

3、filedType

<fieldTypename="string"class="solr.StrField"sortMissingLast="true"omitnorms="true"/>

可选的属性

StrField类型不被分析,而是被逐字地索引/存储。

StrField和TextField都有一个可选的属性“compressthreshold”,保证压缩到不小于一个大小(单位:char)

<fieldTypename="text"class="solr.TextField"positionIncrementGap="100">

solr.TextField 允许用户通过分析器来定制索引和查询,分析器包括 一个分词器(tokenizer)和多个过滤器(filter)

<tokenizerclass="solr.WhitespacetokenizerFactory"/>

空格分词,精确匹配。

<filterclass="solr.WordDelimiterFilterFactory"generateWordParts="1"generateNumberParts="1"catenateWords="1"catenateNumbers="1"catenateall="0"splitOnCaseChange="1"/>

在分词和匹配时,考虑 "-"连字符,字母数字的界限,非字母数字字符,这样 "wifi"或"wi fi"都能匹配"Wi-Fi"。

<filterclass="solr.SynonymFilterFactory"synonyms="synonyms.txt"ignoreCase="true"expand="true"/>

同义词

<filterclass="solr.StopFilterFactory"ignoreCase="true"words="stopwords.txt"enablePositionIncrements="true"/>

在禁用字(stopword)删除后,在短语间增加间隔

stopword:即在建立索引过程中(建立索引和搜索)被忽略的词,比如is this等常用词。在conf/stopwords.txt维护。

4、fields

<fieldname="id"type="string"indexed="true"stored="true"required="true"/>

<fieldname="text"type="text"indexed="true"stored="false"multiValued="true"/>

包罗万象(有点夸张)的field,包含所有可搜索的text fields,通过copyField实现。

<copyFieldsource="cat"dest="text"/>

< copyField source =" name " dest =" text " />
< copyField source =" manu " dest =" text " />
< copyField source =" features " dest =" text " />
< copyField source =" includes " dest =" text " />

添加索引时,将所有被拷贝field(如cat)中的数据拷贝到text field中

作用:

<dynamicFieldname="*_i"type="int"indexed="true"stored="true"/>

如果一个field的名字没有匹配到,那么就会用动态field试图匹配定义的各种模式。

<dynamicFieldname="*"type="ignored"multiValued="true"/>

如果通过上面的匹配都没找到,可以定义这个,然后定义个type,当String处理。(一般不会发生)

但若不定义,找不到匹配会报错。

5、其他一些标签

<uniqueKey>id</uniqueKey>

文档的唯一标识, 必须填写这个field(除非该field被标记required="false"),否则solr建立索引报错。

<defaultSearchField>text</defaultSearchField>

如果搜索参数中没有指定具体的field,那么这是认的域。

<solrQueryParserdefaultOperator="OR"/>

配置搜索参数短语间的逻辑,可以是"AND|OR"。

三、solrconfig.xml

1、索引配置

mainIndex 标记段定义了控制Solr索引处理的一些因素.

2、查询处理配置

query标记段中以下一些与缓存无关的特性:

query部分负责定义与在Solr中发生的时间相关的几个选项:

概念:Solr(实际上是Lucene)使用称为Searcher的Java类来处理Query实例。Searcher将索引内容相关的数据加载到内存中。根据索引、cpu已经可用内存的大小,这个过程可能需要较长的一段时间。要改进这一设计和显著提高性能,Solr引入了一张“温暖”策略,即把这些新的Searcher联机以便为现场用户提供查询服务之前,先对它们进行“热身”。

query中的智能缓存:

其中filterCache、queryResultCache、Named caches(如果实现了org.apache.solr.search.CacheRegenerator)可以自热。

每个缓存声明都接受最多四个属性

对于所有缓存模式而言,在设置缓存参数时,都有必要在内存、cpu和磁盘访问之间进行均衡。统计信息管理页(管理员界面的Statistics)对于分析缓存的 hit-to-miss 比例以及微调缓存大小的统计数据都非常有用。而且,并非所有应用程序都会从缓存受益。实际上,一些应用程序反而会由于需要将某个永远也用不到的条目存储在缓存中这一额外步骤而受到影响。

原文地址:https://www.jb51.cc/xml/293974.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。