`
chenzehe
  • 浏览: 533945 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Solr 配置 SchemaXml

 
阅读更多

The schema.xml file contains all of the details about which fields your documents can contain, and how those fields should be dealt with when adding documents to the index, or when querying those fields.

schema.xml位于solr/conf/目录下,类似于数据表配置文件,定义了加入索引的数据的数据类型,主要包括type、fields和其他的一些缺省设置。

Data Types

The <types> section allows you to define a list of <fieldtype> declarations you wish to use in your schema, along with the underlying Solr class that should be used for that type, as well as the default options you want for fields that use that type.

types节点,这里面定义FieldType子节点,包括name,class,positionIncrementGap等一些参数。

  • name:就是这个FieldType的名称。
  • class:指向org.apache.solr.analysis包里面对应的class名称,用来定义这个类型的行为。
    <schema name="example" version="1.2">  
      <types>  
        <fieldType name="string" class="solr.StrField" sortMissingLast="true" omitNorms="true"/>  
        <fieldType name="boolean" class="solr.BoolField" sortMissingLast="true" omitNorms="true"/>  
        <fieldtype name="binary" class="solr.BinaryField"/>  
        <fieldType name="int" class="solr.TrieIntField" precisionStep="0" omitNorms="true"   
                                                                    positionIncrementGap="0"/>  
        <fieldType name="float" class="solr.TrieFloatField" precisionStep="0" omitNorms="true"   
                                                                    positionIncrementGap="0"/>  
        <fieldType name="long" class="solr.TrieLongField" precisionStep="0" omitNorms="true"   
                                                                    positionIncrementGap="0"/>  
        <fieldType name="double" class="solr.TrieDoubleField" precisionStep="0" omitNorms="true"   
                                                                    positionIncrementGap="0"/>  
      ...  
      </types>  
      ...  
    </schema>  

 必要的时候fieldType还需要自己定义这个类型的数据在建立索引和进行查询的时候要使用的分析器analyzer,包括分词和过滤,如下:

    <fieldType name="text_ws" class="solr.TextField" positionIncrementGap="100">  
      <analyzer>  
        <tokenizer class="solr.WhitespaceTokenizerFactory"/>  
      </analyzer>  
    </fieldType>  
    <fieldType name="text" class="solr.TextField" positionIncrementGap="100">  
      <analyzer type="index">  
        <!--这个分词包是空格分词,在向索引库添加text类型的索引时,Solr会首先用空格进行分词  
             然后把分词结果依次使用指定的过滤器进行过滤,最后剩下的结果,才会加入到索引库中以备查询。  
          注意:Solr的analysis包并没有带支持中文的包,需要自己添加中文分词器,google下。    
         -->  
        <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.  
              add enablePositionIncrements=true in both the index and query  
              analyzers to leave a 'gap' for more 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.SnowballPorterFilterFactory" language="English"   
                                                           protected="protwords.txt"/>  
        </analyzer>  
        <analyzer type="query">  
          <tokenizer class="solr.WhitespaceTokenizerFactory"/>  
            <filter class="solr.SynonymFilterFactory" synonyms="synonyms.txt" ignoreCase="true"   
                                                                              expand="true"/>  
            <filter class="solr.StopFilterFactory"  
                    ignoreCase="true"  
                    words="stopwords.txt"  
                    enablePositionIncrements="true"  
                    />  
            <filter class="solr.WordDelimiterFilterFactory" generateWordParts="1"   
                    generateNumberParts="1" catenateWords="0" catenateNumbers="0"   
                                            catenateAll="0" splitOnCaseChange="1"/>  
            <filter class="solr.LowerCaseFilterFactory"/>  
            <filter class="solr.SnowballPorterFilterFactory" language="English"   
                                                             protected="protwords.txt"/>  
          </analyzer>  
    </fieldType>  
 

Fields

The <fields> section is where you list the individual <field> declarations you wish to use in your documents. Each <field> has a name that you will use to reference it when adding documents or executing searches, and an associated type which identifies the name of the fieldtype you wish to use for this field. There are various field options that apply to a field. These can be set in the field type declarations, and can also be overridden at an individual field's declaration.

 

fields节点内定义具体的字段(类似数据库的字段),含有以下属性:

  • name:字段名
  • type:之前定义过的各种FieldType
  • indexed:是否被索引
  • stored:是否被存储(如果不需要存储相应字段值,尽量设为false)
  • multiValued:是否有多个值(对可能存在多值的字段尽量设置为true,避免建索引时抛出错误)
    <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>  

Copy Fields

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

 以下是拷贝设置:

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

 以下为一个完整的copyfield定义:

<schema name="eshequn.post.db_post.0" version="1.1"
    xmlns:xi="http://www.w3.org/2001/XInclude">
     <fields>
     	<!-- for title -->
        <field name="t" type="text" indexed="true" stored="false" />
        <!-- for abstract -->
        <field name="a" type="text" indexed="true" stored="false" />
        <!-- for title and abstract -->
        <field name="ta" type="text" indexed="true" stored="false" multiValued="true"/>
    </fields>
    <copyField source="t" dest="ta" />
    <copyField source="a" dest="ta" />
</schema>Copy Fields

字段t是文章的标题,字段a是文章的摘要,字段ta是文章标题和摘要的联合。添加索引文档时,只需要传入t和a字段的内容,solr会自动索引ta字段。

Dynamic fields

One of the powerful features of Lucene is that you don't have to pre-define every field when you first create your index. Even though Solr provides strong datatyping for fields, it still preserves that flexibility using "Dynamic Fields". Using <dynamicField> declarations, you can create field rules that Solr will use to understand what datatype should be used whenever it is given a field name that is not explicitly defined, but matches a prefix or suffix used in a dynamicField.

For example the following dynamic field declaration tells Solr that whenever it sees a field name ending in "_i" which is not an explicitly defined field, then it should dynamically create an integer field with that name...

<dynamicField name="*_i"  type="integer"  indexed="true"  stored="true"/>

The Unique Key Field

The <uniqueKey> declaration can be used to inform Solr that there is a field in your index which should be unique for all documents. If a document is added that contains the same value for this field as an existing document, the old document will be deleted. It is not mandatory for a schema to have a uniqueKey field.

 

schema.xml文档注释中的信息:

1、为了改进性能,可以采取以下几种措施:

  • 将所有只用于搜索的,而不需要作为结果的field(特别是一些比较大的field)的stored设置为false
  • 将不需要被用于搜索的,而只是作为结果返回的field的indexed设置为false
  • 删除所有不必要的copyField声明
  • 为了索引字段的最小化和搜索的效率,将所有的 text fields的index都设置成field,然后使用copyField将他们都复制到一个总的 text field上,然后对他进行搜索。
  • 为了最大化搜索效率,使用java编写的客户端与solr交互(使用流通信)
  • 在服务器端运行JVM(省去网络通信),使用尽可能高的Log输出等级,减少日志量。

2、< schema name =" example " version =" 1.2 " >

  • name:标识这个schema的名字
  • version:现在版本是1.2

3、filedType

< fieldType name =" string " class =" solr.StrField " sortMissingLast =" true " omitNorms =" true " />

  • name:标识名称,定义fields节点时field的type属性指定该值
  • class和其他属性决定了这个fieldType的实际行为。(class以solr开始的,都是在org.appache.solr.analysis包下)

可选的属性:

  • sortMissingLast和sortMissingFirst两个属性是用在可以内在使用String排序的类型上(包括:string,boolean,sint,slong,sfloat,sdouble,pdate)。
  • sortMissingLast="true",没有该field的数据排在有该field的数据之后,而不管请求时的排序规则。
  • sortMissingFirst="true",跟上面倒过来呗。
  • 2个值默认是设置成false

StrField类型不被分析,而是被逐字地索引/存储。

StrField和TextField都有一个可选的属性“compressThreshold”,保证压缩到不小于一个大小(单位:char)

 

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

solr.TextField 允许用户通过分析器来定制索引和查询,分析器包括 一个分词器(tokenizer)和多个过滤器(filter)

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

 

< tokenizer class =" solr.WhitespaceTokenizerFactory " />

空格分词,精确匹配。

 

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

在分词和匹配时,考虑 "-"连字符,字母数字的界限,非字母数字字符,这样 "wifi"或"wi fi"都能匹配"Wi-Fi"。

 

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

同义词

 

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

在禁用字(stopword)删除后,在短语间增加间隔

stopword:即在建立索引过程中(建立索引和搜索)被忽略的词,比如is this等常用词。在conf/stopwords.txt维护。

 

4、fields

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

  • name:标识名称。
  • type:先前定义的类型。
  • indexed:是否被用来建立索引(关系到搜索和排序)
  • stored:是否储存
  • compressed:[false],是否使用gzip压缩(只有TextField和StrField可以压缩)
  • mutiValued:是否包含多个值
  • omitNorms:是否忽略掉Norm,可以节省内存空间,只有全文本field和need an index-time boost的field需要norm。(具体没看懂,注释里有矛盾)
  • termVectors:[false],当设置true,会存储 term vector。当使用MoreLikeThis,用来作为相似词的field应该存储起来。
  • termPositions:存储 term vector中的地址信息,会消耗存储开销。
  • termOffsets:存储 term vector 的偏移量,会消耗存储开销。
  • default:如果没有属性需要修改,就可以用这个标识下。

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

包罗万象(有点夸张)的field,包含所有可搜索的text fields,通过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 " />

在添加索引时,将所有被拷贝field(如cat)中的数据拷贝到text field中

作用:

  • 将多个field的数据放在一起同时搜索,提供速度
  • 将一个field的数据拷贝到另一个,可以用2种不同的方式来建立索引。

< dynamicField name =" *_i " type =" int " indexed =" true " stored =" true " />

如果一个field的名字没有匹配到,那么就会用动态field试图匹配定义的各种模式。

  • "*"只能出现在模式的最前和最后
  • 较长的模式会被先去做匹配
  • 如果2个模式同时匹配上,最先定义的优先

< dynamicField name =" * " type =" ignored " multiValued=" true " />

如果通过上面的匹配都没找到,可以定义这个,然后定义个type,当String处理。(一般不会发生)但若不定义,找不到匹配会报错。

 

5、其他一些标签

 < uniqueKey > id </ uniqueKey >

文档的唯一标识, 必须填写这个field(除非该field被标记required="false"),否则solr建立索引报错。(跟wiki里说的It is not mandatory for a schema to have a uniqueKey field. 相反)

< defaultSearchField > text </ defaultSearchField >

如果搜索参数中没有指定具体的field,那么这是默认的域。

< solrQueryParser defaultOperator =" OR " />

配置搜索参数短语间的逻辑,可以是"AND|OR"。

 

分享到:
评论

相关推荐

    solr所需资源下载(数据导入、分词器、数据库连接jar、配置文件、managed-schema)

    包括solr的所有资源文件文件清单如下: IKAnalyzer.cfg.xml ik-analyzer-solr5-5.x.jar ...managed-schema mysql-connector-java-5.1.7-bin.jar solrconfig.xml data-config.xml ext.dic stopword.dic

    基于java-solr-geo空间坐标搜索、距离排序、分页

    基于solr-geo空间搜索 1、Solr的schema.xml配置 定义坐标field 2、Solr的data-config.xml配置 建立索引 3、java查询语法 坐标距离、分页、排序

    SOLR的应用教程

    2 Solr的安装与配置 13 2.1 在Tomcat下Solr安装 13 2.1.1 安装准备 13 2.1.2 安装过程 14 2.1.3 验证安装 15 2.2 中文分词配置 15 2.2.1 mmseg4j 15 2.2.2 paoding 19 2.3 多核(MultiCore)配置 22 2.3.1 MultiCore...

    IKAnalyzer-solr4.x-update.zip

    通过继承Analyzer编写自定义类UseSmartIKAnalyzer和NotUseSmartIKAnalyzer,实现智能分词和最细粒度分词,支持solr4.7通过配置schema.xml实现不同的分词效果 &lt;fieldType name="text_ik" class="solr.TextField"&gt; ...

    tomcat集成solr服务器以及配置好了中文分词器

    只需要解压然后启动tomcat即可访问solr服务器,需要自行在apache-tomcat-solr-7.0.52\webapps\solr\solrhome\collection1\conf\schema.xml修改适合自己的配置的域即可

    solr基础知识介绍

    5.1 schema.xml 9 5.1.1 文件分析 9 5.1.2 文档注释 13 5.2 solrconfig.xml 16 6.Solr缓存 18 6.1 filterCache 18 6.2 queryResultCache 18 6.3 documentCache 19 7.solrj wiki 19 7.1 SolrJ/Solr cross-version ...

    已编译版本solr-8.11.2.tgz

    并对其进行了扩展,提供了比Lucene更为丰富的查询语言,同时实现了可配置,可扩展并对查询性能进行了优化,提供了一个完善的功能管理页面,是一款非常优秀的全文搜索引擎。 3,solr工作方式 文档通过http利用xml加...

    solar1.4环境配置

    solr-1.4.1 环境配置: 1、 下载所需软件,安装配置...在X:\solr\solr\conf当中有schema.xml文件,可以配置索引数据格式。 5、运行solr 启动Tomcat,访问http://localhost:8983 6、添加索引 7、添加中文分词

    支持solr 5.3.0的IKAnalyzer中文分词器

    IKAnalyzer中文分词器本身已经不支持最新的solr 5,集成到solr中分词会报错,这里将解决了solr 5支持问题的最新...附上IK在schema.xml中的配置: &lt;fieldType name="text_ik" class="solr.TextField"&gt; &lt;/fieldType&gt;

    solr 企业搜索引擎教程

    通过对 Solr 进行适当的配置, 某些情况下可能需要进行编码,Solr 可以阅读和使用构建到其他 Lucene 应用程序中的索引。此 外,很多 Lucene 工具(如 Nutch、 Luke)也可以使用 Solr 创建的索引 Solr 的特性包括:  高级...

    ik-analyzer-solr7.zip

    3. 配置Solr的`managed-schema`,添加`ik分词器`,示例如下; ```console &lt;!-- ik分词器 --&gt; &lt;fieldType name="text_ik" class="solr.TextField"&gt; &lt;filter class="solr.LowerCaseFilterFactory"/&gt; ...

    Solr异常.docx

    2. 盐城单机 Solr 对 Schema 识别问题 在盐城发布 REST 查询时,可能会出现 POJO 与查询结果的字段类型不一致的问题。这是因为盐城单机版本的 String+multiValued 识别是 String 集合,而集群版本则是 ArrayList。 ...

    solr全文检索

    里面有关于solr环境搭建的详细文档说明,还有schema.xml,solrconfig.xml这两个文件里的配置说明,还有创建索引,删除索引的代码。及性能的优化。

    SearchSchemer:适用于Solr,Elasticsearch和Sensei的多向模式转换器

    -outputFile --outputType其中: inputFile-要读取的配置文件inputType-输入配置类型outputFile-输出配置文件outputType-输出配置类型inputType和outputType属性可以采用以下值: solr-用于Solr schema.xml elastic...

    Solr4.3.1:配置好的Solr,分词器使用IK

    Solr4.3.1配置好的Solr,分词器使用IK。使用步骤:拷贝solr目录到web服务器,如:tomcat的webapp目录下...根据业务配置索引字段,在schema.xml文件中。修改solr_config.xml,配置索引提交的频率。启动web服务器,访问:

    Mining Solr In Action源代码

    针对Minging Solr in Action原版书的所有相关源代码示例及所有有关schema.xml, solrconfig.xml的详细配置示例,可以直接运行,配合教程再运行源代码更有助于深入理解、掌握solr全面知识点。

    solr6对应的IKAnalyzer分词器

    2. 引入分词器的jar文件:在`server/solr/RD-Product/conf/solrconfig.xml`中配置如下信息,将`rd-lib`这个文件夹下的所有jar包引入到服务中。 &lt;!-- 引入"contrib/rd-lib/"下所有jar文件 --&gt; ${solr.install.dir:../...

    Solr的基本使用

    schema.xml,在SolrCore的conf目录下,它是Solr数据表配置文件,它定义了加入索引的数据的数据类型的。主要包括FieldTypes、Fields和其他的一些缺省设置。field:进行索引,需要创建document,document中包括了很多...

    django-haystack-solr-commands:manage.py solr

    Solr 5.0.0易于使用云核心和自动托管模式进行部署,但无需复制任何schema.xml文件。 Solr 5.0.0可以使用Schema REST API编辑核心的配置。 Solr 5.0.0打破了Haystack版本2.3.1,因为默认情况下它使用托管模式,并且...

    ansj分词jar包

    solr配置ansj,managed-schema.xml中配置 &lt;fieldType name="text_ansj" class="solr.TextField" positionIncrementGap="100"&gt; ...

Global site tag (gtag.js) - Google Analytics