3月23日(水)のAWS Black Belt Tech WebinarでAWSが提供する検索サービスについて話をしますが、 Amazon CloudSearchで使っているSolrでも、Amazon Elasticsearch Serviceで使っているElasticsearchでも、検索エンジンのコアなところはLuceneが使われていて、 いずれにしてもLuceneの事をしっておいた方が良いと思うので(例えばElasticsearch x.xの目玉はLucene x.xの取り込みです、みたいのあるし)、チャチャっとやってみます。 ■ Luceneのダウンロード まずはLuceneのホームページから最新のバイナリを落としてきます。現在のバージョンは5.5.0です。 //embedr.flickr.com/assets/client-code.js 解凍すると↓こんな感じ
$ ls -l total 1264 -rw-r--r--@ 1 eshinoha 1896053708 589819 2 17 03:19 CHANGES.txt -rw-r--r--@ 1 eshinoha 1896053708 1781 1 25 03:09 JRE_VERSION_MIGRATION.txt -rw-r--r--@ 1 eshinoha 1896053708 24570 1 25 03:09 LICENSE.txt -rw-r--r--@ 1 eshinoha 1896053708 6166 2 7 21:04 MIGRATE.txt -rw-r--r--@ 1 eshinoha 1896053708 9488 1 25 03:09 NOTICE.txt -rw-r--r--@ 1 eshinoha 1896053708 724 1 25 03:09 README.txt -rw-r--r--@ 1 eshinoha 1896053708 957 2 7 21:04 SYSTEM_REQUIREMENTS.txt drwxr-xr-x@ 11 eshinoha 1896053708 374 3 19 14:29 analysis drwxr-xr-x@ 3 eshinoha 1896053708 102 3 19 14:29 backward-codecs drwxr-xr-x@ 5 eshinoha 1896053708 170 3 19 14:29 benchmark drwxr-xr-x@ 3 eshinoha 1896053708 102 3 19 14:29 classification drwxr-xr-x@ 3 eshinoha 1896053708 102 3 19 14:29 codecs drwxr-xr-x@ 3 eshinoha 1896053708 102 3 19 14:29 core 〜略〜 drwxr-xr-x@ 4 eshinoha 1896053708 136 3 19 14:29 sandbox drwxr-xr-x@ 4 eshinoha 1896053708 136 3 19 14:29 spatial drwxr-xr-x@ 3 eshinoha 1896053708 102 3 19 14:29 spatial3d drwxr-xr-x@ 3 eshinoha 1896053708 102 3 19 14:29 suggest drwxr-xr-x@ 4 eshinoha 1896053708 136 3 19 14:29 test-framework
とりあえずREADME.txt読んでみます。非常に小ざっぱりしております。笑
1 # Apache Lucene README file 2 3 ## Introduction 4 5 Lucene is a Java full-text search engine. Lucene is not a complete 6 application, but rather a code library and API that can easily be used 7 to add search capabilities to applications. LuceneはJavaで書かれたfull-text searach engineです。 Luceneは完全なアプリケーションではありませんが、コアライブラリとAPIは簡単にアプリケーションに 検索機能のケーパビリティをもたらします 8 9 * The Lucene web site is at: http://lucene.apache.org/ 10 * Please join the Lucene-User mailing list by sending a message to: 11 java-user-subscribe@lucene.apache.org 12 13 ## Files in a binary distribution 14 15 Files are organized by module, for example in core/: ファイルはモジュール毎に管理されていて、例えば core/ の中は、 16 17 * `core/lucene-core-XX.jar`: 18 The compiled core Lucene library. のようにcore Luceneライブラリが集められています。 19 20 To review the documentation, read the main documentation page, located at: 21 `docs/index.html` ドキュメントを読むには docs/index.html を見てください。 22 23 To build Lucene or its documentation for a source distribution, see BUILD.txt ソースコードの配布物でLuceneをビルドしたりドキュメントを見る場合はBUILD.txtを見てください。
README.txtに書いてあったようにdocsにいってindex.htmlを見てみると、Getting Startedが出てきたので、コレに沿ってやってみようと思います。 //embedr.flickr.com/assets/client-code.js ■ Getting Started ・Setting your CLASSPATH まずはクラスパスを通すところから。4つのjarファイルが必要とのこと。 1. Lucene JAR(core/lucene-core-{version}.jar) 2. queryparser JAR(queryparser/lucene-queryparser-{version}.jar) 3. common analysis JAR(analysis/common/lucene-analyzers-common-{version}.jar) 4. Lucene demo JAR(demo/lucene-demo-{version}.jar) ということで以下。カレントディレクトリはLuceneを解凍したところです。
export CLASSPATH=core/lucene-core-5.5.0.jar:queryparser/lucene-queryparser-5.5.0.jar:analysis/common/lucene-analyzers-common-5.5.0.jar:demo/lucene-demo-5.5.0.jar
・Indexing Files インデックスファイルを作って見ましょうということで。 ドキュメントでは/srcの中をインデクシングって書いてあるんだけど、ソース付きの落としてきたわけじゃないので、docsにしてみました。
$ java org.apache.lucene.demo.IndexFiles -docs ./docs/ Indexing to directory 'index'... adding ./docs/analyzers-common/allclasses-frame.html adding ./docs/analyzers-common/allclasses-noframe.html adding ./docs/analyzers-common/constant-values.html 〜略〜 adding ./docs/test-framework/resources/tab.gif adding ./docs/test-framework/resources/titlebar.gif adding ./docs/test-framework/resources/titlebar_end.gif adding ./docs/test-framework/serialized-form.html adding ./docs/test-framework/stylesheet.css 10895 total milliseconds
gifとかも対象になっちゃったけど沢山のHTMLファイルをインデクシング出来たみたいです。 ってことで、検索してみましょう。試しに"kuromoji"で。
$ java org.apache.lucene.demo.SearchFiles Enter query: kuromoji Searching for: kuromoji 27 total matching documents 1. ./docs/analyzers-kuromoji/org/apache/lucene/analysis/ja/tokenattributes/package-summary.html 2. ./docs/analyzers-kuromoji/overview-summary.html 3. ./docs/analyzers-kuromoji/org/apache/lucene/analysis/ja/util/package-summary.html 4. ./docs/analyzers-kuromoji/org/apache/lucene/analysis/ja/tokenattributes/package-use.html 5. ./docs/analyzers-kuromoji/org/apache/lucene/analysis/ja/dict/package-summary.html 6. ./docs/analyzers-kuromoji/org/apache/lucene/analysis/ja/tokenattributes/class-use/InflectionAttribute.html 7. ./docs/analyzers-kuromoji/org/apache/lucene/analysis/ja/tokenattributes/class-use/ReadingAttribute.html 8. ./docs/index.html 9. ./docs/analyzers-kuromoji/org/apache/lucene/analysis/ja/dict/class-use/BinaryDictionary.html 10. ./docs/analyzers-kuromoji/org/apache/lucene/analysis/ja/dict/class-use/CharacterDefinition.html Press (n)ext page, (q)uit or enter number to jump to a page.
nとEnterでページネーションして次いったりとかできました。 ソースは↓にあってmain文からサクっと追いかけられそうだったけど、先に進みます。 docs/demo/src-html/org/apache/lucene/demo/IndexFiles.html docs/demo/src-html/org/apache/lucene/demo/SearchFiles.html ■ Luke(Lucene用のインデックスブラウザ) 上記のままでは日本語検索もままなりませんし、何より毎回黒い画面もなんだかだよね、と。 ということで、LukeっていうGUIがあるのですが、ググってくと何やらレガシーで 最近のLuceneを既にサポートしていないっぽい予感。 ↓みたらGoogle Codeに行けって書かれてて、 http://www.getopt.org/luke/ Google Codeの↓みたらLuceneバージョン4のアルファとかまでしか対応してないっぽい https://code.google.com/archive/p/luke/ で、ググってたらGithub上で↓に辿り着いて、Commitersみたら内田さんいるじゃん、と。 https://github.com/DmitryKey/luke ApacheCon2015でやった↓このプロダクトに関するLTもあるので、5分くらいで親しみを持ってみます、と。(プルリク from the guy in San FranciscoによってElasticsearchのインデックスにも使えるようになったそうです) https://www.youtube.com/watch?list=PLGeM09tlguZTaS5FNoJGYEohaubtIvErS&v=fQAAzpk4oQ4#t=392 ってことで、ギッハブからzipで落としてきてmvn packageしてみます(luke.sh蹴ったらtargetの中にxxが無い的な事いわれたのと、mavenizedって書いてあったから、、) 相当イロイロやってるみたいだったけど、無事ビルドは成功しました。
$ mvn package [INFO] Scanning for projects... [WARNING] [WARNING] Some problems were encountered while building the effective model for luke:luke:jar:5.5.0 [WARNING] 'version' contains an expression but should be a constant. @ luke:luke:${lucene.version}, /Users/eshinoha/Downloads/luke-master/pom.xml, line 9, column 14 [WARNING] [WARNING] It is highly recommended to fix these problems because they threaten the stability of your build. [WARNING] [WARNING] For this reason, future Maven versions might no longer support building such malformed projects. [WARNING] [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building luke 5.5.0 [INFO] ------------------------------------------------------------------------ 〜略〜 INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 03:35 min [INFO] Finished at: 2016-03-19T18:53:05+09:00 [INFO] Final Memory: 39M/239M [INFO] ------------------------------------------------------------------------
で、luke.sh叩いたら、JavaのGUIが立ち上がってきました〜 # なんだかホンワカするUIですこと。 //embedr.flickr.com/assets/client-code.js で、ココから自分でインデクシングのプログラムを書こうと思ったけど、 ↓この辺にズバリ書かれているので割愛させていただいて、 Apache Lucene 使い方メモ - Qiita http://qiita.com/opengl-8080/items/8762b7f328964bd1cd92 Analyzer Toolとか使えばJapanese Analyzerでどうトークナイズされるかーとかは見れたりします。 //embedr.flickr.com/assets/client-code.js 本当はちゃんとJapanese Analyzerでトークナイズして(辞書カスタムとかも含めて、、)、 LukeでExplainを見てチューニングしていく、みたいのが出来るとベストですかね。 //embedr.flickr.com/assets/client-code.js