利用SOLR搭建企业搜索平台 之——模式配置Schema.xml

来源:http://blog.csdn.net/awj3584/article/details/16963525

schema.xml这个配置文件可以在你下载solr包的安装解压目录的\solr\example\solr\collection1\conf中找到,它就是solr模式关联的文件。打开这个配置文件,你会发现有详细的注释。模式组织主要分为三个重要配置

1. types 部分

是一些常见的可重用定义,定义了 Solr(和 Lucene)如何处理 Field。也就是添加到索引中的xml文件属性中的类型,如int、text、date等.

 1 <fieldType name="string" class="solr.StrField" sortMissingLast="true"/>
 2 
 3 <fieldType name="boolean" class="solr.BoolField" sortMissingLast="true"/>
 4 
 5 <fieldType name="int" class="solr.TrieIntField" precisionStep="0" positionIncrementGap="0"/>
 6 
 7 <fieldType name="text_general" class="solr.TextField" positionIncrementGap="100">
 8 
 9 <analyzer type="index">
10 
11   <tokenizer class="solr.StandardTokenizerFactory"/>
12 
13   <filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt" enablePositionIncrements="true" />
14 
15   <filter class="solr.LowerCaseFilterFactory"/>
16 
17 </analyzer>
18 
19 <analyzer type="query">
20 
21   <tokenizer class="solr.StandardTokenizerFactory"/>
22 
23   <filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt" enablePositionIncrements="true" />
24 
25   <filter class="solr.SynonymFilterFactory" synonyms="synonyms.txt" ignoreCase="true" expand="true"/>
26 
27   <filter class="solr.LowerCaseFilterFactory"/>
28 
29 </analyzer>
30 
31 </fieldType>

 

参数说明:

属性

描述

name

标识而已

class

和其他属性决定了这个fieldType的实际行为。

sortMissingLast

设置成true没有该field的数据排在有该field的数据之后,而不管请求时的排序规则, 默认是设置成false。

sortMissingFirst

跟上面倒过来呗。 默认是设置成false

analyzer

字段类型指定的分词器

type

当前分词用用于的操作.index代表生成索引时使用的分词器query代码在查询时使用的分词器

tokenizer

分词器类

filter

分词后应用的过滤器  过滤器调用顺序和配置相同.

2. fileds

是你添加到索引文件中出现的属性名称,而声明类型就需要用到上面的types

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

<field name="path" type="text_smartcn" indexed="false" stored="true" multiValued="false" termVector="true" />

<field name="content" type="text_smartcn" indexed="false" stored="true" multiValued="false" termVector="true"/>

<field name ="text" type ="text_ik" indexed ="true" stored ="false" multiValued ="true"/>

<field name ="pinyin" type ="text_pinyin" indexed ="true" stored ="false" multiValued ="false"/>

<field name="_version_" type="long" indexed="true" stored="true"/>

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

<dynamicField name="*_l" type="long" indexed="true" stored="true"/>

<dynamicField name="*_s" type="string" indexed="true" stored="true" />

<copyField source="content" dest="pinyin"/>

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

<copyField source="pinyin" dest="text"/>

 

field: 固定的字段设置

dynamicField: 动态的字段设置,用于后期自定义字段,*号通配符.例如: test_i就是int类型的动态字段.

copyField:一般用于检索时用的字段。这样就只对这一个字段进行索引分词就行了copyField的dest字段如果有多个source一定要设置multiValued=true,否则会报错的

 

属性

描述

name

字段类型名

class

java类名

indexed

缺省true。 说明这个数据应被搜索和排序,如果数据没有indexed,则stored应是true。

stored

缺省true。说明这个字段被包含在搜索结果中是合适的。如果数据没有stored,则indexed应是true。

omitNorms

字段的长度不影响得分和在索引时不做boost时,设置它为true。

一般文本字段不设置为true。

termVectors

如果字段被用来做more like this 和highlight的特性时应设置为true。

compressed

字段是压缩的。这可能导致索引和搜索变慢,但会减少存储空间,只有StrField和TextField是可以压缩,这通常适合字段的长度超过200个字符。

multiValued

字段多于一个值的时候,可设置为true。

positionIncrementGap

和multiValued一起使用,设置多个值之间的虚拟空白的数量

字段属性说明:

注意:_version_ 是一个特殊字段,不能删除,是记录当前索引版本号的.

3. 其他配置

uniqueKey: 唯一键,这里配置的是上面出现的fileds,一般是id、url等不重复的。在更新、删除的时候可以用到。

defaultSearchField:默认搜索属性,如q=solr就是默认的搜索那个字段

solrQueryParser:查询转换模式,是并且还是或者(AND/OR必须大写)

 

来源:http://blog.csdn.net/zx13525079024/article/details/25043447

 1.fieldtype节点

          fieldtype节点主要用来定义数据类型。   

<fieldType name="string" sortMissingLast="true" class="solr.StrField"/>
<!-- boolean type: "true" or "false" -->
<fieldType name="boolean" sortMissingLast="true" class="solr.BoolField"/>

 

 name指定的是节点定义的名称

 class指向org.apache.solr.analysis中定义的类型名称

 fieldtype还可以自己定义当前类型建立索引和查询数据的时候使用的查询分析器。

 tokenizer指定分词器

 filter指定过滤器

<fieldType name="text_general" class="solr.TextField" positionIncrementGap="100">
  <analyzer type="index">
       <tokenizer class="solr.StandardTokenizerFactory"/>
       <filter class="solr.StopFilterFactory" words="stopwords.txt" ignoreCase="true"/>
       <filter class="solr.LowerCaseFilterFactory"/>
  </analyzer>
  <analyzer type="query">
       <tokenizer class="solr.StandardTokenizerFactory"/>
         <filter class="solr.StopFilterFactory" words="stopwords.txt" ignoreCase="true"/>
         <filter class="solr.SynonymFilterFactory" ignoreCase="true" expand="true" synonyms="synonyms.txt"/>
         <filter class="solr.LowerCaseFilterFactory"/>
      </analyzer>
</fieldType>

 

 positionIncrementGap:可选属性,定义在同一个文档中此类型数据的空白间隔,避免短语匹配错误。
 positionIncrementGap=100  只对 multiValue = true 的fieldType有意义。


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

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

 

2.field节点

  field节点指定建立索引和查询数据的字段。

  name代表数据字段名称

   type代表数据类型,也就是之前定义的fieldtype

   indexed代表是否被索引

   stored代表是否被存储

   multiValued是否有多个值,如果字段可能有多个值,尽可能设为true

    _version节点和root节点是必须保留的,不能删除

<field name="_version_" stored="true" indexed="true" type="long"/>
<field name="_root_" stored="false" indexed="true" type="string"/>
<field name="ProductCode" stored="true" indexed="true" type="string" multiValued="false" required="true"/>
<field name="ProductName" stored="true" indexed="true" type="text_general"/>

     3.copyfield节点

             通过这个节点,可以把一个字段的值复制到另一个字段中,也可以把多个字段的值同时复制到另一个字段中,这样搜索的时候都可以根据一个字段来进行搜索。

<copyField source="ProductName" dest="text"/>
<copyField source="ProductCode" dest="text"/> 

    4.dynamicField节点

              dynamicField表示动态字段,可以动态定义一个字段,只要符合规则的字段都可以

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

 

    5.其他节点

       uniquekey节点是文档的唯一标示,相当于主键,每次更新,删除的时候都根据这个字段来进行操作。必须填写       

 <uniqueKey>ProductCode</uniqueKey>

 

       defaultSearchField指定搜索的时候默认搜索字段的值,

 <defaultSearchField > text </ defaultSearchField >

 

       solrQueryParser指定搜索时多个词之间的关系,可以是or,and两种  

<solrQueryParser defaultOperator="OR" />

  

     6.性能优化

            
         将所有只用于搜索的,而不需要作为结果的field(特别是一些比较大的field)的stored设置为false
         将不需要被用于搜索的,而只是作为结果返回的field的indexed设置为false, 删除所有不必要的copyField声明
         为了索引字段的最小化和搜索的效率,将所有的 text fields的index都设置成false,然后使用copyField将他们都复制到一个总的 text field上,

          然后进行搜索。

posted @ 2014-12-31 14:00  _NullPointer  阅读(254)  评论(0编辑  收藏  举报