Solr core创建后的数据导入

时间:2022-07-28
本文章向大家介绍Solr core创建后的数据导入,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

点击上方【村雨遥】添加关注

目录

  • 1. 前言
  • 2. 配置数据库

1. 前言

在上一篇 文章 中,我们已经介绍了Solr下载及单节点启动和配置,以及如何创建core,但是如何进行数据导入却还没有介绍。这篇文章就将教你在创建core之后,应该如何进行相关配置并导入数据;

2. 配置数据库

  1. 上一篇 文章 中,在创建core时,有一个solrconfig.xml文件,如下图所示:
  1. 打开该文件,并在文件的config标签中添加下列内容,添加后如图所示:
<requestHandler name="/dataimport"  class="solr.DataImportHandler">
      <lst name="defaults">
      <str name="config">data-config.xml</str>
      </lst>
</requestHandler>
  1. 创建data-config.xml文件,如图所示;
  1. data-config.xml文件中添加如下内容;
<?xml version="1.0" encoding="UTF-8" ?>
<dataConfig>
  <!--dataSource标签,配置数据库相关信息-->
  <dataSource name = "db_weibo" type="JdbcDataSource"
    driver="com.mysql.jdbc.Driver"
    url="jdbc:mysql://127.0.0.1:3306/mwyq"
    user="root"
    password="123456"/>

  <document>
    <!--以下的dataSource指定上边的dataSource标签中的name属性,并不是必须要加的,除非你配置了多个数据源,这里我是一个数据源,所以,下边的dataSource属性是可以去掉的,另外,pk属性指定的是manage-schema文件中的uniqueKey标签中的值,即主键-->
    <entity name="weibo" dataSource="db_weibo"  PK="weibo_id"
        query="select weibo_id,weibo_content,weibo_author,weibo_emotion,weibo_time,weibo_lang from weibo"
        deltaImportQuery="select weibo_id,weibo_content,weibo_author,weibo_emotion,weibo_time,weibo_lang from weibo where weibo_id= '${dih.delta.id}'"
                deltaQuery="select weibo_id,weibo_content,weibo_author,weibo_emotion,weibo_time,weibo_lang from weibo where weibo_time > '${dataimporter.last_index_time}'">


        <!--以下的字段column属性对应数据库中字段名称,name是对应solr这边配置的名称;
        注意id,默认名称即为id,表示solr这边一条数据的主键,为需要的字段建立索引关系
        如果数据库中的主键不是id,比如是objectId,那上边的query需要为它起一个别名为id即可-->
        <field column="weibo_id" name="id"/>
        <field column="weibo_id" name="weibo_id"/>
        <field column="weibo_content" name="weibo_content"/>
        <field column="weibo_author" name="weibo_author"/>
        <field column="weibo_emotion" name="weibo_emotion"/>
        <field column="weibo_time" name="weibo_time"/>
        <field column="weibo_lang" name="weibo_lang"/>
    </entity>
  </document>
</dataConfig>

PS: solr有全局索引和增量索引,所以上述配置中有两次query操作;

  • 全局索引:对应上述配置query,即将所有要建立索引的数据均重新建立一般,当数据量很大时除开第一次导入数据之外不推荐,比较耗时;
  • 增量索引:对应上述配置deltaQuery,即将数据库中新增数据建立索引,加入solr查询中;
  • 数据库驱动包:因为配置中用到MySQL数据库,因此需要导入MySQL数据库驱动包,从网上找到驱动包后,将其放入solr-xxx/webapps/solr/WEB-INF/lib文件夹中;
  1. 配置managed-schema文件 即在笔记 1 中创建core后所出现的schema.xml文件,该文件配置内容为solr索引中引入字段的类型设置,对应上一步data-config.xml中配置的field标签。打开schema.xml文件,添加如下配置; <!-- name属性为引入字段在solr中的名称。 type表示类型,solr中会有很多类型,这个在managed-schema中你会看到很多的fieldType标签,都是solr中的类型 indexed:表示是否建立索引,true为建立,如果为false,则该字段不能作为条件查询出来; stored:表示是在solr中显示,如果这里设置为false,将会在solr中查询不到。 multiValued false:表示是否关联查询字段 --> <field name="weibo_id" type="pint" indexed="true" stored="true" required="true" multiValued="false" /> <field name="weibo_content" type="text_general" indexed="true" stored="true" required="true" multiValued="false" /> <field name="weibo_author" type="text_general" indexed="true" stored="true" required="true" multiValued="false" /> <field name="weibo_emotion" type="string" indexed="true" stored="true" required="true" multiValued="false" /> <field name="weibo_time" type="pdate" indexed="true" stored="true" required="true" multiValued="false" /> <field name="weibo_lang" type="string" indexed="true" stored="true" required="true" multiValued="false" />
  2. 进入可视化访问界面,然后导入数据;
  1. 查看导入后的数据,进入访问界面,进入Query选项,直接Execute Query就可以查看刚才导入的数据,如下图所示;
  1. 条件查询,在上一步的Queryq选项中输入要查询的条件,然后直接Execute Query就可以符合查询条件的数据,如下图所说;

PS :进行条件查询时,需要修改solrconfig.xml中的默认检索,将其修改成你想要检索的字段,如下图;