Solr基础教程之Schema.xml(二)

来源:互联网 发布:js显示display 编辑:程序博客网 时间:2024/05/17 22:44

在 Solr 中,使用一个或多个 Document 来构建索引。Document 包括一个或多个 Field。Field 包括名称、内容以及告诉 Solr 如何处理内容的元数据。

例如,Field 可以包含字符串、数字、布尔值或者日期,也可以包含你想添加的任何类型,只需用在solr的配置文件中进行相应的配置即可。Field 可以使用大量的选项来描述,这些选项告诉 Solr 在索引和搜索期间如何处理内容。

下面来挨个详细介绍下这些属性的含义。

1、模式配置Schema.xml
schema.xml这个配置文件可以在你下载solr包的安装解压目录的\solr\example\solr\collection1\conf中找到,也可以在我们上篇讲到的solrhome\mycore\conf中找到,它就是solr模式关联的文件。打开这个配置文件,你会发现有详细的注释。

模式组织主要分为三个重要配置。

2、types 部分

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

<fieldType name="string" class="solr.StrField" sortMissingLast="true"/><fieldType name="boolean" class="solr.BoolField" sortMissingLast="true"/><fieldType name="int" class="solr.TrieIntField" precisionStep="0" positionIncrementGap="0"/><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" />  <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>

参数说明:

属性 描述 name 标识而已 class 和其他属性决定了这个fieldType的实际行为。 sortMissingLast 设置成true没有该field的数据排在有该field的数据之后,而不管请求时的排序规则, 默认是设置成false。 sortMissingFirst 跟上面倒过来呗。 默认是设置成false analyzer 字段类型指定的分词器 type 当前分词用用于的操作,index代表生成索引时使用的分词器,query代表在查询时使用的分词器 tokenizer 分词器类 filter 分词后应用的过滤器 过滤器调用顺序和配置相同. words=”stopwords.txt” 用来配置停止词的,stopwords.txt一般与schema.xml在同一个目录,什么叫停止词呢,比如’在’、’的’、’了’等等这类在搜索时没有实际意义的字词,我们在分词的时候会忽略掉这些。 synonyms=”synonyms.txt” 是用来配置同义词的,比如搜索’裙子’、’群’,都会搜出裙子相关产品,搜’袜’,’袜子’就会搜出袜子相关产品大致就是这个意思了。

3、fileds 部分

filed是你添加到索引文件中出现的属性名称,也就是前文介绍的,select后面跟着的字段名,而声明类型就需要用到上面的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,否则会报错的

copyField就引出了solr的一个全文检索的概念,如果我要实现一个搜索,而我要搜索的属性有很多个那么应该使用如下配置方法:

<!--  关键字搜索  --><field name="keyword" type="text_general" indexed="true" stored="false" multiValued="true"/><copyField source="seoTitle" dest="keyword"/><copyField source="title" dest="keyword"/><copyField source="subTitle" dest="keyword"/><copyField source="shopName" dest="keyword"/><copyField source="seoKeywords" dest="keyword"/><copyField source="category_name_*" dest="keyword"/><copyField source="descriptionForSearch" dest="keyword"/>

此时keyword的值就相当于包含了seoTitle、title、subTitle、shopName、seoKeywords、category_name_*、descriptionForSearch的所有内容。
如果这里不理解,可以看下这篇帖子里面介绍的概念:全文索引概念

另外这里大家一定会好奇,keyword使用的type=”text_general”会达到什么效果,其实很简单,text_general是我在上面定义的分词器,能够根据一定的格式分词,利于搜索,如果不实用text_general,那么就达不到全文检索,模糊搜索的目的了。

字段属性说明:

属性 描述 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_ 是一个特殊字段,不能删除,是记录当前索引版本号的.

4、其他配置

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

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

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

1 0
原创粉丝点击