Solrのフィールド名に日本語を使えるか

前回の記事に引き続き、今回はフィールド名に日本語を使えるかどうかを調査しました。

フィールド名の仕様については、Solrリファレンスガイドの”Field Type Definitions and Properties”に記載があります。

The name of the fieldType. This value gets used in field definitions, in the “type” attribute. It is strongly recommended that names consist of alphanumeric or underscore characters only and not start with a digit. This is not currently strictly enforced.

  • 英数字(先頭文字として数字は使えない)
  • アンダースコア

「今は厳密には強制していません」というのが微妙なところです。“Defining Fields”にもう少し詳しい説明がありました。

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.

英数字とアンダースコア以外の文字も使えないことはないけど、全部のコンポーネントがサポートしているとは限らないよ、ということのようです。

フィールド名に日本語を使うとどういうことが起こるか試してみました。

まずtestという名のコレクションを作成します。

$ curl -s 'http://localhost:8983/solr/admin/collections?action=CREATE&name=test&numShards=1&replicationFactor=1'

「日付」というフィールドを追加します。

$ cat add_field_j.json 
{
  "add-field":{
     "name":"日付",
     "type":"pdate",
     "stored":true }
}
$ curl -s -X POST -H 'Content-type:application/json' -d @add_field_j.json http://localhost:8983/solr/test/schema

APIで「日付」フィールドが存在することを確認できます。

$ curl -s 'http://localhost:8983/solr/test/schema/fields/%E6%97%A5%E4%BB%98'
{
  "responseHeader":{
    "status":0,
    "QTime":0},
  "field":{
    "name":"日付",
    "type":"pdate",
    "stored":true}}

「日付」フィールドに値を持つデータを投入してみます。

$ cat j.json
{"日付":"2019-01-01"}
$ ./post -c test j.json

比較のため、”date”フィールドを追加してデータを投入します。

$ cat add_field_e.json
{
  "add-field":{
     "name":"date",
     "type":"pdate",
     "stored":true }
}
$ curl -s -X POST -H 'Content-type:application/json' -d @add_field_e.json http://localhost:8983/solr/test/schema
$ cat e.json
{"date":"1999-01-01"}
$ ./post -c test e.json

データがどう保持されているか比較します。

$ curl -s 'http://localhost:8983/solr/test/select?q=*%3A*&omitHeader=true'
{
  "response":{"numFound":2,"start":0,"docs":[
      {
        "id":"578b7513-831e-4ef3-bdb9-770268f27a7e",
        "__":["2019-01-01T00:00:00Z"],
        "_version_":1651454167605051392},
      {
        "date":"1999-01-01T00:00:00Z",
        "id":"892eb3b5-a648-45a5-80c1-5b975e8ebc47",
        "_version_":1651454169922404352}]
  }}

“date”の方は特に問題ありません。「日付」フィールドは無く”__”というフィールドに値が格納されています。値が配列になっていることから、「日付」フィールドの定義とは異なるフィールドとして扱われていることが分かります。何か別のダイナミックフィールドのルールにヒットしたようです。

当然検索もできません。

$ curl -s 'http://localhost:8983/solr/test/select?q=%E6%97%A5%E4%BB%98%3A%5B*%20TO%20*%5D&omitHeader=true'
{
  "response":{"numFound":0,"start":0,"docs":[]
  }}
$ curl -s 'http://localhost:8983/solr/test/select?q=date%3A%5B*%20TO%20*%5D&omitHeader=true'
{
  "response":{"numFound":1,"start":0,"docs":[
      {
        "date":"1999-01-01T00:00:00Z",
        "id":"892eb3b5-a648-45a5-80c1-5b975e8ebc47",
        "_version_":1651454169922404352}]
  }}

というわけで、割と基本的なところで躓いてしまいました。結論としては、フィールド名に日本語を使うのも実用としては無理ということになりそうです。


コメント