LoginSignup
1
2

More than 5 years have passed since last update.

Solrで新しい条件を追加してデータを取得する

Posted at

「今までの検索条件に加えて、'hoge'と検索された時に一定条件を満たしているデータが出てくるようにしたい。データには直接'hoge'とという文字列は含まれていないから普通の検索で引っ掛けることはできない。」ということがあったので、そのときに何をしたのかを記録に残しておきます。
使っているSolrのバージョンは5.5です。

data-config.xmlの改修

data-configにはどのようにデータを取得し、取得したデータのどの項目を使ってどう処理するかが書かれているはずです。なのでここに書かれているSQLを改修します。

改修前のdata-config.xml

data-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<dataConfig>
    <dataSource ~~ />
    <document>
        <entity name="object"
            query="SELECT column_a
                   FROM table_a
                   WHERE ~~">
            <field column="COLUMN_A" name="column_a"/>
            <entity name="keyword" pk="COLUMN_A"
                query="SELECT key FROM table_b WHERE column_a = '${object.COLUMN_A}'">
                <field column="key" name="key"/>
            </entity>
        </entity>
    </document>
</dataConfig>

元々、別テーブルを参照してある条件を満たしていればkeywordとして取得できるように作っていました。なのでこのあたりを改修します。

改修後のdata-config.xml

data-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<dataConfig>
    <dataSource ~~ />
    <document>
        <entity name="object"
            query="SELECT column_a
            column_b
                   FROM table_a
                   WHERE ~~">
            <field column="COLUMN_A" name="column_a"/>
            <field column="COLUMN_B" name="column_b"/>
            <entity name="keyword" pk="COLUMN_A"
                query="SELECT key FROM table_b WHERE column_a = '${object.COLUMN_A}'
                UNION
                SELECT 'hoge' FROM table_a WHERE column_a = '${object.COLUMN_A}' AND column_b = '${object.COLUMN_B}' AND hoge_flg = 1">
                <field column="key" name="key"/>
            </entity>
        </entity>
    </document>
</dataConfig>

変更箇所は以下の通りです。

  1. 今まで使っていなかったtable_aのカラムを使うことになったので、select対象にcolumn_bを追加
  2. 今まで使っていなかったtable_aのカラムを使うことになったので、fieldにcolumn_bを追加
  3. hogeで取得できるようにunionでselect文を追加

ただ今まで使っていなかったカラムを使う場合はこれだけでは足りなくて、schema.xmlも改修する必要があるので、そちらも見ていきます。

schema.xmlの改修

改修前のschema.xml

schema.xml
…略…
<schema name="fuga" version="1.5">
…略…
   <!-- field names should consist of alphanumeric or underscore characters only and
      not start with a digit.  This is not currently strictly enforced,
      but other field names will not have first class support from all components
      and back compatibility is not guaranteed.  Names with both leading and
      trailing underscores (e.g. _version_) are reserved.
   -->

   <!-- If you remove this field, you must _also_ disable the update log in solrconfig.xml
      or Solr won't start. _version_ and update log are required for SolrCloud
   --> 
   <field name="_version_" type="long" indexed="true" stored="true" />
   <field name="column_a" type="string" indexed="true" stored="true" />
</schema>
…略…

column_bが定義されていないので追加します。

改修後のschema.xml

schema.xml
…略…
<schema name="fuga" version="1.5">
…略…
   <!-- field names should consist of alphanumeric or underscore characters only and
      not start with a digit.  This is not currently strictly enforced,
      but other field names will not have first class support from all components
      and back compatibility is not guaranteed.  Names with both leading and
      trailing underscores (e.g. _version_) are reserved.
   -->

   <!-- If you remove this field, you must _also_ disable the update log in solrconfig.xml
      or Solr won't start. _version_ and update log are required for SolrCloud
   --> 
   <field name="_version_" type="long" indexed="true" stored="true" />
   <field name="column_a" type="string" indexed="true" stored="true" />
   <field name="column_b" type="string" indexed="true" stored="true" />
</schema>
…略…

schema.xmlを変更したら反映させるために再起動をしました(確かフルインポートでもいけるはずですが)。

余談

ここに辿り着く前に2回行き詰まりました。1回目はschema.xmlを変更してない、2回目はdata-configの変更箇所1で書いたselectし忘れというものでした。

1
2
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
1
2