SOLR加载数据,创建索引和数据时,核心数据结构的配置文件是schema.xml,该配置文件主要用于配置数据源,字段类型定义,搜索类型定义等。schema.xml的配置直接影响搜索结果的准确性与效率。
schema.xml配置节点说明:
一、
<fieldType name="string" class="solr.StrField" sortMissingLast="true" />
<fieldType name="boolean" class="solr.BoolField" sortMissingLast="true"/>
<fieldtype name="binary" class="solr.BinaryField"/>
<fieldType name="int" class="solr.TrieIntField" precisionStep="0" positionIncrementGap="0"/>
<fieldType name="float" class="solr.TrieFloatField" precisionStep="0" positionIncrementGap="0"/>
<fieldType name="long" class="solr.TrieLongField" precisionStep="0" positionIncrementGap="0"/>
<fieldType name="double" class="solr.TrieDoubleField" precisionStep="0" positionIncrementGap="0"/>
<fieldType name="tint" class="solr.TrieIntField" precisionStep="8" positionIncrementGap="0"/>
<fieldType name="tfloat" class="solr.TrieFloatField" precisionStep="8" positionIncrementGap="0"/>
<fieldType name="tlong" class="solr.TrieLongField" precisionStep="8" positionIncrementGap="0"/>
<fieldType name="tdouble" class="solr.TrieDoubleField" precisionStep="8" positionIncrementGap="0"/>
<fieldType name="date" class="solr.TrieDateField" precisionStep="0" positionIncrementGap="0"/>
<fieldType name="tdate" class="solr.TrieDateField" precisionStep="6" positionIncrementGap="0"/>
<fieldType name="pint" class="solr.IntField"/>
<fieldType name="plong" class="solr.LongField"/>
<fieldType name="pfloat" class="solr.FloatField"/>
<fieldType name="pdouble" class="solr.DoubleField"/>
<fieldType name="pdate" class="solr.DateField" sortMissingLast="true"/>
<fieldType name="sint" class="solr.SortableIntField" sortMissingLast="true" omitNorms="true"/>
<fieldType name="slong" class="solr.SortableLongField" sortMissingLast="true" omitNorms="true"/>
<fieldType name="sfloat" class="solr.SortableFloatField" sortMissingLast="true" omitNorms="true"/>
<fieldType name="sdouble" class="solr.SortableDoubleField" sortMissingLast="true" omitNorms="true"/>
<fieldType name="random" class="solr.RandomSortField" indexed="true" />[/xml]
注明了若干种搜索类型,字符串,数字,浮点,日期,布尔等。
对于文章内容搜索,很多时候我们需要自己定义搜索类型在建立索引和进行查询的时候要使用的分析器analyzer,包括分词tokenizer和过滤filter,例如:[xml]
<fieldType name="text_general" class="solr.TextField" positionIncrementGap="100">
<analyzer type="index">
<tokenizer class="solr.StandardTokenizerFactory"/>
<filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt" enablePositionIncrements="true" />
<!-- in this example, we will only use synonyms at query time
<filter class="solr.SynonymFilterFactory" synonyms="index_synonyms.txt" ignoreCase="true" expand="false"/>
-->
<filter class="solr.LowerCaseFilterFactory"/>
</analyzer>
<analyzer type="query">
<tokenizer class="solr.StandardTokenizerFactory"/>
<filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt" enablePositionIncrements="true" />
<filter class="solr.SynonymFilterFactory" synonyms="synonyms.txt" ignoreCase="true" expand="true"/>
<filter class="solr.LowerCaseFilterFactory"/>
</analyzer>
</fieldType>[/xml]
对于中文内容的搜索,我们往往要加入中文分词模块,例如MMS或者IKAnalyzer:
例如,IKAnalyzer2012的配置片段:[xml]
<!-- IKAnalyzer2012 中文分词-->
<fieldType name="text_ika" class="solr.TextField">
<analyzer type="index" positionIncrementGap="100" autoGeneratePhraseQueries="true">
<tokenizer class="org.wltea.analyzer.solr.IKTokenizerFactory" isMaxWordLength="false"/>
<filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt" enablePositionIncrements="true" />
<filter class="solr.LowerCaseFilterFactory"/>
</analyzer>
<analyzer type="query">
<tokenizer class="org.wltea.analyzer.solr.IKTokenizerFactory" isMaxWordLength="true" />
<filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt" enablePositionIncrements="true" />
<filter class="solr.SynonymFilterFactory" synonyms="synonyms.txt" ignoreCase="true" expand="true"/>
<filter class="solr.LowerCaseFilterFactory"/>
</analyzer>
</fieldType>[/xml]
MMSeg的配置片段:[xml]
<fieldType name="textComplex" class="solr.TextField" positionIncrementGap="100" >
<analyzer>
<tokenizer class="com.chenlb.mmseg4j.solr.MMSegTokenizerFactory" mode="complex" dicPath="字典物理路径"/>
<filter class="solr.LowerCaseFilterFactory"/>
</analyzer>
</fieldType>
<fieldType name="textMaxWord" class="solr.TextField" positionIncrementGap="100" >
<analyzer>
<tokenizer class="com.chenlb.mmseg4j.solr.MMSegTokenizerFactory" mode="max-word" dicPath="字典物理路径"/>
<filter class="solr.LowerCaseFilterFactory"/>
</analyzer>
</fieldType>
<fieldType name="textSimple" class="solr.TextField" positionIncrementGap="100" >
<analyzer>
<tokenizer class="com.chenlb.mmseg4j.solr.MMSegTokenizerFactory" mode="simple" dicPath="字典物理路径"/>
<filter class="solr.LowerCaseFilterFactory"/>
</analyzer>
</fieldType>[/xml]
name:定义搜索字段名
class:这个搜索字段实际使用的类与方法。在org.appache.solr.analysis包下
其他可选的属性:
sortMissingLast,sortMissingFirst两个属性是用在可以内在使用String排序的类型上,默认false
适用于字段类型:string,boolean,sint,slong,sfloat,sdouble,pdate。
sortMissingLast="true",没有该field的数据排在有该field的数据之后,而不管请求时的排序规则。
sortMissingFirst="true",排序规则与sortMissingLast相反。
class="solr.StrField" 字段类型将不被分析作为单字索引与存储。
class="solr.StrField"和class="solr.TextField"都有一个可选的属性“compressThreshold”,保证压缩到不小于一个大小(单位:char)
solr.TextField 允许用户通过分析器来定制索引和查询,分析器包括一个分词器(tokenizer)和多个过滤器(filter)
positionIncrementGap:可选属性,定义在同一个文档中此类型数据的空白间隔,避免短语匹配错误。
常见分词器:
空格分词,精确匹配。
将非字母连接符号加入兼容范围,如"-"字符,字母数字的界限符,非字母数字字符,比如 "wifi"或"wi fi"都能匹配"Wi-Fi"。
设置同义词过滤
这里同义词你自己可以设置,将同义词写到synonyms.txt文件中,这文件在conf下,格式如下:[xml]
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#-----------------------------------------------------------------------
#some test synonym mappings unlikely to appear in real input text
aaafoo => aaabar
bbbfoo => bbbfoo bbbbar
cccfoo => cccbar cccbaz
Braiding Adjustable Bracelet => woven Adjustable Bracelet
fooaaa,baraaa,bazaaa
# Some synonym groups specific to this example
GB,gib,gigabyte,gigabytes
MB,mib,megabyte,megabytes
Television, Televisions, TV, TVs
#notice we use "gib" instead of "GiB" so any WordDelimiterFilter coming
#after us won't split it into two words.
# Synonym mappings can be used for spelling correction too
pixima => pixma
a\,a => b\,b
words1,words2,words3
words1,words1
words1,words1,words1
words1,words1
words1,words1,words1[/xml]
设置禁用字过滤
solr将禁用字替换成分割符,配置在conf/stopwords.txt,例如常见的"is and or 是 的 也 在"之类。配置到stopwords.txt中后,这些字符将不被索引和搜索。
二、
配置片段例如:[xml]
<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_ika" 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="all" type="text" indexed="true" stored="false" multiValued="true"/>
</fields>[/xml]
请先分清数据源自段与搜索字段。
name:数据源字段名
type:搜索类型名例如中文ika搜索名text_ika
indexed:是否被索引,只有设置为true的字段才能进行搜索排序(earchable, sortable, facetable)
stored:是否存储内容,如果不需要存储字段值,尽量设置为false以提高效率
multiValued:是否为多值类型,SOLR允许配置多个数据源字段存储到一个搜索字段中。多个值必须为true,否则有可能抛出异常
compressed(true|false):是否使用gzip压缩存储(仅适用于compressable;TextField和StrField)
compressThreshold(
omitNorms(true|false):当为true时,字段检索时被省略相关的规范
omitTermFreqAndPositions(true|false):当为true时,省略这一领域的长远频率,位置和有效载荷
termVectors(true|false):当设置true,会存储 term vector。当使用MoreLikeThis,用来作为相似词的field应该存储起来。
termPositions:存储 term vector中的地址信息,会消耗存储开销。
termOffsets:存储 term vector 的偏移量,会消耗存储开销。
三、如何将多个数据源字段内容配置到一个搜索字段中
即multiValued多值字段又称为拷贝字段[xml]
<copyField source="summary" dest="all"/>
<copyField source="name" dest="all"/>
<copyField source="content" dest="all"/>[xml]
将源字段content,name,summary拷贝到all字段中,这种字段非常有用,合理应用能有效的增加搜索命中。结合字段权重设置,能有效的提高搜索质量。
四、设置动态搜索字段类型dynamic[xml]
<dynamicField name="*_i" type="int" indexed="true" stored="true"/>
<dynamicField name="*_s" type="string" indexed="true" stored="true"/>[/xml]
将数据源字段形如*_i的字段定义成int搜索类型
将数据源字段形如*_s的字段定义成string搜索类型
常用于偷懒。。。
五、唯一字段声明:uniqueKey
solr必须设置一个唯一字段,常设置为id。[xml]
<uniqueKey>id</uniqueKey>[/xml]
六、默认搜索字段:defaultSearchField
设置solr默认搜索的字段[xml]
<defaultSearchField>name</defaultSearchField>[/xml]
七、默认搜索操作符参数,及搜索短语间的逻辑[xml]
<solrQueryParser defaultOperator="AND|OR" />[/xml]
用AND增加准确率,用OR增加覆盖面,建议用AND,也可在搜索语句中定义。例如搜索“河西 万达”,使用AND默认搜索为“河西AND万达“