Solrのコレクション名に日本語を使えるか

先日、テーブル名やカラム名に日本語が使われているデータベースからデータをSolrにインポートする機会がありました。Solr側のコレクション名やフィールド名で日本語を使えるならインポートの手間が小さくて済むので、実際そういうことができるのか調べてみました。

コレクションAPIで日本語名のコレクションを作ってみます。

$ curl -s 'http://localhost:8983/solr/admin/configs?action=CREATE&omitHeader=true&name=test1&baseConfigSet=_default'
$ curl -s 'http://localhost:8983/solr/admin/collections?action=CREATE&name=テスト&numShards=1&replicationFactor=1&wt=json'
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<title>Error 500 Server Error</title>
</head>
<body><h2>HTTP ERROR 500</h2>
<p>Problem accessing /solr/admin/collections. Reason:
<pre>    Server Error</pre></p><h3>Caused by:</h3><pre>org.apache.solr.common.SolrException: URLDecoder: The query string contains a not-%-escaped byte > 127 at position 19
(略)

サーバエラーになってしまったのでURLエンコードしてパラメータ指定します。

$ curl -s 'http://localhost:8983/solr/admin/collections?action=CREATE&name=%E3%83%86%E3%82%B9%E3%83%88&numShards=1&replicationFactor=1'
{
  "responseHeader":{
      "status":400,
      "QTime":0},
      "error":{
      "metadata":[
        "error-class","org.apache.solr.common.SolrException",
        "root-error-class","org.apache.solr.common.SolrException"],
        "msg":"Invalid collection: [テスト]. collection names must consist entirely of periods, underscores, hyphens, and alphanumerics as well not start with a hyphen",
        "code":400}}
エラーメッセージによると、コレクション名に利用できるのは以下の文字種に限るようです。
  • 英数文字
  • ピリオド
  • アンダースコア
  • ハイフン(ハイフンは先頭文字としては使えない)

ソースコードを調べると、コレクション名のチェックをしているのは以下のクラスでした。

solr/solrj/src/java/org/apache/solr/client/solrj/util/SolrIdentifierValidator.java

チェックに使う正規表現は以下の通りです。

final static Pattern identifierPattern = Pattern.compile("^(?!\\-)[\\._A-Za-z0-9\\-]+$");

SolrIdentifierValidatorではシャード、コレクション、コア、エイリアスの名前をチェックしています。チェックに使う正規表現は共通なので、シャード、コレクション、コア、エイリアスについては同じ仕様であることが分かりました。


コメント