Solr 筆記 2-core 創建後的數據導入

Solr 筆記 2-core 創建後的數據導入

前言

在筆記 1 中,我們已經介紹了Solr下載及單節點啟動和配置,以及如何創建core,但是如何進行數據導入卻還沒有介紹。這篇文章就將教你在創建core之後,應該如何進行相關配置並導入數據;

配置資料庫

  1. 筆記 1 中,在創建core時,有一個solrconfig.xml文件,如下圖所示:
  1. 打開該文件,並在文件的config標籤中添加下列內容,添加後如圖所示:
<requestHandler name="/dataimport"  class="solr.DataImportHandler">        <lst name="defaults">        <str name="config">data-config.xml</str>        </lst>      </requestHandler>

image

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