Apache Solr教程

本教程演示如何将应用程序模式插件与ApacheSolr数据存储一起使用。本教程将重点介绍ApacheSolr数据存储的特定方面,以及 App-Schema documentation 应该先读。

本教程的用例将通过应用程序模式提供关于ApacheSolr核心中某些气象站索引的信息。注意,这个用例完全是虚构的,只用于演示ApacheSolr和应用程序模式的集成。

站点数据由站点的一些元信息组成,例如站点的名称和位置。当使用ApacheSolr作为数据源时,我们需要提供的唯一额外的不同配置是数据存储本身的配置。

Apache Solr数据源配置作为特定语法,允许我们指定几何属性并显式设置默认几何:

<sourceDataStores>
  <SolrDataStore>
    <id>stations</id>
    <url>http://localhost:8983/solr/stations</url>
    <index name="stations">
      <geometry default="true">
        <name>location</name>
        <srid>4326</srid>
        <type>POINT</type>
      </geometry>
    </index>
  </SolrDataStore>
</sourceDataStores>

在这种特殊情况下, location 属性包含点几何图形,并将成为默认几何图形。

完整的映射文件是:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<as:AppSchemaDataAccess xmlns:as="http://www.geotools.org/app-schema">
    <namespaces>
        <Namespace>
            <prefix>st</prefix>
            <uri>http://www.stations.org/1.0</uri>
        </Namespace>
        <Namespace>
            <prefix>gml</prefix>
            <uri>http://www.opengis.net/gml/3.2</uri>
        </Namespace>
    </namespaces>
    <sourceDataStores>
        <SolrDataStore>
            <id>stations</id>
            <url>http://localhost:8983/solr/stations</url>
            <index name="stations">
                <geometry default="true">
                    <name>location</name>
                    <srid>4326</srid>
                    <type>POINT</type>
                </geometry>
            </index>
        </SolrDataStore>
    </sourceDataStores>
    <targetTypes>
        <FeatureType>
            <schemaUri>stations.xsd</schemaUri>
        </FeatureType>
    </targetTypes>
    <typeMappings>
        <FeatureTypeMapping>
            <mappingName>stations_solr</mappingName>
            <sourceDataStore>stations</sourceDataStore>
            <sourceType>stations</sourceType>
            <targetElement>st:Station</targetElement>
            <attributeMappings>
                <AttributeMapping>
                    <targetAttribute>st:Station</targetAttribute>
                    <idExpression>
                        <OCQL>station_id</OCQL>
                    </idExpression>
                </AttributeMapping>
                <AttributeMapping>
                    <targetAttribute>st:stationName</targetAttribute>
                    <sourceExpression>
                        <OCQL>station_name</OCQL>
                    </sourceExpression>
                </AttributeMapping>
                <AttributeMapping>
                    <targetAttribute>st:position</targetAttribute>
                    <sourceExpression>
                        <OCQL>station_location</OCQL>
                    </sourceExpression>
                </AttributeMapping>
            </attributeMappings>
        </FeatureTypeMapping>
    </typeMappings>
</as:AppSchemaDataAccess>

属性的映射是直接的,并遵循常规的应用程序模式属性映射语法。当前不支持多值字段。

使用solr作为应用程序模式索引

app schema indexes是映射的扩展,它允许使用apache solr作为查询的索引,并从普通app schema datasource(sql db、mongodb,…)中检索数据。.

使用它的唯一要求是安装了geoserver-app-schema扩展和solr扩展。

索引层如何工作

当应用程序模式检测到为FeatureType激活了索引层时,它将为从geoserver OWS请求传入的每个查询使用solr配置的字段。如果传入查询仅使用索引字段,则应用程序架构将仅在solr数据源上查询以检索匹配的功能ID,并将连接到普通数据源以获取所有深度数据,但仅限于匹配的ID。

警告

请注意,两个主键(solr index core和数据源)都应该匹配以使索引层工作。

链接仅索引存储

开始像往常一样创建solrdatastore定义以及postgis存储定义:

(...)
<sourceDataStores>
(...)
    <SolrDataStore>
        <id>stations_index</id>
        <url>http://localhost:8983/solr/stations</url>
        <index name="stations">
            <geometry default="true">
                <name>location</name>
                <srid>4326</srid>
                <type>POINT</type>
            </geometry>
        </index>
    </SolrDataStore>
     <DataStore>
          <id>postgis_dataStore</id>
          <parameters>
              <Parameter>
                  <name>Connection timeout</name>
                  <value>20</value>
              </Parameter>
              <Parameter>
                  <name>port</name>
                  <value>5432</value>
              </Parameter>
              <Parameter>
                  <name>passwd</name>
                  <value>postgres</value>
              </Parameter>
              <Parameter>
                  <name>dbtype</name>
                  <value>postgis</value>
              </Parameter>
(...)

在FeatureTypeMapping设置中将Solr索引链接为索引层:

  • indexdatastore:存储中仅用作索引层的solrdatastore id属性。

  • indextype:要使用的solr核心。

<typeMappings>
(...)
    <FeatureTypeMapping>
        <mappingName>Stations</mappingName>
        <sourceDataStore>postgis_dataStore</sourceDataStore>
        <sourceType>meteo_stations</sourceType>
        <targetElement>st:Station</targetElement>
        <defaultGeometry>st:position</defaultGeometry>
        <indexDataStore>stations_index</indexDataStore>
        <indexType>stations</indexType>
        <attributeMappings>
        (...)

链接已启用索引的属性

要将Solr核心字段链接为属性映射的索引,只需添加具有此格式的索引字段定义:

<AttributeMapping>
(...)
  <indexField>${SOLR_FIELD_NAME}</indexField>
(...)
</AttributeMapping>
  • $solr_field_name:索引层中要使用的solr core的字段名。

例如,如果您需要使用solr字段:station_id和station_name;您将在mapping上写入:

<AttributeMapping>
    <targetAttribute>st:Station</targetAttribute>
    <idExpression>
        <OCQL>id</OCQL>
    </idExpression>
    <indexField>station_id</indexField>
</AttributeMapping>
<AttributeMapping>
    <targetAttribute>st:stationName</targetAttribute>
    <sourceExpression>
        <OCQL>strConcat('1_', common_name)</OCQL>
    </sourceExpression>
    <indexField>station_name</indexField>
</AttributeMapping>
Previous: MongoDB教程
Next: 造型