映射文件

应用程序模式功能类型是使用一个映射文件配置的,该文件定义了该功能的数据源以及从源数据到输出XML中XPath的映射。

大纲

以下是映射文件的大纲:

<?xml version="1.0" encoding="UTF-8"?>
<as:AppSchemaDataAccess xmlns:as="http://www.geotools.org/app-schema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.geotools.org/app-schema AppSchemaDataAccess.xsd">
    <namespaces>...</namespaces>
    <includedTypes>...</includedTypes>
    <sourceDataStores>...</sourceDataStores>
    <catalog>...</catalog>
    <targetTypes...</targetTypes>
    <typeMappings>...</typeMappings>
</as:AppSchemaDataAccess>
  • namespaces 定义映射文件中使用的所有命名空间前缀。

  • includedTypes (可选)定义映射文件中引用的所有包含的非要素类型映射文件位置。

  • sourceDataStores 提供源数据存储区的配置信息。

  • catalog 是用于解析XML架构位置的OASIS目录的位置。

  • targetTypes 是定义功能类型的XML架构的位置。

  • typeMappings 给出源数据存储区字段与输出复杂特性元素之间的关系。

映射文件架构

  • AppSchemaDataAccess.xsd 是可选的,因为地理服务器不使用它。存在 AppSchemaDataAccess.xsd 在与映射文件相同的文件夹中,XML编辑器可以观察其语法并提供上下文帮助。

设置

命名空间

这个 namespaces 节定义映射文件中使用的所有XML命名空间::

<Namespace>
    <prefix>gsml</prefix>
    <uri>urn:cgi:xmlns:CGI:GeoSciML:2.0</uri>
</Namespace>
<Namespace>
    <prefix>gml</prefix>
    <uri>http://www.opengis.net/gml</uri>
</Namespace>
<Namespace>
    <prefix>xlink</prefix>
    <uri>http://www.w3.org/1999/xlink</uri>
</Namespace>

包含类型(可选)

非功能类型(如gsml:compositionpart是一种嵌套在gsml:geologicUnit中的数据类型)可以单独映射以实现其可重用性,但我们不希望将其配置为功能类型,因为我们不希望单独访问它。相关的特性类型不需要显式包含在这里,因为它将有自己的工作区配置,以便geoserver找到它。中的位置路径 Include 标记是相对于映射文件的。例如,如果gsml:compositionpart配置文件与gsml:geologicUnit配置位于同一目录中:

<includedTypes>
    <Include>gsml_CompositionPart.xml</Include>
</includedTypes>

源数据存储

每个映射文件都需要至少一个数据存储来为功能提供数据。应用程序模式重用geoserver数据存储,因此有许多可用的类型。参见 数据存储 有关数据存储配置的详细信息。例如::

<sourceDataStores>
    <DataStore>
        <id>datastore</id>
        <parameters>
            ...
        </parameters>
    </DataStore>
    ...
</sourceDataStores>

如果你有不止一个 DataStore 在映射文件中,确保为每个映射文件 id .

目录(可选)

OASISXML目录配置文件的位置,作为相对于映射文件的路径给出。参见 应用程序架构解析 更多信息。例如::

<catalog>../../../schemas/catalog.xml</catalog>

目标类型

这个 targetTypes 部分列出了定义映射所需的所有应用程序模式。通常只需要一个。例如::

<targetTypes>
    <FeatureType>
        <schemaUri>http://www.geosciml.org/geosciml/2.0/xsd/geosciml.xsd</schemaUri>
    </FeatureType>
</targetTypes>

映射

类型映射和特征类型映射

这个 typeMappings 节是应用程序架构模块的核心。它定义了从简单特征到一个或多个简单特征的嵌套结构的映射。它包含一个列表,其中包括 FeatureTypeMapping 元素,每个元素定义一种输出要素类型。例如::

<typeMappings>
    <FeatureTypeMapping>
        <mappingName>mappedfeature1</mappingName>
        <sourceDataStore>datastore</sourceDataStore>
        <sourceType>mappedfeature</sourceType>
        <targetElement>gsml:MappedFeature</targetElement>
        <isDenormalised>true</isDenormalised>
        <defaultGeometry>gsml:MappedFeature/gsml:shape/gml:Polygon</defaultGeometry>
        <attributeMappings>
            <AttributeMapping>
                ...
  • mappingName 是可选标记,用于标识 特征链 当同一类型有多个FeatureTypeMapping实例时。这仅用于功能链接目的,不适用于识别顶级功能。

  • sourceDataStore 必须是定义源数据存储时提供的标识符 sourceDataStores 部分。

  • sourceType 是简单功能类型名称。例如:

    • 表或视图名称,Postgis为小写,Oracle为大写。

    • 属性文件名(没有.properties后缀)

  • targetElement 是目标应用程序架构中的元素名称。这与WFS要素类型名称相同。

  • isDenormalised 是可选标记(默认为true),用于指示此类型是否包含非规范化数据。如果数据没有非规范化,那么应用程序模式将构建一个更有效的查询来应用全局功能限制。与低全局功能限制(通过 Services --> WFS )将此选项设置为false可以防止进行不必要的处理和数据库查找。

  • defaultGeometry 可用于显式定义应用作默认几何图形的特征类型的属性,这在WMS中比WFS更相关。默认的几何XML路径可以引用功能类型的任何属性,这与在OGC过滤器中引用所需属性所使用的路径完全相同。路径可以引用一个嵌套属性,该属性属于与根特征类型具有零或一个关系的链接特征。

属性映射和属性映射

attributeMappings 包括一个列表 AttributeMapping 元素::

<AttributeMapping>
    <targetAttribute>...</targetAttribute>
    <idExpression>...</idExpression>
    <sourceExpression>...</sourceExpression>
    <targetAttributeNode>...</targetAttributeNode>
    <isMultiple>...</isMultiple>
    <ClientProperty>...</ClientProperty>
</AttributeMapping>

目标属性

targetAttribute 在目标元素的上下文中,是输出元素的xpath。例如,如果包含的映射是针对某个功能的,则应该能够映射 gml:name 属性,通过设置目标属性:

<targetAttribute>gml:name</targetAttribute>

多值属性产生于 非规范化源 自动编码。如果希望将来自不同输入列的多值属性编码为属性的特定实例,则可以使用(基于一个)索引。例如,可以设置第三个 gml:name 用:

<targetAttribute>gml:name[3]</targetAttribute>

保留名称 FEATURE_LINK 用于映射未用XML编码但在中使用所需的数据 特征链 .

iExpression(可选)

用于设置自定义 gml:id 输出功能类型的。这应该是数据库列本身的名称。使用函数将导致异常,因为默认联接实现不支持它。

备注

每个功能都必须具有 gml:id .此要求是实施限制(严格来说, gml:id 在GML中是可选的)。

  • 如果未指定IDexpression, gml:id<the table name>.<primary key> ,例如 MAPPEDFEATURE.1 .

  • 在没有主键的情况下,这将是 <the table name>.<generated gml id> ,例如 MAPPEDFEATURE.fid--46fd41b8_1407138b56f_-7fe0 .

  • 如果使用属性文件而不是数据库表,则默认 gml:id 将是在属性文件中等号(“=”)之前找到的行键,例如,行“mf1=泥岩点(12)…”的特征将具有gml:id mf1 .

备注

gml:id 必须是 NCName .

sourceExpression(可选)

使用A sourceExpression 标记以设置源数据中的元素内容。例如,要从名为 DESCRIPTION ::

<sourceExpression><OCQL>DESCRIPTION</OCQL></sourceExpression>

如果 sourceExpression 不存在,生成的元素为空(除非由另一个映射设置)。

可以使用cql表达式计算元素的内容。此示例将两列中的字符串和一个文本连接起来:

<sourceExpression>
    <OCQL>strConCat(FIRST , strConCat(' followed by ', SECOND))</OCQL>
</sourceExpression>

您也可以使用 CQL功能 用于词汇翻译。

警告

请避免对用户要查询的属性使用cql表达式,因为当前实现无法反转这些表达式以生成有效的SQL,而是读取所有功能以计算属性以查找与筛选查询匹配的功能。返回到蛮力搜索会使对cql计算表达式的查询非常缓慢。如果必须连接字符串以生成内容,您可能会发现在数据库中这样做要快得多。

LinkElement和LinkField(可选)

存在 linkElementlinkField 改变的意义 sourceExpression 到A 特征链 映射,其中映射的源是类型的特征 linkElement 具有属性 linkField 匹配表达式。例如,以下内容 sourceExpression 作为映射的结果使用(可能是多值) gsml:MappedFeature 为此 gml:name[2] 等于 URN 对于源功能。这实际上是一个外键关系:

<sourceExpression>
    <OCQL>URN</OCQL>
    <linkElement>gsml:MappedFeature</linkElement>
    <linkField>gml:name[2]</linkField>
</sourceExpression>

功能类型 gsml:MappedFeature 可能在另一个映射文件中定义。这个 linkField 可以是 FEATURE_LINK 如果您希望通过XML中未公开的属性来关联这些特性。参见 特征链 进行全面的讨论。

对于特殊情况, linkElement 可以是OCQL函数,并且 linkField 可以省略。参见 多态性 更多信息。

TargetAttributeNode(可选)

targetAttributeNode 如果属性类型包含抽象元素,并且应用程序架构无法确定封闭属性的类型,则需要。

在这个例子中, om:result 属于 xs:anyType ,这是抽象的。我们可以用 targetAttributeNode 要将属性类型的类型设置为包含非抽象元素的类型,请执行以下操作:

<AttributeMapping>
      <targetAttribute>om:result</targetAttribute>
      <targetAttributeNode>gml:MeasureType</targetAttributeNode>
      <sourceExpression>
          <OCQL>TOPAGE</OCQL>
      </sourceExpression>
      <ClientProperty>
          <name>xsi:type</name>
          <value>'gml:MeasureType'</value>
      </ClientProperty>
      <ClientProperty>
          <name>uom</name>
          <value>'http://www.opengis.net/def/uom/UCUM/0/Ma'</value>
      </ClientProperty>
</AttributeMapping>

如果转换类型很复杂,则特定类型由targettribute中的xpath隐式确定,不需要targettributenode。例如,在本例中 om:result 自动专业化为mappedFeatureType::

<AttributeMapping>
      <targetAttribute>om:result/gsml:MappedFeature/gml:name</targetAttribute>
      <sourceExpression>
          <OCQL>NAME</OCQL>
      </sourceExpression>
</AttributeMapping>

虽然不是必需的,但是我们仍然可以为根节点指定targettributenode,并按照正常方式映射子属性。此映射必须在封闭元素的映射之前进行。通过这样做,如果为违反TargetAttributeNode中类型的任何子属性指定了映射,则应用程序架构将报告异常。例如。::

<AttributeMapping>
      <targetAttribute>om:result</targetAttribute>
      <targetAttributeNode>gsml:MappedFeatureType<targetAttributeNode>
</AttributeMapping>
<AttributeMapping>
      <targetAttribute>om:result/gsml:MappedFeature/gml:name</targetAttribute>
      <sourceExpression>
          <OCQL>NAME</OCQL>
      </sourceExpression>
</AttributeMapping>

请注意,GML编码规则要求复杂类型永远不是另一个复杂类型的直接属性;它们总是包含在属性类型中,以确保其类型编码在周围的元素中。编码的GML总是类型/属性/类型/属性。这也被称为GML“条带化”规则。应用程序模式映射文件的结果是 targetAttributeNode 必须应用于属性,并且类型必须设置为xsd属性类型,而不是包含属性的类型。 (gsml:CGI_TermValuePropertyTypegsml:CGI_TermValueType )。因为xpath引用的属性类型不是编码的内容, targetAttributeNode 在映射中显示 targetAttribute 与复杂类型一起使用时没有其他元素。

XML编码器将对映射到不符合GML条带化规则的复杂类型的嵌套复杂功能进行编码。Java配置属性 encoder.relaxed 可以设置为 false 禁用此行为。

EncodeifEmpty(可选)

这个 encodeIfEmpty 元素将确定如果属性包含空值或空值,该属性是否将被编码。默认 encodeIfEmpty 设置为false,因此将跳过不包含值的任何属性::

<encodeIfEmpty>true</encodeIfEmpty>

encodeIfEmpty 可用于显示仅包含客户端属性的属性,例如 xlink:title .

ismultiple(可选)

这个 isMultiple 元素说明此属性是否有多个值,这些值来自非规范化的行。因为默认值是 false 在这种情况下,它被省略了,通常被认为是:

<isMultiple>true</isMultiple>

例如,下表是用非规范化的 NAME 具有多个值的列:

ID

NAME

DESCRIPTION

gu.25678

火山群1

橄榄玄武岩、凝灰岩、微辉长岩

gu.25678

火山群2

橄榄玄武岩、凝灰岩、微辉长岩

配置文件指定 isMultiple 对于 gml:name 映射到的属性 NAME 专栏:

<AttributeMapping>
    <targetAttribute>gml:name</targetAttribute>
    <sourceExpression>
        <OCQL>NAME</OCQL>
    </sourceExpression>
    <isMultiple>true</isMultiple>
    <ClientProperty>
        <name>codeSpace</name>
        <value>'urn:ietf:rfc:2141'</value>
    </ClientProperty>
</AttributeMapping>

输出产生多个 gml:name 按ID分组的每个功能的属性:

<gsml:GeologicUnit gml:id="gu.25678">
    <gml:description>Olivine basalt, tuff, microgabbro</gml:description>
    <gml:name codeSpace="urn:ietf:rfc:2141">Yaugher Volcanic Group 1</gml:name>
    <gml:name codeSpace="urn:ietf:rfc:2141">Yaugher Volcanic Group 2</gml:name>
 ...

IsList(可选)

这个 isList 元素说明此属性是否可能有多个值,作为列表连接。用法与 isMultiple ,但值显示在单个节点中串联,而不是每个值编码在单独的节点中。因为默认值是 false 在这种情况下,它被省略了,通常被认为是:

<isList>true</isList>

例如,下表有多个 POSITION 对于每个功能:

ID

POSITION

ID1.2

1948-05年

ID1.2

1948-06年

ID1.2

1948-07年

ID1.2

1948-08年

ID1.2

1948-09年

配置文件使用 isListtimePositionList 属性映射到 POSITION 专栏:

<AttributeMapping>
    <targetAttribute>csml:timePositionList</targetAttribute>
    <sourceExpression>
        <OCQL>POSITION</OCQL>
    </sourceExpression>
    <isList>true</isList>
</AttributeMapping>

产量:

<csml:pointSeriesDomain>
    <csml:TimeSeries gml:id="ID1.2">
        <csml:timePositionList>1949-05 1949-06 1949-07 1949-08 1949-09</csml:timePositionList>
    </csml:TimeSeries>
</csml:pointSeriesDomain>

ClientProperty(可选,多值)

映射可以有一个或多个 ClientProperty 在映射目标上设置XML属性的元素。每个 ClientProperty 有一个 name 和A value 这是一个任意的cql表达式。不 OCQL 元素在内部使用 value .

这个例子是 ClientProperty 元素设置 codeSpace 文本字符串的XML属性 urn:ietf:rfc:2141 .注意在文字字符串周围使用单引号。这可以应用于gml codetype:的任何目标属性:

<ClientProperty>
    <name>codeSpace</name>
    <value>'urn:ietf:rfc:2141'</value>
</ClientProperty>

当使用GML关联模式通过引用对属性进行编码时, xlink:href 属性,并且元素为空。这 ClientProperty 元素设置 xlink:href XML属性设置为 RELATED_FEATURE_URN 数据源中的字段(例如,Oracle数据库表中的列)。此映射可应用于任何属性类型,例如 gml:FeaturePropertyType ,或模仿GML关联模式的其他类型::

<ClientProperty>
    <name>xlink:href</name>
    <value>RELATED_FEATURE_URN</value>
</ClientProperty>

请参阅中的讨论 特征链 在特殊情况下 xlink:href 通过引用为多值属性创建。

CQL

cql函数允许在映射文件中指定数据转换和条件行为。

数据库标识符

当引用数据库表/视图名称或列名时,请使用:

  • Postgis小写

  • Oracle Spatial的大写字母

非规范化源

来自非规范化源(同一个源功能ID出现多次)的多值属性将自动编码。例如,一个视图可能有一个重复的 id 可变列 name 所以任意大量的 gml:name 可以为输出功能对属性进行编码。

警告

非规范化源必须分组,以便提供具有重复ID的功能,而不需要任何干预功能。这可以通过确保按ID对非规范化的源功能进行排序来实现。不遵守此限制将导致数据损坏。但是,在使用时不需要这种限制 加入性能支持 因为这样订货就会自动进行。

基数为1..n的属性

考虑到以下两个表,第一个表包含与气象站相关的信息:

ID

NAME

st.1

车站1

st.2

车站2

第二个表包含与气象站相关的标签:

ID

STATION_ID

TAG

CODE

tg.1

st.1

温度

X年

tg.2

st.1

X2Y型

tg.3

st.2

压力

X3Y系列

一个站点可以有多个标签,在站点和标签之间建立一对多的关系,第一个站点的GML表示应该如下所示:

(...)
<st:Station gml:id="st.1">
  <st:name>Station 1</st:name>
  <st:tag st:code="X1Y">temperature</st:tag>
  <st:tag st:code="X2Y">wind</st:tag>
</st:Station_gml32>
(...)

基于JDBC的数据存储和ApacheSolr数据存储中的自定义语法支持具有一对多关系的映射。

基于SQL的数据存储

当使用基于JDBC的数据存储具有1..n关系的属性时,可以这样映射::

(...)
<AttributeMapping>
  <targetAttribute>st:tag</targetAttribute>
  <jdbcMultipleValue>
    <sourceColumn>ID</sourceColumn>
    <targetTable>TAGS</targetTable>
    <targetColumn>STATION_ID</targetColumn>
    <targetValue>TAG</targetValue>
  </jdbcMultipleValue>
  <ClientProperty>
    <name>st:code</name>
    <value>CODE</value>
  </ClientProperty>
</AttributeMapping>
(...)

这个 targetValue 指的是 <st:tag> 元素,客户端属性用常规语法映射。后台应用程序模式将负责关联 st:code 具有正确标记的属性值。

此功能的另一个变体可用于无界匿名序列上的嵌套元素,使用“anonymousAttribute”元素定义生成匿名无界序列中的子元素和值:

(...)
<AttributeMapping>
  <targetAttribute>st:tag</targetAttribute>
  <jdbcMultipleValue>
    <sourceColumn>ID</sourceColumn>
    <targetTable>TAGS</targetTable>
    <targetColumn>STATION_ID</targetColumn>
  </jdbcMultipleValue>
  <anonymousAttribute>
    <name>st:code</name>
    <value>CODE</value>
  </anonymousAttribute>
</AttributeMapping>
(...)

在这种情况下,将使用计算出的客户端属性值生成“st:code”元素子元素:

(...)
<st:Station gml:id="st.1">
  <st:name>Station 1</st:name>
  <st:tag>
    <st:code>X1Y</st:code>
    <st:code>X2Y</st:code>
  </st:tag>
</st:Station_gml32>
(...)

具有匿名无界序列的架构,如:

<xs:complexType name="StationType">
  <xs:complexContent>
    <xs:extension base="gml:AbstractFeatureType">
      <xs:sequence>
        <xs:element name="name" type="xs:string"/>
        <xs:element name="contact" type="st:ContactPropertyType"/>
        <xs:element name="location" type="gml:GeometryPropertyType"/>
        <xs:element maxOccurs="unbounded" minOccurs="0" name="tag" type="st:TagType"/>
        <xs:element name="measurements" type="ms:MeasurementPropertyType" minOccurs="0" maxOccurs="unbounded"/>
        <xs:element name="maintainer" minOccurs="0">
          <xs:complexType>
            <xs:sequence maxOccurs="unbounded">
              <xs:element name="name" type="xs:string"/>
              <xs:element name="level" type="xs:integer" nillable="true"/>
              <xs:element name="classType" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:extension>
  </xs:complexContent>
</xs:complexType>

我们可以定义以下映射:

 <FeatureTypeMapping>
  <sourceDataStore>stations_gml32</sourceDataStore>
  <sourceType>stations_gml32</sourceType>
  <targetElement>st_gml32:Station_gml32</targetElement>
  <attributeMappings>
    <AttributeMapping>
      <targetAttribute>st_gml32:Station_gml32</targetAttribute>
      <idExpression>
        <OCQL>ID</OCQL>
      </idExpression>
    </AttributeMapping>
    <AttributeMapping>
      <targetAttribute>st_gml32:name</targetAttribute>
      <sourceExpression>
        <OCQL>NAME</OCQL>
      </sourceExpression>
    </AttributeMapping>
    <AttributeMapping>
      <targetAttribute>st_gml32:location</targetAttribute>
      <sourceExpression>
        <OCQL>LOCATION</OCQL>
      </sourceExpression>
    </AttributeMapping>
    <AttributeMapping>
      <targetAttribute>st_gml32:maintainer</targetAttribute>
      <jdbcMultipleValue>
        <sourceColumn>ID</sourceColumn>
        <targetTable>MAINTAINERS_GML32</targetTable>
        <targetColumn>OWNER</targetColumn>
      </jdbcMultipleValue>
      <anonymousAttribute>
        <name>st_gml32:name</name>
        <value>NAME</value>
      </anonymousAttribute>
      <anonymousAttribute>
        <name>st_gml32:level</name>
        <value>LEVEL</value>
      </anonymousAttribute>
      <anonymousAttribute>
        <name>st_gml32:classType</name>
        <value>CLASSTYPE</value>
      </anonymousAttribute>
    </AttributeMapping>
    <AttributeMapping>
      <targetAttribute>st_gml32:measurements</targetAttribute>
      <sourceExpression>
        <OCQL>ID</OCQL>
        <linkElement>ms_gml32:Measurement_gml32</linkElement>
        <linkField>FEATURE_LINK[1]</linkField>
      </sourceExpression>
    </AttributeMapping>
  </attributeMappings>
</FeatureTypeMapping>

所以Geoserver将生成这样的GML:

<st_gml32:Station_gml32 gml:id="st.2">
  <st_gml32:name>station2</st_gml32:name>
  <st_gml32:location>
    <gml:Point srsDimension="2" srsName="urn:ogc:def:crs:EPSG::4326">
      <gml:pos>-3 -2</gml:pos>
    </gml:Point>
  </st_gml32:location>
  <st_gml32:measurements>
    <ms_gml32:Measurement_gml32 gml:id="ms.3">
      <ms_gml32:name>pressure</ms_gml32:name>
      <ms_gml32:tag>pressure_tag</ms_gml32:tag>
    </ms_gml32:Measurement_gml32>
  </st_gml32:measurements>
  <st_gml32:maintainer>
    <st_gml32:name>mnt_c</st_gml32:name>
    <st_gml32:level>73</st_gml32:level>
    <st_gml32:classType>st_2_mnt_c</st_gml32:classType>
    <st_gml32:name>mnt_d</st_gml32:name>
    <st_gml32:level>74</st_gml32:level>
    <st_gml32:classType>st_2_mnt_d</st_gml32:classType>
    <st_gml32:name>mnt_e</st_gml32:name>
    <st_gml32:level>75</st_gml32:level>
    <st_gml32:classType>st_2_mnt_e</st_gml32:classType>
  </st_gml32:maintainer>
</st_gml32:Station_gml32>

阿帕奇·索尔

使用Apache Solr数据存储时,可以使用 multiValued 像这样映射的字段:

(...)
<AttributeMapping>
  <targetAttribute>st:tag</targetAttribute>
  <solrMultipleValue>TAG</solrMultipleValue>
  <ClientProperty>
    <name>st:code</name>
    <value>CODE</value>
  </ClientProperty>
</AttributeMapping>
(...)

外部Apache Solr索引

可以使用外部ApacheSolr索引加速基于文本的搜索。考虑以下WFS GetFeatureRequest ::

<wfs:GetFeature service="WFS" version="2.0.0" xmlns:fes="http://www.opengis.net/fes/2.0" xmlns:gml="http://www.opengis.net/gml/3.2.1" xmlns:wfs="http://www.opengis.net/wfs/2.0">
  <wfs:Query typeNames="st:Station">
    <fes:Filter>
      <fes:PropertyIsLike escapeChar="!" singleChar="." wildCard="*">
        <fes:ValueReference>st:Station/st:measurement/st:Measurement/st:description</fes:ValueReference>
        <fes:Literal>*high*</fes:Literal>
      </fes:PropertyIsLike>
    </fes:Filter>
  </wfs:Query>
</wfs:GetFeature>

此请求将返回至少有一个测量值包含文本的所有站点 high 在它的描述中。这种类型的基于文本的查询(通常)在关系数据库中执行非常昂贵。

ApacheSolr是一个著名的开源项目,旨在解决这些类型的性能问题,它允许我们索引几个字段并有效地查询这些字段。尽管Apache Solr允许我们索引几种类型的字段,例如数字或甚至纬度/经度,但在处理文本字段和基于文本的查询时,它真正闪光的地方是。

外部索引的目标是允许应用程序模式利用ApacheSolr文本搜索性能,同时利用关系数据库的关系功能。实际上,这意味着完整的数据将存储在关系数据库中,我们只需要在ApacheSolr中索引改进基于文本的查询所需的字段。

以站点用例为例,我们的数据将存储在关系数据库中,例如PostgreSQL,我们将在Apache Solr索引中索引可能的描述度量值。

我们的映射文件将如下所示:

<as:AppSchemaDataAccess xmlns:as="http://www.geotools.org/app-schema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.geotools.org/app-schema AppSchemaDataAccess.xsd">
  <namespaces>
    <Namespace>
      <prefix>gml</prefix>
      <uri>http://www.opengis.net/gml/3.2</uri>
    </Namespace>
    <Namespace>
      <prefix>st</prefix>
      <uri>http://www.stations.org/1.0</uri>
    </Namespace>
  </namespaces>
  <sourceDataStores>
    <SolrDataStore>
      <id>stations_solr</id>
      <url>http://localhost:8983/solr/stations_index</url>
      <index name="stations_index"/>
    </SolrDataStore>
    <DataStore>
      <id>stations_db</id>
      <parameters>
        <Parameter>
          <name>dbtype</name>
          <value>postgisng</value>
        </Parameter>
      </parameters>
    </DataStore>
  </sourceDataStores>
  <targetTypes>
    <FeatureType>
      <schemaUri>./stations.xsd</schemaUri>
    </FeatureType>
  </targetTypes>
  <typeMappings>
    <FeatureTypeMapping>
      <sourceDataStore>stations_db</sourceDataStore>
      <sourceType>stations</sourceType>
      <targetElement>st:Station</targetElement>
      <!-- configure the index data store for this feature type -->
      <indexDataStore>stations_solr</indexDataStore>
      <indexType>stations_index</indexType>
      <attributeMappings>
        <AttributeMapping>
          <targetAttribute>st:Station</targetAttribute>
          <idExpression>
            <OCQL>id</OCQL>
          </idExpression>
          <!-- the Solr index field that matches this feature type table primary key -->
          <indexField>id</indexField>
        </AttributeMapping>
        <AttributeMapping>
          <targetAttribute>st:name</targetAttribute>
          <sourceExpression>
            <OCQL>name</OCQL>
          </sourceExpression>
        </AttributeMapping>
        <AttributeMapping>
          <targetAttribute>st:measurement</targetAttribute>
          <sourceExpression>
            <OCQL>id</OCQL>
            <linkElement>st:Measurement</linkElement>
            <linkField>FEATURE_LINK[1]</linkField>
          </sourceExpression>
          <isMultiple>true</isMultiple>
        </AttributeMapping>
      </attributeMappings>
    </FeatureTypeMapping>
    <FeatureTypeMapping>
      <sourceDataStore>stations_db</sourceDataStore>
      <sourceType>measurements</sourceType>
      <targetElement>st:Measurement</targetElement>
      <attributeMappings>
        <AttributeMapping>
          <targetAttribute>st:Measurement</targetAttribute>
          <idExpression>
            <OCQL>id</OCQL>
          </idExpression>
        </AttributeMapping>
        <AttributeMapping>
          <targetAttribute>st:description</targetAttribute>
          <sourceExpression>
            <OCQL>description</OCQL>
          </sourceExpression>
          <!-- the Solr index field that indexes the description possible values -->
          <indexField>description_txt</indexField>
        </AttributeMapping>
        <AttributeMapping>
          <targetAttribute>FEATURE_LINK[1]</targetAttribute>
          <sourceExpression>
            <OCQL>station_id</OCQL>
          </sourceExpression>
        </AttributeMapping>
      </attributeMappings>
    </FeatureTypeMapping>
  </typeMappings>
</as:AppSchemaDataAccess>

为了能够使用外部apache solr索引,我们至少需要:

  • 声明solr数据存储和索引 :这是在根要素类型映射中完成的,例如 ST:车站 .

  • 映射与数据库主键匹配的solr索引字段 :这是根功能类型的ID映射,例如`<indexfield>id<indexfield>``。

  • 映射在apache solr中索引的每个属性 :使用 索引字段 元素,例如 <indexField>description_txt</indexField> .

值得一提的是,如果定义了一个外部solr索引,那么应用程序模式总是先查询外部solr索引,然后查询关系数据库。