engins 发表于 2015-11-12 10:27:46

Solr4.0 如何配置使用UUID自动生成id值


原文链接http://blog.iyunv.com/keepthinking_/article/details/8501058#comments

最近学习了Lucene,随便也学习了Solr,Solr规定每一条记录必须有一个主键值,用来唯一标识一条索引的记录,默认是使用id字段来作主键的(可以通过修改schema.xml文件更改),最烦的是这个主键不能设置自动增长,所以每添加一条记录,不得不手动为id字段赋值,如果不小心重复了,还很恶心的直接覆盖了原来的记录,所以在编程的时候不得不通过一些途径来维护这个id值,通过google发现了一个可以自动生成id值的方法,即让solr自动生成UUID值(Universal
Unique Identifiers通用唯一标识符),这样编程的时候就不用维护这个id值了,使用这种做法的缺点就是:id值不是数值连续的,它是一串字符,如:5bb977a7-8a4c-46d6-ae49-b4eefade080c

具体配置如下:(这是Solr 4.0的配置)

一、配置schema.xml文件

1、添加fieldType



<types>
<!-- other field types -->
<fieldType name=&quot;uuid&quot; class=&quot;solr.UUIDField&quot; indexed=&quot;true&quot; />
</types>

2、添加主键id字段配置(注释或者删除原来的id字段配置,切记)



<field name=&quot;id&quot; type=&quot;uuid&quot; indexed=&quot;true&quot; stored=&quot;true&quot; required=&quot;true&quot; multiValued=&quot;false&quot; />





二、配置solrconfig.xml文件



1、注释掉以下的配置,原因及可能产出的异常参考:https://issues.apache.org/jira/browse/SOLR-3398



<searchComponent name=&quot;elevator&quot; class=&quot;solr.QueryElevationComponent&quot; >
<str name=&quot;queryFieldType&quot;>string</str>
<str name=&quot;config-file&quot;>elevate.xml</str>
</searchComponent>
2、添加一个updateRequestProcessorChain配置





<updateRequestProcessorChain name=&quot;uuid&quot;>
<processor class=&quot;solr.UUIDUpdateProcessorFactory&quot;>
<str name=&quot;fieldName&quot;>id</str>
</processor>
<processor class=&quot;solr.RunUpdateProcessorFactory&quot; />
</updateRequestProcessorChain>
3、修改其中一个requestHandler配置,注意:上一步是添加,而这里是修改,如果直接添加的话,那么就会重复配置,这样后面的配置会覆盖前面的配置,本人就是很不幸的被默认的配置覆盖了我添加的配置,当时够郁闷的!





<requestHandler name=&quot;/update&quot; class=&quot;solr.UpdateRequestHandler&quot;>
<!-- See below for information on defining
updateRequestProcessorChains that can be used by name
on each Update Request
-->
<!--
<lst name=&quot;defaults&quot;>
<str name=&quot;update.chain&quot;>dedupe</str>
</lst>
-->
<lst name=&quot;defaults&quot;>
<str name=&quot;update.chain&quot;>uuid</str>
</lst>
</requestHandler>
页: [1]
查看完整版本: Solr4.0 如何配置使用UUID自动生成id值