Solr+lucene-gosenで日本語検索

最新版のSolr/Luceneで日本語を扱う場合には、文字列を2文字ずつ切り出すCJKAnalyzerもありますが、MeCab相当の機能を持つlucene-gosenを使うことができます。
http://code.google.com/p/lucene-gosen/

LuceneMeCabの機能を使う場合、Senが有名でしたが、残念ながら現在はメンテナンスがされていないようです。
Senの入手方法 | 関口宏司のLuceneブログ

lucene-gosenをSolrで使うためには、すでにapache-solr-3.2.0が展開されているとして:

$ svn checkout http://lucene-gosen.googlecode.com/svn/trunk/ lucene-gosen
$ cp lucene-gosen/example/*.txt apache-solr-3.2.0/example/solr/conf/
$ mkdir -p apache-solr-3.2.0/example/solr/lib
$ cd apache-solr-3.2.0/example/solr/lib
$ curl -O http://lucene-gosen.googlecode.com/files/lucene-gosen-1.1.0-ipadic.jar

そして、apache-solr-3.2.0/example/solr/conf/schema.xmlに、lucene-gosen/example/schema.xml.snippetの内容を追加します。これで、text_jaというフィールド型を指定すると日本語単語分割が行われるようになります。
Solrのデモの場合、全文検索の対象はtextという型および名前のフィールドにコピーされますが、このフィールド型をtext_jaに変更してみます。

   < !-- catchall field, containing all other searchable text fields (implemented
        via copyField further on in this schema  -->
   <field name="text" type="text_ja" indexed="true" stored="false" multiValued="true"/>

Solrサーバを再起動します。

$ cd apache-solr-3.2.0/example
$ java -Dsolr.clustering.enabled=true -jar start.jar

日本語のサンプルとして、例えば次のようなファイルをsolr_ja.xmlとして登録してみます。

<add>
<doc>
  <field name="id">SOLR2000</field>
  <field name="name">Solr(ソーラ), オープンソースの全文検索システム</field>
  <field name="manu">Apacheソフトウェア財団</field>
  <field name="cat">ソフトウェア</field>
  <field name="cat">検索</field>
  <field name="features">Luceneを使った先進的な全文検索機能</field>
  <field name="price">0</field>
  <field name="popularity">10</field>
  <field name="inStock">true</field>
  <field name="incubationdate_dt">2006-01-17T00:00:00.000Z</field>
</doc>
</add>

$ apache-solr-3.2.0/example/exampledocs/post.sh solr_ja.xml

すると、ブラウザでhttp://localhost:8983/solr/browseをアクセスすれば、先ほど登録した日本語文書が検索されることが分かります。