Jakarta Lucene

About

Resources

Download

Jakarta

About the Code
コードについて

In this section we walk through the sources behind the basic Lucene demo such as where to find it, its parts and their function. This section is intended for Java developers wishing to understand how to use Jakarta Lucene in their applications.

この章では、基本的なLuceneデモのソースを見ていきながら、デモのある場所や、 デモを構成する部品、機能について説明します。 アプリケーション内でJakarta Luceneを使用する方法を理解したいと思っているJava開発者用に書かれています。


Location of the source
ソースの場所

Relative to the directory created when you extracted Lucene or retreived it from CVS, you should see a directory called "src" which in turn contains a directory called "demo". This is the root for all of the Lucene demos. Under this directory is org/apache/lucene/demo, this is where all the Java sources live.

Luceneを解凍したときや、CVSから取り出してきたときに作られたディレクトリから相対的に見て、 "src"というディレクトリがあるはずです。その中に、"demo"というディレクトリがあります。 これが、全Luceneデモのルートディレクトリになります。 このディレクトリの下に、org/apache/lucene/demoがあり、Javaのソースコードがすべて入っています。

Within this directory you should see the IndexFiles class we executed earlier. Bring that up in vi or your alternative text editor and lets take a look at it.

このディレクトリの中に、先程実行したIndexFilesクラスもあるはずです。 viやお使いのテキストエディターで開いて、中身を覗いてみましょう。


IndexFiles
IndexFiles

As we discussed in the previous walkthrough, the IndexFiles class creates a Lucene Index. Lets take a look at how it does this.

前回のウォークスルーで説明した通り、IndexFilesクラスでLuceneのインデックスを作成します。 どのようにインデックスを作るのかを見てみましょう。

The first substantial thing the main function does is instantiate an instance of IndexWriter. It passes a string called "index" and a new instance of a class called "StandardAnalyzer". The "index" string is the name of the directory that all index information should be stored in. Because we're not passing any path information, one must assume this will be created as a subdirectory of the current directory (if does not already exist). On some platforms this may actually result in it being created in other directories (such as the user's home directory).

main関数が行う最初の重要なことは、IndexWriterをのインスタンスを初期化することです。 "index" という文字列と"StandardAnalyzer" というクラスの新しいインスタンスを渡します。 "index" という文字列は、すべてのインデックス情報が蓄積されるディレクトリの名前です。 パス情報を渡していないので、カレントディレクトリのサブディレクトリとして、 (存在していなければ) ディレクトリを作成します。 プラットフォームによっては、他のディレクトリ (例えばユーザーのホームディレクトリなど) に作られるかもしれません。

The IndexWriter is the main class responsible for creating indicies. To use it you must instantiate it with a path that it can write the index into, if this path does not exist it will create it, otherwise it will refresh the index living at that path. You must a also pass an instance of org.apache.analysis.Analyzer.

IndexWriter は、インデックスを作成する役割を担うメインクラスです。 使用するには、インデックスを書き込むパスでインスタンスかしなければいけません。 このパスが存在しなければ作成しますが、インデックスが存在する場合は新しくします。 org.apache.analysis.Analyzerのインスタンスも渡す必要があります。

The Analyzer, in this case, the Stop Analyzer is little more than a standard Java Tokenizer, converting all strings to lowercase and filtering out useless words from the index. By useless words I mean common language words such as articles (a,an,the) and other words that would be useless for searching. It should be noted that there are different rules for every language, and you should use the proper analyzer for each. Lucene currently provides Analyzers for English and German.

Analyzer (この場合はStop Analyzer ) は、標準的なJavaのTokenizerとは違い、 すべての文字列を小文字に直し、インデックスから不必要な単語を除外します。 不必要な単語というのは、冠詞 (a,an,the) のような一般的な言葉や、 検索に必要のないその他の単語をさします。 言語ごとに異なった規則があることに注して、適当なAnalyzerを使うべきです。 Luceneは現在英語とドイツ語のAnalyzerを提供しています。

Looking down further in the file, you should see the indexDocs() code. This recursive function simply crawls the directories and uses FileDocument to create Document objects. The Document is simply a data object to represent the content in the file as well as its creation time and location. These instances are added to the indexWriter. Take a look inside FileDocument. Its not particularly complicated, it just adds fields to the Document.

ファイルをさらに見ていくと、indexDocs() というコードがあります。この再帰的な関数は、 ディレクトリを走査し、FileDocumentを使ってDocumentオブジェクトを作ります。 Documentは、ファイルの中に書かれている内容や、作成時間、場所を表すデータオブジェクトです。 これらのインスタンスは、indexWriterに追加されます。 FileDocumentの中身をみてみましょう。 特に複雑ではありませんし、Documentにフィールドを追加してあるだけです。

As you can see there isn't much to creating an index. The devil is in the details. You may also wish to examine the other samples in this directory, particularly the IndexHTML class. It is a bit more complex but builds upon this example.

見てきた通り、ここではインデックスを作成するところまでは説明しません。 悪魔はもっと深いところにいるのです。 このディレクトリの中にある他のサンプル、特にIndexHTMLクラスは試してみたいことでしょう。 もっと難しいですが、この例が参考になるでしょう。


Searching Files
ファイルを検索する

The SearchFiles class is quite simple. It primarily collaborates with an IndexSearcher, StandardAnalyzer (which is used in the IndexFiles class as well) and a QueryParser. The query parser is constructed with an analyzer used to interperate your query in the same way the Index was interperated: finding the end of words and removing useless words like 'a', 'an' and 'the'. The Query object contains the results from the QueryParser which is passed to the searcher. The searcher results are returned in a collection of Documents called "Hits" which is then iterated through and displayed to the user.

SearchFilesクラスはとても単純です。 まず、IndexSearcher, StandardAnalyzer (IndexFilesクラスでも使いました), QueryParserと協力します。 クエリパーサーはアナライザと一緒に作成され、アナライザはインデックスが解析されたのと同じやり方でクエリを解析するのに使います: つまり、単語の最後を探し出し、'a', 'an' 'the'のような不必要な単語を取り除きます。 Queryオブジェクトはサーチャーに渡されたQueryParserからの結果を含んでいます。 検索結果は"Hits"というDocumentsのコレクションで返され、 コレクションは繰り返しユーザーに対して表示されます。


The Web example...
Web の例...

read on>>>

つづく>>>



Copyright © 1999-2002, Apache Software Foundation