Solr连接mysql并实现检索

本文介绍了如何将遥感影像元数据存储在MySQL数据库后,使用Solr建立索引以提升搜索引擎性能。主要步骤包括确定MySQL与Solr的连接方式,配置索引结构,以及使用Data Import Handler (DIH)通过JDBC导入数据并创建索引。在solrconfig.xml和data-config.xml中配置请求处理器和数据库连接信息,然后在schema.xml中定义字段类型和索引。最终,用户可在Solr的web界面进行查询操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

将遥感影像的元数据存储到mysql之后,需要为数据库中的元数据建立索引,这是决定搜索引擎性能的重要因素。建立索引的步骤如下:
1)确定mysql与solr的连接方式
2)定义索引的结构,配置元数据保存的字段和索引的字段
3)solr连接mysql并导入mysql中的数据,根据第2步建立的索引结构建立索引。
配置并建立索引
DIH(Data Import Handler)是从数据库中抓取数据并创建索引的一种方式,它以JDBC的方式与数据库进行交互,以实现多源数据共享。但是DIH并不属于solr发布的核心部分的一员,本文solr中的数据是从mysql中导入的,因此要在solr核心中配置导入脚本,再导入相关的jar包来扩展这一功能。
1.在solrconfig.xml中添加requestHandler,具体代码如下所示:

 <requestHandler name="/dataimport" class="solr.DataImportHandler">
    <lst name="defaults">
      <str name="config">data-config.xml</str>
    </lst>
  </requestHandler>

2.新建data-config.xml文件配置连接数据库信息,具体配置信息如下所示:

<?xml version="1.0" encoding="UTF-8" ?>
<dataConfig>
    <dataSource type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/metadata?serverTimezone=Asia/Shanghai" user="root" password="rasdaman" batchSize="-1"/>
    <document>
        <entity name="landsat8" query="select * from landsat8">
            <field column="id" name="id" />
            <field column="platformInfo" name="platformInfo" />
            <field column="dataType" name="dataType" />
            <field column="sensorInfo" name="sensorInfo" />
            <field column="recevingStation" name="recevingaStation" />
            <field column="dayNight" name="dayNight" />
            <field column="Path" name="Path" />
            <field column="Row" name="Row" />           
            <field column="illuminationElevationAngle" name="illuminationElevationAngle" />
            <field column="illuminationAzimuthAngle" name="illuminationAzimuthAngle" />
            <field column="acqusitionTime" name="acqusitionTime" />
            <field column="beginPosition" name="beginPosition" />
            <field column="endPosition" name="endPosition" />
            <field column="cloudCoveragePercentage" name="cloudCoveragePercentage" />
            <field column="LUcloud" name="LUcloud" />
            <field column="LDcloud" name="LDcloud" />
            <field column="RUcloud" name="RUcloud" />
            <field column="RDcloud" name="RDcloud" />
            <field column="centreLon" name="centreLon" />
            <field column="centreLat" name="centreLat" />
            <field column="LUlon" name="LUlon" />
            <field column="LUlat" name="LUlat" />
            <field column="RUlon" name="RUlon" />
            <field column="RUlat" name="RUlat" />
            <field column="RDlon" name="RDlon" />
            <field column="RDlat" name="RDlat" />
            <field column="LDlon" name="LDlon" />
            <field column="LDlat" name="LDlat" />
         </entity>
    </document>
</dataConfig>

1)数据源包括dataSource Type(数据源类型)、driver(数据库驱动)、url(待导入数据的地址)、user(mysql的用户名)、password(mysql的登录密码)。
2)document中的entity(实体对象),solr的web客户端用它来选择所使用的查询语句
3)document中的query(查询语句),利用SQL语句确定需要导入的数据项,本文选择全部导入。
4)field(字段),数据库中的每一个字段都是唯一的,建立索引时,该字段与索引域对应。
3.上述两步描述的是solr访问并连接mysql中的数据的配置信息,建立索引环节是在manageschema.xml中完成的。在schema.xml中添加从mysql中导入的数据项对应的字段,与data-config.xml中的字段保持一致,并指定负责索引的字段以及负责保存的字段,而且还需要指定字段的类型,具体配置如下所示:

 <field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false" />
   <field name="platformInfo" type="text_general" indexed="true" stored="true"/>
   <field name="dataType" type="text_general" indexed="true" stored="true"/>
   <field name="sensorInfo" type="text_general" indexed="true" stored="true"/>
   <field name="recevingaStation" type="string" indexed="true" stored="true"/>
   <field name="dayNight" type="string" indexed="true" stored="true"/>
   <field name="Path" type="pint" indexed="true" stored="true"/>
   <field name="Row" type="pint" indexed="true" stored="true"/>
   <field name="illuminationElevationAngle"  type="pfloat" indexed="true" stored="true"/>
   <field name="illuminationAzimuthAngle" type="pfloat" indexed="true" stored="true" />
   <field name="acqusitionTime" type="date" indexed="true" stored="true" />  
   <field name="beginPosition" type="date" indexed="true" stored="true"/>
   <field name="endPosition" type="date" indexed="true" stored="true"/>
   <field name="cloudCoveragePercentage" type="pfloat" indexed="true" stored="true"/>
   <field name="LUcloud" type="string" indexed="true" stored="true"/>
   <field name="LDcloud" type="string" indexed="true" stored="true"/>
   <field name="RUcloud" type="string" indexed="true" stored="true"/>
   <field name="RDcloud" type="string" indexed="true" stored="true"/>
   <field name="centreLon" type="pfloat" indexed="true" stored="true"/>
   <field name="centreLat" type="pfloat" indexed="true" stored="true"/>
   <field name="LUlon" type="pfloat" indexed="true" stored="true"/>
   <field name="LUlat" type="pfloat" indexed="true" stored="true"/>
   <field name="RUlon" type="pfloat" indexed="true" stored="true"/>
   <field name="RUlat" type="pfloat" indexed="true" stored="true"/>
   <field name="RDlon" type="pfloat" indexed="true" stored="true"/>
   <field name="RDlat" type="pfloat" indexed="true" stored="true"/>
   <field name="LDlon" type="pfloat" indexed="true" stored="true"/>
   <field name="LDlat" type="pfloat" indexed="true" stored="true"/>

以上设置完成便可在solr的web界面创建索引并进行查询操作。查询语句和查询结果如图1所示。
在这里插入图片描述
图1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值