Solr 6.0 学习(三)Schema.xml 配置

来源:互联网 发布:狼群捕食算法 编辑:程序博客网 时间:2024/05/02 09:48
派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2.   略...  
  3. <!--    
  4. 这是Solr的schema文件,应该命名为schema.xml,并且在solr home的conf目录下  
  5. (如,默认在./solr/conf/schema.xml).  
  6.   
  7.  有关如何根据需要定制化该文件,请参照:  
  8.  http://wiki.apache.org/solr/SchemaXml  性能须知: 这里包含了很多实际应用不需要的可选项。 为改善性能,你可以:  
  9.   - 尽量将所有仅用于搜索,而不用于实际返回的字段设置stored="false";  
  10.   - 尽量将所有仅用于返回,而不用于搜索的字段设置indexed="false";  
  11.   - 去掉所有不需要的copyField 语句;  
  12.   - 为了达到最佳的索引大小和搜索性能,对所有的文本字段设置indexed="false",  
  13.     使用copyField将他们拷贝到“整合字段”name="text"的字段中,使用整合字段进行搜索;  
  14.   - 使用server模式来运行JVM,同时将log级别调高, 避免输出所有请求的日志。  
  15. -->  
  16.   
  17. <schema name="example" version="1.5">  
  18.   略...  
  19.   
  20.  <fields>  
  21.    <!-- fields各个属性说明:  
  22.      name: 必须属性 - 字段名  
  23.      type: 必须属性 - <types>中定义的字段类型   
  24.      indexed: 如果字段需要被索引(用于搜索或排序),属性值设置为true  
  25.      stored: 如果字段内容需要被返回,值设置为true  
  26.      docValues: 如果这个字段应该有文档值(doc values),设置为true。文档值在门  
  27.            面搜索,分组,排序和函数查询中会非常有用。虽然不是必须的,而且会导致生成  
  28.            索引变大变慢,但这样设置会使索引加载更快,更加NRT友好,更高的内存使用效率。  
  29.            然而也有一些使用限制:目前仅支持StrField, UUIDField和所有 Trie*Fields,   
  30.            并且依赖字段类型, 可能要求字段为单值(single-valued)的,必须的或者有默认值。  
  31.      multiValued: 如果这个字段在每个文档中可能包含多个值,设置为true  
  32.      termVectors: [false] 设置为true后,会保存所给字段的相关向量(vector)  
  33.            当使用MoreLikeThis时, 用于相似度判断的字段需要设置为stored来达到最佳性能.  
  34.      termPositions: 保存和向量相关的位置信息,会增加存储开销   
  35.      termOffsets: 保存 offset 和向量相关的信息,会增加存储开销  
  36.      required: 字段必须有值,否则会抛异常  
  37.      default: 在增加文档时,可以根据需要为字段设置一个默认值,防止为空  
  38.    -->  
  39.   
  40.    <!-- 字段名由字母数字下划线组成,且不能以数字开头。两端为下划线的字段为保留字段,  
  41.       如(_version_)。  
  42.     -->  
  43.           
  44.    <field name="id" type="string" indexed="true" stored="true"   
  45.            required="true" multiValued="false" />   
  46.   
  47.    <field name="title" type="text_general" indexed="true"   
  48.            stored="true" multiValued="true"/>  
  49.    <field name="description" type="text_general" indexed="true" stored="true"/>  
  50.    <field name="author" type="text_general" indexed="true" stored="true"/>  
  51.    <field name="keywords" type="text_general" indexed="true" stored="true"/>  
  52.    <field name="category" type="text_general" indexed="true" stored="true"/>  
  53.    <field name="url" type="text_general" indexed="true" stored="true"/>  
  54.    <field name="last_modified" type="date" indexed="true" stored="true"/>  
  55.    <!-- 注意: 为了节省空间,这个字段默认不被索引, 因使用copyField被拷贝到了名为text的字段中  
  56.       。用于内容返回和高亮。搜索时使用text字段   
  57.    -->  
  58.    <field name="content" type="text_general" indexed="false"   
  59.            stored="true" multiValued="true"/>  
  60.      
  61.    <!-- 整合字段(catchall field), 包含其他可搜索的字段 (通过copyField实现) -->  
  62.    <field name="text" type="text_general" indexed="true"   
  63.            stored="false" multiValued="true"/>  
  64.   
  65.    <!-- 保留字段,不能删除,否则报错 -->  
  66.    <field name="_version_" type="long" indexed="true" stored="true"/>  
  67.      
  68.  </fields>  
  69.   
  70.   
  71.  <!-- 文档的唯一标识,可理解为主键,除非标识为required="false", 否则值不能为空-->  
  72.  <uniqueKey>id</uniqueKey>  
  73.   
  74.   <!-- 拷贝需要索引的字段到整合字段中  -->  
  75.    <copyField source="title" dest="text"/>  
  76.    <copyField source="author" dest="text"/>  
  77.    <copyField source="description" dest="text"/>  
  78.    <copyField source="keywords" dest="text"/>  
  79.    <copyField source="content" dest="text"/>  
  80.    <copyField source="url" dest="text"/>  
  81.    
  82.   <types>  
  83.     <!-- 字段类型定义 -->  
  84.     <fieldType name="string" class="solr.StrField" sortMissingLast="true" />  
  85.     <fieldType name="boolean" class="solr.BoolField" sortMissingLast="true"/>  
  86.     <fieldType name="int" class="solr.TrieIntField" precisionStep="0"   
  87.         positionIncrementGap="0"/>  
  88.     <fieldType name="float" class="solr.TrieFloatField" precisionStep="0"   
  89.         positionIncrementGap="0"/>  
  90.     <fieldType name="long" class="solr.TrieLongField" precisionStep="0"   
  91.         positionIncrementGap="0"/>  
  92.     <fieldType name="double" class="solr.TrieDoubleField" precisionStep="0"   
  93.         positionIncrementGap="0"/>  
  94.     <fieldType name="date" class="solr.TrieDateField" precisionStep="0"   
  95.         positionIncrementGap="0"/>  
  96.       略...  
  97.     <!-- Thai,泰语类型字段 -->  
  98.     <fieldType name="text_th" class="solr.TextField" positionIncrementGap="100">  
  99.       <analyzer>   
  100.         <tokenizer class="solr.StandardTokenizerFactory"/>  
  101.         <filter class="solr.LowerCaseFilterFactory"/>  
  102.         <filter class="solr.ThaiWordFilterFactory"/>  
  103.         <filter class="solr.StopFilterFactory" ignoreCase="true"   
  104.             words="lang/stopwords_th.txt" />  
  105.       </analyzer>  
  106.     </fieldType>  
  107.       
  108.     <!-- Turkish,土耳其语类型字段 -->  
  109.     <fieldType name="text_tr" class="solr.TextField" positionIncrementGap="100">  
  110.       <analyzer>   
  111.         <tokenizer class="solr.StandardTokenizerFactory"/>  
  112.         <filter class="solr.TurkishLowerCaseFilterFactory"/>  
  113.         <filter class="solr.StopFilterFactory" ignoreCase="false"   
  114.             words="lang/stopwords_tr.txt" />  
  115.         <filter class="solr.SnowballPorterFilterFactory" language="Turkish"/>  
  116.       </analyzer>  
  117.     </fieldType>  
  118.       
  119.     <!-- Chinese,需要我们自己配置,整合mmseg4j就配置在这里 -->  
  120.  </types>  
  121.     
  122.   <!-- 文档相似度判断依赖于文档相似度得分。 一个自定义的 Similarity 或 SimilarityFactory   
  123.      可以在这里指定, 但是默认的设置已经适合大多数应用。可以参考:   
  124.      http://wiki.apache.org/solr/SchemaXml#Similarity  
  125.     -->  
  126.   <!--  
  127.      <similarity class="com.example.solr.CustomSimilarityFactory">  
  128.        <str name="paramkey">param value</str>  
  129.      </similarity>  
  130.     -->  
  131.   
  132. </schema>  


以上配置参考:http://my.oschina.net/HuifengWang/blog/307471

##################################使用solr遇到一个问题 start############################

solr 在使用查询的时候,【q=city:new york】 的时候会命中包含new york的所有数据文档并返回。

但是使用中文【q=city:成都】 的时候会命中包含成和都的合集,实际上我们需要的是精确查找,查找资料发现,如果想只查找包含【成都】

这个词语的文档,我们需要这样做【q=city:"成都"】必须要添加上引号

##################################使用solr遇到一个问题 end############################

比较重要的几个配置

分词:

[html] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. <!-- IK分词 start-->  
  2.     <fieldType name="text_ik" class="solr.TextField">     
  3.         <analyzer type="index">  
  4.             <!--   
  5.                 IKTokenizerFactory:继承 TokenizerFactory    
  6.                 useSmart:是否启用 智能分词  
  7.             -->  
  8.             <tokenizer class="org.wltea.analyzer.lucene.IKTokenizerFactory" useSmart="false" />  
  9.             <!-- 
  10.                 StopFilterFactory:停止分词,会根据stopwords.txt中配置的文件停止分词 
  11.             -->  
  12.             <filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt" />  
  13.         </analyzer>  
  14.         <analyzer type="query">  
  15.             <tokenizer class="org.wltea.analyzer.lucene.IKTokenizerFactory" useSmart="true" />  
  16.             <filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt" />  
  17.         </analyzer>  
  18.     </fieldType>  
  19. <!-- IK分词 end-->  


同义词配置:

[html] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. <fieldType name="text_general" class="solr.TextField" positionIncrementGap="100">  
  2.       <analyzer type="index">  
  3.         <tokenizer class="solr.StandardTokenizerFactory"/>  
  4.         <filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt" />  
  5.         <!-- in this example, we will only use synonyms at query time  
  6.         <filter class="solr.SynonymFilterFactory" synonyms="index_synonyms.txt" ignoreCase="true" expand="false"/>  
  7.         -->  
  8.         <filter class="solr.LowerCaseFilterFactory"/>  
  9.       </analyzer>  
  10.       <analyzer type="query">  
  11.         <tokenizer class="solr.StandardTokenizerFactory"/>  
  12.         <filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt" />  
  13.         <filter class="solr.SynonymFilterFactory" synonyms="synonyms.txt" ignoreCase="true" expand="true"/>  
  14.         <filter class="solr.LowerCaseFilterFactory"/>  
  15.       </analyzer>  
  16.     </fieldType>  


评分权重配置(支持自定义):

[html] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. <similarity class="com.example.solr.CustomSimilarityFactory" />  

配置默认查询字段

以query查询为例 默认查询的是field name="text"的字段,其他select或者自己定义的查询组件也是一样的道理

[html] view plain copy
print?在CODE上查看代码片派生到我的代码片
  1. <requestHandler name="/query" class="solr.SearchHandler">  
  2.      <lst name="defaults">  
  3.        <str name="echoParams">explicit</str>  
  4.        <str name="wt">json</str>  
  5.        <str name="indent">true</str>  
  6.        <!-- 默认查询字段 -->  
  7.        <str name="df">text</str>  
  8.      </lst>  
  9.   </requestHandler>  


###########################修改schema.xml###################################

将solr发布到tomcat中,例如需要修改schema.xml,添加一个field,这个时候其实是不支持热发布的,需要重启tomcat




0 0