ソフトウェアエンジニア現役続行

雑多なことを綴ります

Luke (Lucene インデックスブラウザ) の lucene-4.x 対応版

Luke は Lucene のインデックスブラウザで、とても便利なツールだったのですが、lucene のバージョンが 4.x になってから使うことができなくなっていました。ところが探してみると、4.x に対応したものが GitHub 上に公開されていました (tarzanek/luke · GitHub)。これは Luke オリジナル (http://luke.googlecode.com/svn/からコードフォークさせて、Lucene 4.x に対応したものです。使い方などは全く同じです。オリジナルのほうは開発が止まっていて今も Lucene 4.x に対応しておりません。

tarzanek/luke をダウンロードしてビルドする

簡単です。

$ git clone https://github.com/tarzanek/luke.git

でコードをダウンロードして、

$ ant

でビルドが完了します。dist ディレクトリ配下に jar ファイルができているのを確認してください。

tarzanek/luke を使ってみる

$ java -classpath dist/lukeall-X.Y.Z.jar org.getopt.luke.Luke

GUI が起動するので、GUI から Lucene のインデックスディレクトリを開けば OK です。

ここでは例として以下のプログラムを実行します。

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

import java.io.File;
import java.io.StringReader;

public class IndexString {

  private IndexString() {}

  public static void main(String[] args) throws Throwable {
    String indexPath = "index";
    String sampleString = "This is a sample program to test lucene and luke.";
    Directory dir = FSDirectory.open(new File(indexPath));
    Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_47);
    IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_47, analyzer);
    iwc.setOpenMode(OpenMode.CREATE);
    IndexWriter writer = new IndexWriter(dir, iwc);
    Document doc = new Document();
    doc.add(new TextField("contents", new StringReader(sampleString)));
    writer.addDocument(doc);
    writer.close();
  }
}

lucene-core-4.7.Xlucene-analyzers-common-4.7.X のライブラリに依存しているので、Maven Repository などでライブラリを入手してください)

このプログラムを実行すると、sampleString 変数で定義された文字列がインデキシングされ、その結果が indexPath 変数で定義したディレクトリに格納されます。

そして Luke を起動してインデックスの格納されたディレクトリを選ぶと、先ほどの sampleString 変数の中身がインデキシングされていることが確認できます。

f:id:Oswald:20140429183306j:plain