LoginSignup
0
0

More than 3 years have passed since last update.

SolrのData Import Handler(DIH)を使ってみた

Last updated at Posted at 2019-07-06

表題の通りでSolrでデータベースの読込みを試してみた内容です。

取込んで検索することだけしかできません。差分取込みなどは、今後の課題とします。

データの準備

PostgreSQL Sample Databaseからdvdrental.zipをダウンロードしてサーバにリストアする。
リストアの方法は省略します。(ここを参照下さい)

JDBCドライバの準備

ここから最新バージョンのpostgresql-42.2.6.jarをダウンロードする。

JDBCドライバの配置

ダウンロードしたpostgresql-42.2.6.jarは、$SOLR_HOME\example\example-DIH\solr\db\libに配置する。(derby-10.9.1.0.jarやhsqldb-2.4.0.jarが配置されているフォルダ)

db-data-configの修正

$SOLR_HOME\example\example-DIH\solr\db\conf\db-data-configをエディタで開いて

接続情報の設定

    <dataSource driver="org.postgresql.Driver" 
             url="jdbc:postgresql://DBサーバ:ポート番号/dvdrental"
             user="user"
             password="password" />

DBサーバ等は、各環境に合わせます。

取込み用のクエリーを記述

        <entity name="city" query="select * from city" >
          <field column="city_id" name="id" />
          <field column="city" name="city" />
        </entity>

差分取込みを行う場合、「deltaQuery」属性や「parentDeltaQuery」属性を使うようです。(Apache Solr Reference Guide(PDF、英語)より)

起動

$SOLR_HOME>bin\solr.cmd -e dihで起動する。

データフィールドの追加

今回サンプルデータの都市名を取込むだけなので、管理画面からフィールド名を追加しました。
<field name="city" type="text_general" uninvertible="true" multiValued="true" indexed="true" required="true" stored="true"/>

取込む

ブラウザでhttp://localhost:8983/solrに接続する。
「Core Selector」から「DB」を選択し、「Dataimport」を選択する。
「Excecute」ボタンをクリックする。(再取込みする場合、「Clean」をオンにする)
※「Debug」をオンにすると取込んだ情報が「Raw Debug-Response」から確認できる。

クエリーで確認する

「Core Selector」から「DB」を選択し、「Query」を選択する。
「q」に「city:al-」と入力し、「Execute Query」ボタンをクリックする。
検索結果は、以下の通り。

{
"responseHeader":{
"status":0,
"QTime":13,
"params":{
"q":"city:al-",
"_":"1562396399091"}},
"response":{"numFound":7,"start":0,"docs":[
{
"city":["al-Ayn"],
"id":"12",
"_version_":1638291520175996937},
{
"city":["al-Hawiya"],
"id":"13",
"_version_":1638291520175996938},
{
"city":["al-Manama"],
"id":"14",
"_version_":1638291520175996939},
{
"city":["al-Qadarif"],
"id":"15",
"_version_":1638291520175996940},
{
"city":["al-Qatif"],
"id":"16",
"_version_":1638291520175996941},
{
"city":["Jalib al-Shuyukh"],
"id":"233",
"_version_":1638291520438140936},
{
"city":["Shubra al-Khayma"],
"id":"476",
"_version_":1638291520651001873}]
}}

今回苦労した点

① JDBCドライバの配置場所がリファレンスガイドに書いてある場所とは違った
取込みを行うとJDBCドライバが見つからないというログが出力されていた。リファレンスガイドを読むも英語力が無いのでlibタグばかり気にしていた。

DataImporter Full Import failed:java.lang.RuntimeException: java.lang.RuntimeException: org.apache.solr.handler.dataimport.DataImportHandlerException: Could not load driver: org.postgresql.Driver Processing Document # 1

② 取込むデータには、IDが必要(?)
取込みクエリーに<field column="city_id" name="id" />が無いとインデックスされなかった。

SolrWriter Error creating document : SolrInputDocument(fields: [city=Acua, last_update=2019-07-05 22:39:59.299765]) => org.apache.solr.common.SolrException: Document is missing mandatory uniqueKey field: id

トピック

db-data-config.xmlを修正した後Solrを再起動しなくても、「Dataimport」内の「Configuration」の右にある「Reload」で修正内容が反映できた。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0