點擊上方【村雨遙】添加關注
目錄
1. 前言
2. 配置數據庫
1. 前言
在上一篇 文章 中,我們已經介紹了Solr
下載及單節點啓動和配置,以及如何創建core
,但是如何進行數據導入卻還沒有介紹。這篇文章就將教你在創建core
之後,應該如何進行相關配置並導入數據;
2. 配置數據庫
-
上一篇 文章 中,在創建 core
時,有一個solrconfig.xml
文件,如下圖所示:
-
打開該文件,並在文件的 config
標籤中添加下列內容,添加後如圖所示:
<requestHandler name="/dataimport" class="solr.DataImportHandler">
<lst name="defaults">
<str name="config">data-config.xml</str>
</lst>
</requestHandler>
-
創建 data-config.xml
文件,如圖所示;
-
在 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
文件夾中;
-
配置
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" /> -
進入可視化訪問界面,然後導入數據;
-
查看導入後的數據,進入訪問界面,進入 Query
選項,直接Execute Query
就可以查看剛纔導入的數據,如下圖所示;
-
條件查詢,在上一步的 Query
中q
選項中輸入要查詢的條件,然後直接Execute Query
就可以符合查詢條件的數據,如下圖所說;
PS :進行條件查詢時,需要修改solrconfig.xml
中的默認檢索,將其修改成你想要檢索的字段,如下圖;
往期精選
長按識別二維碼
關注我
求分享
求點贊
求在看
本文分享自微信公衆號 - 村雨遙(cunyu1943)。
如有侵權,請聯繫 [email protected] 刪除。
本文參與“OSC源創計劃”,歡迎正在閱讀的你也加入,一起分享。