目录
本文导读
- 本文继承《Solr 4.10.3 后台管理页面介绍》
- schema.xml,在 SolrCore 的 conf 目录下,是 Solr 数据表配置文件,定义了加入索引的域,以及域的数据类型。
- schema.xml 主要包括 FieldType、Field、dynamicField、copyField 等。
FieldType (域类型定义)
- FieldType 表示域类型,相当于 Mysql 数据库中字段的数据类型。
- Field(域) 必须指定其中一个 FieldType。
- 如下所示是 schema.xml 文件中默认定义好的部分 FieldType 内容
<fieldType name="string" class="solr.StrField" sortMissingLast="true" />
<!-- boolean type: "true" or "false" -->
<fieldType name="boolean" class="solr.BoolField" sortMissingLast="true"/>
<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"/>
<!-- A Trie based date field for faster date range queries and date faceting. -->
<fieldType name="tdate" class="solr.TrieDateField" precisionStep="6" positionIncrementGap="0"/>
<!--Binary data type. The data should be sent/retrieved in as Base64 encoded Strings -->
<fieldtype name="binary" class="solr.BinaryField"/>
<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" />
<!-- 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" />
<filter class="solr.SynonymFilterFactory" synonyms="synonyms.txt" ignoreCase="true" expand="true"/>
<filter class="solr.LowerCaseFilterFactory"/>
</analyzer>
</fieldType>
........
- FieldType 标签属性包括:name、class、positionIncrementGap 等参数:
- name:定义 FieldType 的名称
- class:Solr 提供的数据类型,如上所示的 solr.TextField;solr.TextField 类型允许用户添加分词器来定制索引和查询,分析器包括一个分词器(tokenizer)和多个过滤器(filter)
- positionIncrementGap:可选属性,定义在同一个文档中此类型数据的空白间隔,避免短语匹配错误,此值相当于 Lucene 的短语查询设置 slop 值。
- FieldType 定义的时候最重要的就是定义这个域类型的数据在建立索引和进行查询的时候要使用的分析器 Analyzer,包括分词和过滤
- 索引分词器中:如上所示使用了 solr.StandardTokenizerFactory 标准分词器,solr.StopFilterFactory 停用词过滤器,solr.LowerCaseFilterFactory 小写过滤器。
- 搜索分析器中:如上所示使用了 solr.StandardTokenizerFactory 标准分词器,solr.StopFilterFactory 停用词过滤器,还用到了solr.SynonymFilterFactory 同义词过滤器。
- 当然后以后还会自己添加自定义分词器,如 IK-Analyzer 中文分词器等,配置方式和上面的 text_general 基本一致。
- sortMissingLast : 为 true时,那些在该 Field 上没有值的 documents 将被排在那些在该 Field 上有值的 documents 之后。
- sortMissingFirst:为 true 时,与 sortMissingLast 情况正好相反。如果两者都设为 false,则使用Lucene的排序。
- <analyzer type="XXX">:index 代表生成索引时使用的分词器;query 代表在查询时使用的分词器。
- <tokenizer class="XXX":分词器类,如制定 Ik-Analyzer 时是 org.wltea.analyzer.lucene.IKAnalyzer
- <filter class="XXX":分词后应用的过滤器 过滤器调用顺序和配置相同
- 如上所示 text_general 域类型在索引与检索时使用的都是 solr.StandardTokenizerFactory 标准分词器,即对英文分词效果好,对中文采用的是单字分词,所以实际项目中需要自己手动新增中文分词的类型,如 org.wltea.analyzer.lunece.IKAnalyzer 中文分词器等
Field (域定义)
- 如下所示为 schema.xml 默认定义的部分 Field(域)
- 实际项目中根据需要可以直接使用其中的 Field,对于不能满足时可以自己新增。
<field name="_version_" type="long" indexed="true" stored="true"/>
<!-- points to the root document of a block of nested documents. Required for nested
document support, may be removed otherwise
-->
<field name="_root_" type="string" indexed="true" stored="false"/>
<!-- Only remove the "id" field if you have a very good reason to. While not strictly
required, it is highly recommended. A <uniqueKey> is present in almost all Solr
installations. See the <uniqueKey> declaration below where <uniqueKey> is set to "id".
-->
<field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false" />
<field name="sku" type="text_en_splitting_tight" indexed="true" stored="true" omitNorms="true"/>
<field name="name" type="text_general" indexed="true" stored="true"/>
<field name="manu" type="text_general" indexed="true" stored="true" omitNorms="true"/>
<field name="cat" type="string" indexed="true" stored="true" multiValued="true"/>
<field name="features" type="text_general" indexed="true" stored="true" multiValued="true"/>
<field name="includes" type="text_general" indexed="true" stored="true" termVectors="true" termPositions="true" termOffsets="true" />
<field name="weight" type="float" indexed="true" stored="true"/>
<field name="price" type="float" indexed="true" stored="true"/>
<field name="popularity" type="int" indexed="true" stored="true" />
<field name="inStock" type="boolean" indexed="true" stored="true" />
<field name="store" type="location" indexed="true" stored="true"/>
<!-- Common metadata fields, named specifically to match up with
SolrCell metadata when parsing rich documents such as Word, PDF.
Some fields are multiValued only because Tika currently may return
multiple values for them. Some metadata is parsed from the documents,
but there are some which come from the client context:
"content_type": From the HTTP headers of incoming stream
"resourcename": From SolrCell request param resource.name
-->
<field name="title" type="text_general" indexed="true" stored="true" multiValued="true"/>
<field name="subject" type="text_general" indexed="true" stored="true"/>
<field name="description" type="text_general" indexed="true" stored="true"/>
<field name="comments" type="text_general" indexed="true" stored="true"/>
<field name="author" type="text_general" indexed="true" stored="true"/>
<field name="keywords" type="text_general" indexed="true" stored="true"/>
<field name="category" type="text_general" indexed="true" stored="true"/>
<field name="resourcename" type="text_general" indexed="true" stored="true"/>
<field name="url" type="text_general" indexed="true" stored="true"/>
<field name="content_type" type="string" indexed="true" stored="true" multiValued="true"/>
<field name="last_modified" type="date" indexed="true" stored="true"/>
<field name="links" type="string" indexed="true" stored="true" multiValued="true"/>
<!-- Main body of document extracted by SolrCell.
NOTE: This field is not indexed by default, since it is also copied to "text"
using copyField below. This is to save space. Use this field for returning and
highlighting document content. Use the "text" field to search the content. -->
<field name="content" type="text_general" indexed="false" stored="true" multiValued="true"/>
<!-- catchall field, containing all other searchable text fields (implemented
via copyField further on in this schema -->
<field name="text" type="text_general" indexed="true" stored="false" multiValued="true"/>
<!-- catchall text field that indexes tokens both normally and in reverse for efficient
leading wildcard queries. -->
<field name="text_rev" type="text_general_rev" indexed="true" stored="false" multiValued="true"/>
<!-- non-tokenized version of manufacturer to make it easier to sort or group
results by manufacturer. copied from "manu" via copyField -->
<field name="manu_exact" type="string" indexed="true" stored="false"/>
<field name="payloads" type="payloads" indexed="true" stored="true"/>
- name:域名称。
- type:FieldType 定义的域类型。如上所示的 content、title、name、author 等使用的 text_general 类型,而 text_general 默认使用的是 solr.StandardTokenizerFactory 标准分词器,对中文分词效果不好,所以实际开发中,通常需要自己补充。
- indexed=true/false:是否建立索引,将来需要搜索和排序时,则需要建立索引
stored=true|false
- stored=true|false:是否储存,将来需要获取值并显示时,则需要存储。注意不存储并不代表不能索引,索引与存储是两个概念。当 indexed=true、stored=alse 时,则能根据这个域进行检索,但是无法获取这个域的值。
multiValued
- multiValued=true|false:是否存储多个值。solr 允许一个 Field 存储多个值,比如存储一个用户的好友id(多个),商品的图片(多个,大图和小图),采用数组的形式。
uniqueKey(主键域)
- Solr 中默认定义唯一主键 key 为 id 域
<!-- Field to use to determine and enforce document uniqueness.
Unless this field is marked with required="false", it will be a required field -->
<uniqueKey>id</uniqueKey>
- Solr 在 删除、更新 索引时使用 id域 进行判断,创建索引时必须指定唯一约束。
- 也可以自定义唯一主键,但实际项目建议使用默认的接口
<!-- Only remove the "id" field if you have a very good reason to. While not strictly
required, it is highly recommended. A <uniqueKey> is present in almost all Solr
installations. See the <uniqueKey> declaration below where <uniqueKey> is set to "id".
-->
<field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false" />
copyField (复制域)
- copyField 复制域,可以将多个 Field 复制到一个 Field 中,以便进行统一的检索。
- 如下所示为 schema.xml 默认提供的部分 copyFIeld 域,可以自己进行设置。
<copyField source="cat" dest="text"/>
<copyField source="name" dest="text"/>
<copyField source="manu" dest="text"/>
<copyField source="features" dest="text"/>
<copyField source="includes" dest="text"/>
<copyField source="manu" dest="manu_exact"/>
<!-- Copy the price into a currency enabled field (default USD) -->
<copyField source="price" dest="price_c"/>
<!-- Text fields from SolrCell to search by default in our catch-all field -->
<copyField source="title" dest="text"/>
<copyField source="author" dest="text"/>
<copyField source="description" dest="text"/>
<copyField source="keywords" dest="text"/>
<copyField source="content" dest="text"/>
<copyField source="content_type" dest="text"/>
<copyField source="resourcename" dest="text"/>
<copyField source="url" dest="text"/>
<!-- Create a string version of author for faceting -->
<copyField source="author" dest="author_s"/>
- source:被复制整合的 Field(域)
- dest:整合后的目标 Field(域)
- 意思是当存储了如下的 title 、content 等域索引和文档之后,将来可以根据 title域 或者 content 域分别检索,也可以直接根据 上面的 "text" 域直接检索所有的 source 指定的域。
<field name="content" type="text_general" indexed="false" stored="true" multiValued="true"/>
<field name="title" type="text_general" indexed="true" stored="true" multiValued="true"/>
dynamicField( 动态域)
- 动态字段就是不用指定具体的名称,只要定义字段名称的规则,例如 "name=*_i",表示何以 "_i" 结尾的字段都被认为是符合这个定义的,例如:name_i,gender_i,school_i等。
- schema.xml 文件中默认提供的 dynamicField 字段部分如下所示。
<dynamicField name="*_i" type="int" indexed="true" stored="true"/>
<dynamicField name="*_is" type="int" indexed="true" stored="true" multiValued="true"/>
<dynamicField name="*_s" type="string" indexed="true" stored="true" />
<dynamicField name="*_ss" type="string" indexed="true" stored="true" multiValued="true"/>
<dynamicField name="*_l" type="long" indexed="true" stored="true"/>
<dynamicField name="*_ls" type="long" indexed="true" stored="true" multiValued="true"/>
<dynamicField name="*_t" type="text_general" indexed="true" stored="true"/>
<dynamicField name="*_txt" type="text_general" indexed="true" stored="true" multiValued="true"/>
<dynamicField name="*_en" type="text_en" indexed="true" stored="true" multiValued="true"/>
<dynamicField name="*_b" type="boolean" indexed="true" stored="true"/>
<dynamicField name="*_bs" type="boolean" indexed="true" stored="true" multiValued="true"/>
<dynamicField name="*_f" type="float" indexed="true" stored="true"/>
<dynamicField name="*_fs" type="float" indexed="true" stored="true" multiValued="true"/>
<dynamicField name="*_d" type="double" indexed="true" stored="true"/>
<dynamicField name="*_ds" type="double" indexed="true" stored="true" multiValued="true"/>
<!-- Type used to index the lat and lon components for the "location" FieldType -->
<dynamicField name="*_coordinate" type="tdouble" indexed="true" stored="false" />
<dynamicField name="*_dt" type="date" indexed="true" stored="true"/>
<dynamicField name="*_dts" type="date" indexed="true" stored="true" multiValued="true"/>
<dynamicField name="*_p" type="location" indexed="true" stored="true"/>
<!-- some trie-coded dynamic fields for faster range queries -->
<dynamicField name="*_ti" type="tint" indexed="true" stored="true"/>
<dynamicField name="*_tl" type="tlong" indexed="true" stored="true"/>
<dynamicField name="*_tf" type="tfloat" indexed="true" stored="true"/>
<dynamicField name="*_td" type="tdouble" indexed="true" stored="true"/>
<dynamicField name="*_tdt" type="tdate" indexed="true" stored="true"/>
<dynamicField name="*_c" type="currency" indexed="true" stored="true"/>
<dynamicField name="ignored_*" type="ignored" multiValued="true"/>
<dynamicField name="attr_*" type="text_general" indexed="true" stored="true" multiValued="true"/>
<dynamicField name="random_*" type="random" />