Solr主要配置文件之schema.xml。

        schema.xml,这个相当于数据表配置文件,它定义了加入索引的数据的数据类型的。主要包括types、fields和其他的一些缺省设置。

        注:schema.xml里有一个uniqueKey的配置,这里将id字段作为索引文档的唯一标识符,非常重要。

        <uniqueKey>id</uniqueKey>

1、FieldType(类型)

        首先需要在types结点内定义一个FieldType子节点,包括name,class,positionIncrementGap等等一些参数,name就是这个FieldType的名称,class指向org.apache.solr.analysys包里面对应的class名称,用来定义这个类型的行为。在FieldType定义的时候最重要的就是定义这个类型的数据在建立索引和进行查询的时候要使用的分析器analyzer,包括分词和过滤。例如:

       <fieldType name = "text" class = "solr.TextField" positionIncrementGap = "100">

          <analyzer type = "index">

                 <tokenizer class = "solr.WhitespaceTokenizerFactory" />

                 <! -- in this example,we will only use synonyms at query time

                <filter class = "solr.SynonymFilterFactory" synonyms = "index_synonyms.txt" ignoreCase = "true" expand = "false" />

                 -->

                <! -- Case insensitive stop word removal.

                        enablePositionIncrements = true ensures that a 'gap' is left to allow for accurate phrase queries.

                -->

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

                <filter class = "solr.WordDelimiterFilterFactory" generateWordParts = "1" generateNumberParts = "1" catenateWords = "1" catenateNumbers = "1" catenateAll = "0" splitOnCaseChange = "1" />

                <filter class = "solr.LowerCaseFilterFactory" />

                <filter class = "solr.EnglishPorterFilterFactory" protected = "protwords.txt" />

                <filter class = "solr.RemoveDuplicatesTokenFilterFactory" />

           </analyzer>

           ......

       </fieldType>

        在index的analyzer中使用solr.WhitespaceTokenizerFactory这个分词包,就是空格分词,然后使用solr.StopFilterFactory,solr.WordDelimiterFilterFactory,solr.LowerCaseFilterFactory,solr.EnglishPorterFilterFactory,solr.RemoveDuplicatesTokenFilterFactory这几个过滤器。在向索引库中添加text类型的索引的时候,Solr会首先用空格进行分词,然后把分词结果依次使用指定的过滤器进行过滤,最后剩下的结果才会加入到索引库中以备查询。Solr的analysis包并没有带支持中文分词的包。

2.Fields(字段)

        接下来的工作就是在fields结点内定义具体的字段(类似数据库中的字段),就是filed,filed定义包括name,type(为之前定义过的各种FieldType),indexed(是否被索引),stored(是否被储存),multiValued(是否有多个值)等等。

        例:

        <fields>

             <field name = "id" type = "integer" indexed = "true" stored = "true" required = "true" />

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

             <field name = "summary" type = "text"  indexed = "true" stored = "true" />

             <field name = "author" type = "string" indexed = "true" stored = "true" />

             <field name = "date" type = "date" indexed = "false" stored = "true" />

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

             <field name = "keywords" type = "keyword_text" indexed = "true" stored = "false" multiValued = "true" />

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

        </fields>

        field的定义相当重要,有几个技巧需注意一下,对可能存在多值的字段尽量设置multiValued属性为true,避免建索引是抛出错误;如果不需要存储相应字段值,尽量将stored属性设为false。

3.copyField(复制字段)

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

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

         并在拷贝字段结点处完成拷贝设置:

         <copyField source = "name" dest = "all" />

         <copyField source = "summary" dest = "all" />

         注:“拷贝字段”就是查询时候不用再输入:userName:张三anduserProfile:张三的个人简介。直接可以输入“张三”就可以将“名字”含“张三”或者简介中含“张三”的又或者“名字”和“简介”都含有“张三”的查询出来。他将需要查询的内容放在了一个字段中,并且默认查询该字段设为该字段就行了。

4.dynamicField(动态字段)

        除此之外,还可以定义动态字段,所谓动态字段就是不用指定具体的名称,只要定义字段名称的规则,例如定义一个dynamicField,name为*_1,定义他的type为text,那么在使用这个字段的时候,任何以_i结尾的字段都被认为是符合这个定义的,例如:name_i,gender_i,school_i等。

        schema.xml配置文件大体上就是这样,更多细节请参见solrwiki:

        http://wiki.apache.org/solr/SchemaXml

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值