Python から Lucene ライブラリを使いたかったので Python の Java 実装である Jython を使って見ました。
Solr の ベースとなっている Lucene は Java で記述されたライブラリなので
Jython を使うと Python の文法のままで Lucene 簡単に使うことができました!!
Jython の インストール
% wget http://sourceforge.net/projects/jython/files/jython/jython_installer-2.5.1.jar # su # java -jar jython_installer-2.5.1.jar [ 以下、ほとんどデフォルトでリターンキーのみ押下 指定したのは installation type で 2 Standard と target directory を /usr/local/jython2.5.1 にしたところだけ ] Welcome to Jython ! You are about to install Jython version 2.5.1 (at any time, answer c to cancel the installation) For the installation process, the following languages are available: English, German Please select your language [E/g] >>> Do you want to read the license agreement now ? [y/N] >>> Do you accept the license agreement ? [Y/n] >>> The following installation types are available: 1. All (everything, including sources) 2. Standard (core, library modules, demos and examples, documentation) 3. Minimum (core) 9. Standalone (a single, executable .jar) Please select the installation type [ 1 /2/3/9] >>> 2 Do you want to install additional parts ? [y/N] >>> Do you want to exclude parts from the installation ? [y/N] >>> Please enter the target directory >>> /usr/local/jython2.5.1 Unable to find directory /usr/local/jython2.5.1, create it ? [Y/n] >>> Please enter the java home directory (empty for using the current java runtime) >>> Your java version to start Jython is: Sun Microsystems Inc. / 1.6.0_16 Your operating system version is: Linux / 2.6.18-164.15.1.el5 Summary: - mod: true - demo: true - doc: true - src: false - JRE: /usr/local/jdk1.6.0_16/jre Please confirm copying of files to directory /usr/local/jython2.5.1 [Y/n] >>> 10 % 20 % 30 % 40 % 50 % 60 % 70 % 80 % 90 % Generating start scripts ... 100 % Do you want to show the contents of README ? [y/N] >>> Congratulations! You successfully installed Jython 2.5.1 to directory /usr/local/jython2.5.1. # /usr/local/jython2.5.1/jython [ 初回の起動だけ cachedir の更新があるので target directory に書き込み権限のあるユーザでやる ] *sys-package-mgr*: processing new jar, '/usr/local/jython2.5.1/jython.jar' *sys-package-mgr*: processing new jar, '/usr/local/jdk1.6.0_16/jre/lib/resources.jar' *sys-package-mgr*: processing new jar, '/usr/local/jdk1.6.0_16/jre/lib/rt.jar' *sys-package-mgr*: processing new jar, '/usr/local/jdk1.6.0_16/jre/lib/jsse.jar' *sys-package-mgr*: processing new jar, '/usr/local/jdk1.6.0_16/jre/lib/jce.jar' *sys-package-mgr*: processing new jar, '/usr/local/jdk1.6.0_16/jre/lib/charsets.jar' *sys-package-mgr*: processing new jar, '/usr/local/jdk1.6.0_16/jre/lib/ext/sunpkcs11.jar' *sys-package-mgr*: processing new jar, '/usr/local/jdk1.6.0_16/jre/lib/ext/sunjce_provider.jar' *sys-package-mgr*: processing new jar, '/usr/local/jdk1.6.0_16/jre/lib/ext/dnsns.jar' *sys-package-mgr*: processing new jar, '/usr/local/jdk1.6.0_16/jre/lib/ext/localedata.jar' Jython 2.5.1 (Release_2_5_1:6813, Sep 26 2009, 13:47:54) [Java HotSpot(TM) 64-Bit Server VM (Sun Microsystems Inc.)] on java1.6.0_16 Type "help", "copyright", "credits" or "license" for more information. >>>
Jython コードの作成
Python の import 文で Lucene ライブラリを読みこむことができます。
↓の例では Solr のインデックス内の全てのドキュメントを 表出します。
#! /usr/local/jython2.5.1/jython import sys import string from org.apache.lucene.store import FSDirectory from org.apache.lucene.search import IndexSearcher from org.apache.lucene.search import MatchAllDocsQuery if __name__ == "__main__": indexPath = "/Solr のインデックスのディレクトリ/index" dir = FSDirectory.getDirectory(indexPath, False) searcher = IndexSearcher( dir ); query = MatchAllDocsQuery() hits = searcher.search(query); numHits = hits.length() for i in range(0, numHits): doc = hits.doc(i) print i, doc.get('フィールド名1'), doc.get('フィールド名2')