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中的默認檢索,將其修改成你想要檢索的字段,如下圖;



往期精選



Solr 單節點的啓動與配置
手把手搭建自己的文檔類型網站,免費!
Lombok,你的開發效率神器!


關注

長按識別二維碼

關注我


求分享

求點贊

求在看




本文分享自微信公衆號 - 村雨遙(cunyu1943)。
如有侵權,請聯繫 [email protected] 刪除。
本文參與“OSC源創計劃”,歡迎正在閱讀的你也加入,一起分享。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章