zoukankan      html  css  js  c++  java
  • solr的schema.xml配置属性解释

    schema.xml做什么?

    SOLR加载数据,创建索引和数据时,核心数据结构的配置文件是schema.xml,该配置文件主要用于配置数据源,字段类型定义,搜索类型定义等。schema.xml的配置直接影响搜索结果的准确性与效率。

    <types></types>节点

    types节点主要用于搜索类型的定义,这里给出常用类型的定义。

     1 <fieldType name="string" class="solr.StrField" sortMissingLast="true" /> 2 <fieldType name="boolean" class="solr.BoolField" sortMissingLast="true"/> 3 <fieldtype name="binary" class="solr.BinaryField"/> 4 <fieldType name="int" class="solr.TrieIntField" precisionStep="0" positionIncrementGap="0"/> 5 <fieldType name="float" class="solr.TrieFloatField" precisionStep="0" positionIncrementGap="0"/> 6 <fieldType name="long" class="solr.TrieLongField" precisionStep="0" positionIncrementGap="0"/> 7 <fieldType name="double" class="solr.TrieDoubleField" precisionStep="0" positionIncrementGap="0"/> 8 <fieldType name="tint" class="solr.TrieIntField" precisionStep="8" positionIncrementGap="0"/> 9 <fieldType name="tfloat" class="solr.TrieFloatField" precisionStep="8" positionIncrementGap="0"/>10 <fieldType name="tlong" class="solr.TrieLongField" precisionStep="8" positionIncrementGap="0"/>11 <fieldType name="tdouble" class="solr.TrieDoubleField" precisionStep="8" positionIncrementGap="0"/>12 <fieldType name="date" class="solr.TrieDateField" precisionStep="0" positionIncrementGap="0"/>13 <fieldType name="tdate" class="solr.TrieDateField" precisionStep="6" positionIncrementGap="0"/>14 <fieldType name="pint" class="solr.IntField"/>15 <fieldType name="plong" class="solr.LongField"/>16 <fieldType name="pfloat" class="solr.FloatField"/>17 <fieldType name="pdouble" class="solr.DoubleField"/>18 <fieldType name="pdate" class="solr.DateField" sortMissingLast="true"/>19 <fieldType name="sint" class="solr.SortableIntField" sortMissingLast="true" omitNorms="true"/>20 <fieldType name="slong" class="solr.SortableLongField" sortMissingLast="true" omitNorms="true"/>21 <fieldType name="sfloat" class="solr.SortableFloatField" sortMissingLast="true" omitNorms="true"/>22 <fieldType name="sdouble" class="solr.SortableDoubleField" sortMissingLast="true" omitNorms="true"/>23 <fieldType name="random" class="solr.RandomSortField" indexed="true" />

    这里给出的类型有,字符串,bool,int,date等等,基本java常用的都有。下面解释一下各个参数的意思。

    name:定义搜索字段名

    class:这个搜索字段实际使用的类与方法。在org.appache.solr.analysis包下,包括了常用的类型。

    其他可选的属性: 

    sortMissingLast,sortMissingFirst两个属性是用在可以内在使用String排序的类型上,默认false,适用于字段类型:string,boolean,sint,slong,sfloat,sdouble,pdate。

    sortMissingLast="true",没有该field的数据排在有该field的数据之后,而不管请求时的排序规则,在Java中对应的意思就是,该字段为NULL,排在后面。

    sortMissingFirst="true",排序规则与sortMissingLast相反。

    positionIncrementGap:可选属性,定义在同一个文档中此类型数据的空白间隔,避免短语匹配错误。 

    在配置中,string类型的class是solr.StrField,而这个字段是不会被分析存储的,也就是说不会被分词。

    而对于文章或者长文本来说,我们必须对其进行分词才能保证搜索某些字段时能够给出正确的结果。这时我们就可以用到另外一个class,solr.TextField。它允许用户通过分析器来定制索引和查询,分析器包括一个分词器(tokenizer)和多个过滤器(filter) 。

    下面就来看一下中文分词吧,这里用的分词是IK Analyzer 2012。

    中文分词

     1 <!-- IKAnalyzer2012 --> 2 <fieldType name="text_ika" class="solr.TextField"> 3     <analyzer type="index" positionIncrementGap="100" autoGeneratePhraseQueries="true"> 4         <tokenizer class="org.wltea.analyzer.solr.IKTokenizerFactory" isMaxWordLength="false"/> 5         <!-- <filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt" enablePositionIncrements="true" /> --> 6         <!-- <filter class="solr.LowerCaseFilterFactory"/> --> 7     </analyzer> 8     <analyzer type="query"> 9         <tokenizer class="org.wltea.analyzer.solr.IKTokenizerFactory" isMaxWordLength="true" />10         <!-- <filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt" enablePositionIncrements="true" /> -->11         <!-- <filter class="solr.SynonymFilterFactory" synonyms="synonyms.txt" ignoreCase="true" expand="true"/> -->12         <!-- <filter class="solr.LowerCaseFilterFactory"/> -->13     </analyzer>14 </fieldType>

    分词用的依旧是fieldType,为的是在下面的field中能够用到。

    有两个analyzer,一个是index,一个是query,index是针对于所有,query是针对于搜索。

    可以看到我注掉了两个filter,为什么呢。

    先说简单的吧,一个是query中的同义词Filter,solr.SynonymFilterFactory,注掉他是因为当前没有一个庞大的词库能够支撑中文如此复杂的同义词量。

    另外一个是忽略大小写的Filter,这个Filter可以根据自己的需要进行添加或删除,在我们的系统中,主要处理中文,这个用处也不大。

    还有一个注掉的Filter是停词Filter,这个用处挺大的,为什么注掉呢?因为我感觉他在这里不太合适。

    解释一下:停词组件在中文分词中很重要,IK也提供了相对应的配置方法,不仅仅可以处理停词,而且还可以自定义词库。所以,我建议将停词在IK中配置而不是在schema.xml中。

    两种方法都说一下:

    第一种:在schema.xml中配置,不要注释stopword组件,并将停词文件拷贝至solrHome/core/conf目录下(注意文件的编码方式,至少保证文本文件是UTF-8格式,更加严格的,保证文本文件是无BOM格式的UTF-8编码)。

    第二种:在IK配置文件中配置,请下载一个IK分词组件,里面会有一个IKAnalyzer.cfg.xml的配置文件,拷贝到solr项目的源代码根目录下,并将stopword.dic也拷贝到根目录下,如下图所示:

    记得要导入IK的Jar包,这样,在你的文件中就可以使用IK提供的中文分词了。

     给一个我用的stopword.dic,去下载。

    IK也可以自定义词库,这个可以看一下IK的文档,很简单,将你的自定义词库的文件拷贝至根目录,并在IK配置文件中配置即可。

    <fields></fields>节点

    filed字段用于定义数据源字段所使用的搜索类型与相关设置。

     1 <field name="id" type="long" indexed="true" stored="true" multiValued="false" required="true" /> 2 <field name="name" type="text_ika" indexed="true" stored="true" multiValued="false" /> 3 <field name="address" type="text_ika" indexed="true" stored="true" multiValued="false" /> 4 <field name="description" type="text_ika" indexed="true" stored="true" multiValued="false" /> 5 <field name="saturdayMerchant" type="boolean" indexed="true" stored="true" multiValued="false" /> 6 <field name="huiMerchant" type="boolean" indexed="true" stored="true" multiValued="false" /> 7 <field name="bankMerchant" type="boolean" indexed="true" stored="true" multiValued="false" /> 8 <field name="rebate" type="int" indexed="true" stored="true" multiValued="false" /> 9 <field name="valid" type="boolean" indexed="true" stored="true" multiValued="false" />10 <field name="createTime" type="date" indexed="true" stored="true" multiValued="false" />11 <field name="priority" type="int" indexed="true" stored="true" multiValued="false" />12 <field name="telephone" type="string" indexed="true" stored="true" multiValued="false" />13 <field name="city" type="text_ika" indexed="true" stored="true" multiValued="false" />14 <field name="district_id" type="long" indexed="true" stored="true" multiValued="false" />15 <field name="district_name" type="text_ika" indexed="true" stored="true" multiValued="false" />16 <field name="merchantCategory_id" type="long" indexed="true" stored="true" multiValued="false" />17 <field name="merchantCategory_name" type="text_ika" indexed="true" stored="true" multiValued="false" />18 <field name="bank_id" type="long" indexed="true" stored="true" multiValued="true" />19 <field name="bank_name" type="text_ika" indexed="true" stored="true" multiValued="true" />20 21 <field name="all" type="text_ika" indexed="true" stored="false" multiValued="true" />

    name:数据源字段名,搜索使用到。

    type:搜索类型名例如中文ika搜索名text_ika,对应于fieldType中的name。不需要分词的字符串类型,string即可,如果需要分词,用上面配置好的分词type。

    indexed:是否被索引,只有设置为true的字段才能进行搜索排序分片(earchable, sortable, facetable)。

    stored:是否存储内容,如果不需要存储字段值,尽量设置为false以提高效率。

    multiValued:是否为多值类型,SOLR允许配置多个数据源字段存储到一个搜索字段中。多个值必须为true,否则有可能抛出异常。

    copyField节点

    如果我们的搜索需要搜索多个字段该怎么办呢?这时候,我们就可以使用copyField。代码如下:

    1 <copyField source="name" dest="all" />2 <copyField source="address" dest="all" />3 <copyField source="description" dest="all" />4 <copyField source="city" dest="all" />5 <copyField source="district_name" dest="all" />6 <copyField source="merchantCategory_name" dest="all" />7 <copyField source="bank_name" dest="all" />

    我们将所有的中文分词字段全部拷贝至all中,当我们进行全文检索是,只用搜索all字段就OK了。

    注意,这里的目标字段必须支持多值,最好不要存储,因为他只是做搜索。indexed为true,stored为false。

    copyField节点和field节点都在fields节点之内。

    问题又来了,如果需要根据不同的重要性进行区分,例如name的重要性比address大,该怎么办呢,后面再说这个问题。

    uniqueKey节点

    solr必须设置一个唯一字段,常设置为id,此唯一一段有uniqueKey节点指定。

    例如:

    1 <uniqueKey>id</uniqueKey>

    defaultSearchField节点

    默认搜索的字段,我们已经将需要搜索的字段拷贝至all字段了,在这里设为all即可。

    1 <defaultSearchField>all</defaultSearchField>

    solrQueryParser节点

    默认搜索操作符参数,及搜索短语间的逻辑,用AND增加准确率,用OR增加覆盖面,建议用AND,也可在搜索语句中定义。例如搜索“河西 万达”,使用AND默认搜索为“河西AND万达“。

    1 <solrQueryParser defaultOperator="OR"/> 
  • 相关阅读:
    IntelliJ IDEA 14.03 java 中文文本处理中的编码格式设置
    应聘感悟
    STL string分析
    CUDA SDK VolumeRender 分析 (1)
    BSP
    CUDA SDK VolumeRender 分析 (3)
    CUDA SDK VolumeRender 分析 (2)
    Windows软件发布时遇到的一些问题
    Ten Commandments of Egoless Programming (转载)
    复习下光照知识
  • 原文地址:https://www.cnblogs.com/hhcblog/p/3853255.html
Copyright © 2011-2022 走看看