schema.xml配置

2019-03-17  本文已影响0人  Vekaco

schema.xml用来定义文档的结构。一个文档有一个或多个域组成,而域又需要由不同的域类型进行定义。并且在创建索引的过程中,solr又需要对域进行分词。以下讲一一对以上内容进行说明。schema还分为三种:手动模式,自动模式和无schema模式。

域类型

域类型定义了应该如何从域中解析数据以及域是怎样被查询的。

 <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>

Solr内置预定义域类型

几个重点域类型讲解

  1. CurrencyField:
    提供对货币值的支持,solr可以在查询时使用他完成货币的兑换和货币汇率换算,该域支持以下功能:

配置案例1:通过FileExchangeRateProvider加载currencyConfig.xml配置文件
schema.xml配置

<fieldType name="currency" class="solr.CurrencyField" precisionStep="8" defaultCurrency="USD" currencyConfig="currency.xml" />

currency.xml内容

<currencyConfig version="1.0">
  <rates>
    <!-- Updated from http://www.exchangerate.com/ at 2011-09-27 -->
    <rate from="USD" to="ARS" rate="4.333871" comment="ARGENTINA Peso" />
    <rate from="USD" to="AUD" rate="1.025768" comment="AUSTRALIA Dollar" />
    <rate from="USD" to="EUR" rate="0.743676" comment="European Euro" />
    <rate from="USD" to="BRL" rate="1.881093" comment="BRAZIL Real" />
    <rate from="USD" to="CAD" rate="1.030815" comment="CANADA Dollar" />
    <rate from="USD" to="CLP" rate="519.0996" comment="CHILE Peso" />
    <rate from="USD" to="CNY" rate="6.387310" comment="CHINA Yuan" />
    <rate from="USD" to="CZK" rate="18.47134" comment="CZECH REP. Koruna" />
    <rate from="USD" to="DKK" rate="5.515436" comment="DENMARK Krone" />
    <rate from="USD" to="HKD" rate="7.801922" comment="HONG KONG Dollar" />
    <rate from="USD" to="HUF" rate="215.6169" comment="HUNGARY Forint" />
    <rate from="USD" to="ISK" rate="118.1280" comment="ICELAND Krona" />
...
</rates>
</currencyConfig>

配置案例2:通过OpenExchangeRatesOrgProvider调用远程接口获取汇率数据

<fieldType name="currency" class="solr.CurrencyField" precisionStep="8" providerClass="solr.OpenExchangeRatesOrgProvider" refreshInterval="60" ratesFieldLocation="http://www.openexchangerates.org/api/latest.json?appid=your-personalAppIdKey"/>
  1. TrieDateField:
    用于对日期时间类型数据进行索引,日期时间类型数据在Java里可以使用java.util.Date,Long,Integer来表示,Long、Integer类型可以表示毫秒数;使用时需要遵循以下格式约束:YYYY-MM-DDThh:mm:ssZ

solr对于日期时间存储只能精确到毫秒,任何超出毫秒经度部分的数据将会被丢弃,因此如果需要精确到毫秒,那么日期时间数据格式应该类似如下:

1972-05-20T17:33:18.772Z
1972-05-20T17:33:18.77Z
1972-05-20T17:33:18.7Z

可以通过以下代码将以上日期时间转换成Date对象:

String input = "1972-05-20T17:33:18.772Z";
TimeZone utc = TimeZone.getTimeZone("UTC");
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
f.setTimeZone(utc);
GregorianCalendar cal = new GregorianCalendar(utc);
cal.setTime(f.parse(input));
Date date = cal.getTime();

一个TrieDateField域类型完整配置如下:

<field name="Date" class="solr.TrieDateField" datetimeformat="yyyy=MM-dd'T' HH:mm:ss.sss'Z'" indexed="true" multivalued="false" stored="true" precisionStep="6" type="date">
</field>
  1. DateRangeField:
    该域类型与TrieDateField类似,唯一的区别在于,该类型会以String类型暴露域值而非Date类型。
    使用该域类型时,日期范围字符串必须类似如下:[2000 TO 2014-05-21T17:33:18.772Z],并支持日期范围的多值域索引。
    案例:查询出某个时间段内正开放经营的餐厅。需要注意的是,每个餐厅在一天之内可以有多个开放时间,而且每天的开放时间不固定。
<field name="openingHours" type="date_range" indexed="true" stored="true" multiValued="true"/>
<fieldType name="date_range" class="solr.DateRangeField"/>

date_range域的数据:

[
"[2016-02-01T03:00Z TO 2016-02-01T15:00Z]",
"[2016-02-02T03:00Z TO 2016-02-02T15:00Z]",
"[2016-02-03T03:00Z TO 2016-02-03T15:00Z]",
"[2016-02-04T03:00Z TO 2016-02-04T15:00Z]",
"[2016-02-05T03:00Z TO 2016-02-05T16:00Z]",
"[2016-02-06T03:00Z TO 2016-02-06T16:00Z]"
]

可以通过如下查询:facet.range=openingHours&f.openingHours.facet.range.start=NOW&f.openingHours.facet.range.end=NOW%2B6HOUR&f.openingHours.facet.range.gap=%2B1HOUR

4.EnumField:
该域类型用于对枚举类型的数据进行索引。

public enum LogLevel {
INFO,
DEBUG,
WARING,
ERROR
}

EnumField配置

<fieldType name="logLevelType" class="solr.EnumField" enumsConfig="enums-Config.xml" enumName="logLevel"
<?xml version="1.0"?>
<enumsConfig>
 <enum name="logLevel">
<value>INFO</value>
<value>DEBUG</value>
<value>WARNING</value>
<value>ERROR</value>
</enum>
<enum name="priority">
<value>HIGH</value>
...
</enum>
...
</enumsConfig>
  1. ExternalFileField:
    该域类型支持为某个域指定域值,而该域值是从外部文件加载得到的。也就是说,某个域的与之不需要存储到Solr中,可以从外部文件获取。ExternalFileField是不支持查询的,它只能用于function query或者显示。
    案例:假设你的Document有一个views域,而这个views域可能表示的是一篇博客的浏览量,由于浏览量可能每分每秒都在改变,你可能希望每个1分钟或者半小时单独更新下views表示浏览量的域,而文档其他域的值可能更新并不是那么频繁。
<fieldType name="viewsFile" keyField="id" defVal="0" stored="false" indexed="false" class="solr.ExternalFileField" valType="pfloat"/>
1=1000
2=20000
3=999

左边表示keyField参数表示的域的域值,右边表示需要存储到外部文件而且经常需要更新的域的域值。至此,我们只需要在field定义里通过type=“viewsFile”来应用ExternalFileField,之后更新veiews域的域值就不需要更新每一份Document而只需周期性更新外部文件的键值和对应的数值即可。

  1. UUIDField:
    使用场景:

UUID配置:

<!--schema.xml-->
<fieldType name="uuid" class="solr.UUIDField" indexed="true" />
<field name="id" type="uuid" indexed="true" stored="true" multiValued="false" required="true"/>

经过上面步骤,我们的UUIDField域已经配置好了,但solr还提供了一个自动更新UUIDField的处理器,也就是使用它UUIDUpdateProcessorFactory,我们就不需要关心如何创建和更新UUIDField域了,全权交给UUIDUpdateProcessorFactory处理就行了。

<!--solrconfig.xml-->
<updateRequestProcessorChain name="dispup">
<processor class="solr.UUIDUpdateProcessorFactory">
<str name="fieldName">id</str>
</processor>
<processor class="solr.LogUpdateProcessorFactory"/>
<processor class="solr.DistributedUpdateProcessorFactory"/>
<processor class="solr.RunUpdateProcessorFactory"/>
</updateRequestProcessorChain>
<requestHandler name="/update" class="solr.UpdateRequestHandler">
<lst name="defaults">
<str name="update.chain">dispup</str>
</lst>
</requestHandler>
上一篇 下一篇

猜你喜欢

热点阅读