关于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名称,用来定义这个类型的行为。
  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"/>
  6. <fieldTypename="int"class="solr.TrieIntField"precisionStep="0"omitnorms="true"
  7. positionIncrementGap="0"/>
  8. <fieldTypename="float"class="solr.TrieFloatField"precisionStep="0"omitnorms="true"
  9. positionIncrementGap="0"/>
  10. <fieldTypename="long"class="solr.TrieLongField"precisionStep="0"omitnorms="true"
  11. positionIncrementGap="0"/>
  12. <fieldTypename="double"class="solr.TrieDoubleField"precisionStep="0"omitnorms="true"
  13. positionIncrementGap="0"/>
  14. ...
  15. </types>
  16. ...
  17. </schema>

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

view plain print ?
  1. <fieldTypename="text_ws"class="solr.TextField"positionIncrementGap="100">
  2. <analyzer>
  3. <tokenizerclass="solr.WhitespacetokenizerFactory"/>
  4. </analyzer>
  5. </fieldType>
  6. <fieldTypename="text"class="solr.TextField"positionIncrementGap="100">
  7. <analyzertype="index">
  8. <!--这个分词包是空格分词,在向索引库添加text类型的索引时,Solr会首先用空格进行分词
  9. 然后把分词结果依次使用指定的过滤器进行过滤,最后剩下的结果,才会加入到索引库中以备查询
  10. 注意:Solr的analysis包并没有带支持中文的包,需要自己添加中文分词器,google下。
  11. -->
  12. <tokenizerclass="solr.WhitespacetokenizerFactory"/>
  13. <!--inthisexample,wewillonlyusesynonymsatquerytime
  14. <filterclass="solr.SynonymFilterFactory"synonyms="index_synonyms.txt"
  15. ignoreCase="true"expand="false"/>
  16. -->
  17. <!--Caseinsensitivestopwordremoval.
  18. addenablePositionIncrements=trueinboththeindexandquery
  19. analyzerstoleavea'gap'formoreaccuratephrasequeries.
  20. -->
  21. <filterclass="solr.StopFilterFactory"
  22. ignoreCase="true"
  23. words="stopwords.txt"
  24. enablePositionIncrements="true"
  25. />
  26. <filterclass="solr.WordDelimiterFilterFactory"generateWordParts="1"
  27. generateNumberParts="1"catenateWords="1"catenateNumbers="1"
  28. catenateall="0"splitOnCaseChange="1"/>
  29. <filterclass="solr.LowerCaseFilterFactory"/>
  30. <filterclass="solr.snowballPorterFilterFactory"language="English"
  31. protected="protwords.txt"/>
  32. </analyzer>
  33. <analyzertype="query">
  34. <tokenizerclass="solr.WhitespacetokenizerFactory"/>
  35. <filterclass="solr.SynonymFilterFactory"synonyms="synonyms.txt"ignoreCase="true"
  36. expand="true"/>
  37. <filterclass="solr.StopFilterFactory"
  38. ignoreCase="true"
  39. words="stopwords.txt"
  40. enablePositionIncrements="true"
  41. />
  42. <filterclass="solr.WordDelimiterFilterFactory"generateWordParts="1"
  43. generateNumberParts="1"catenateWords="0"catenateNumbers="0"
  44. catenateall="0"splitOnCaseChange="1"/>
  45. <filterclass="solr.LowerCaseFilterFactory"/>
  46. <filterclass="solr.snowballPorterFilterFactory"language="English"
  47. protected="protwords.txt"/>
  48. </analyzer>
  49. </fieldType>

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

  • name:字段名
  • type:之前定义过的各种FieldType
  • indexed:是否被索引
  • stored:是否被存储(如果不需要存储相应字段值,尽量设为false)
  • multiValued:是否有多个值(对可能存在多值的字段尽量设置为true,避免建索引时抛出错误
?
  1. <fields>
  2. <fieldname="id"type="integer"indexed="true"stored="true"required="true"/>
  3. <fieldname="name"type="text"indexed="true"stored="true"/>
  4. <fieldname="summary"type="text"indexed="true"stored="true"/>
  5. <fieldname="author"type="string"indexed="true"stored="true"/>
  6. <fieldname="date"type="date"indexed="false"stored="true"/>
  7. <fieldname="content"type="text"indexed="true"stored="false"/>
  8. <fieldname="keywords"type="keyword_text"indexed="true"stored="false"multiValued="true"/>
  9. <!--拷贝字段-->
  10. <fieldname="all"type="text"indexed="true"stored="false"multiValued="true"/>
  11. </fields>

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

以下是拷贝设置:

?
  1. <copyFieldsource="name"dest="all"/>
  2. <copyFieldsource="summary"dest="all"/>

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

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

?
  1. <dynamicFieldname="*_i"type="int"indexed="true"stored="true"/>
  2. <dynamicFieldname="*_s"type="string"indexed="true"stored="true"/>
  3. <dynamicFieldname="*_l"type="long"indexed="true"stored="true"/>
  4. <dynamicFieldname="*_t"type="text"indexed="true"stored="true"/>
  5. <dynamicFieldname="*_b"type="boolean"indexed="true"stored="true"/>
  6. <dynamicFieldname="*_f"type="float"indexed="true"stored="true"/>
  7. <dynamicFieldname="*_d"type="double"indexed="true"stored="true"/>
  8. <dynamicFieldname="*_dt"type="date"indexed="true"stored="true"/>

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

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

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

3、filedType

fieldTypename="stringclass="solr.StrFieldsortMissingLast="trueomitnorms"/>

可选的属性

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

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

fieldTypename="text="solr.TextFieldpositionIncrementGap="100">

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

name: 字段类型名
class: java类
indexed: 缺省true。 说明这个数据应被搜索和排序,如果数据没有indexed,则stored应是true。
stored: 缺省true。说明这个字段被包含在搜索结果中是合适的。如果数据没有stored,则indexed应是true。
sortMissingLast: 指没有该指定字段数据的document排在有该指定字段数据的document的后面
sortMissingFirst: 指没有该指定字段数据的document排在有该指定字段数据的document的前面
omitnorms: 字段的长度不影响得分和在索引时不做boost时,设置它为true。一般文本字段不设置为true。
termVectors: 如果字段被用来做more like this 和highlight的特性时应设置为true。
compressed: 字段是压缩的。这可能导致索引和搜索变慢,但会减少存储空间,只有StrField和TextField是可以压缩,这通常适合字段的长度超过200个字符。
multiValued: 字段多于一个值的时候,可设置为true。
positionIncrementGap: 和multiValued
一起使用,设置多个值之间的虚拟空白的数量

tokenizer="solr.WhitespacetokenizerFactory"/>

空格分词,精确匹配。

filter="solr.WordDelimiterFilterFactorygenerateWordParts="1generateNumberPartscatenateWordscatenateNumberscatenateall="0splitOnCaseChange 在分词和匹配时,考虑 "-"连字符,字母数字的界限,非字母数字字符,这样 "wifi"或"wi fi"都能匹配"Wi-Fi"。

="solr.SynonymFilterFactorysynonyms="synonyms.txtignoreCaseexpand 同义词

="solr.StopFilterFactorywords="stopwords.txtenablePositionIncrements 在禁用字(stopword)删除后,在短语间增加间隔

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

4、fields

field="idtypeindexedstoredrequired

  • name:标识而已。
  • type:先前定义的类型。
  • indexed:是否被用来建立索引(关系到搜索和排序)
  • stored:是否储存
  • compressed:[false],是否使用gzip压缩(只有TextField和StrField可以压缩)
  • mutiValued:是否包含多个值
  • omitnorms:是否忽略掉norm,可以节省内存空间,只有全文本field和need an index-time boost的field需要norm。(具体没看懂,注释里有矛盾)
  • termVectors:[false],当设置true,会存储 term vector。当使用MoreLikeThis,用来作为相似词的field应该存储起来。
  • termPositions:存储 term vector中的地址信息,会消耗存储开销。
  • termOffsets:存储 term vector 的偏移量,会消耗存储开销。
  • default:如果没有属性需要修改,就可以用这个标识下。
  • ="falsemultiValued 包罗万象(有点夸张)的field,包含所有可搜索的text fields,通过copyField实现。

    copyFieldsource="catdest"/>

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

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

    作用:

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

    ="*="ignoredmultiValued="true"/>

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

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

    5、其他一些标签

    uniqueKey>id</>

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

    defaultSearchField>text 如果搜索参数中没有指定具体的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:通过存储一个匹配给定查询的文档 id 的无序集,过滤器让 Solr 能够有效提高查询性能。缓存这些过滤器意味着对Solr的重复调用可以导致结果集的快速查找。更常见的场景是缓存一个过滤器,然后再发起后续的精炼查询,这种查询能使用过滤器来限制要搜索的文档数。
  • queryResultCache:为查询、排序条件和所请求文档的数量缓存文档 id 的有序集合。
  • documentCache:缓存Lucene Document,使用内部Lucene文档id(以便不与Solr唯一id相混淆)。由于Lucene的内部Document id 可以因索引操作而更改,这种缓存不能自热。
  • Named caches:命名缓存是用户定义的缓存,可被 Solr定制插件 所使用。
  • 其中filterCache、queryResultCache、Named caches(如果实现了org.apache.solr.search.CacheRegenerator)可以自热。

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

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

    相关文章

    php输出xml格式字符串
    J2ME Mobile 3D入门教程系列文章之一
    XML轻松学习手册
    XML入门的常见问题(一)
    XML入门的常见问题(三)
    XML轻松学习手册(2)XML概念