Lucene-standardanalyzer
Lucene-StandardAnalyzer
これは最も洗練されたアナライザーであり、名前、電子メールアドレスなどを処理できます。 各トークンを小文字にし、一般的な単語や句読点がある場合は削除します。
クラス宣言
以下は org.apache.lucene.analysis.StandardAnalyzer クラスの宣言です-
public final class StandardAnalyzer
extends StopwordAnalyzerBase
フィールド
以下は org.apache.lucene.analysis.StandardAnalyzer クラスのフィールドです-
- static int DEFAULT_MAX_TOKEN_LENGTH –これはデフォルトの最大許容トークン長です。
- static Set <?> STOP_WORDS_SET -通常は検索には役に立たない一般的な英語の単語を含む変更不可能なセット。
クラスコンストラクター
次の表は、さまざまなクラスのコンストラクタを示しています-
S.No. | Constructor & Description |
---|---|
1 |
StandardAnalyzer(Version matchVersion) デフォルトのストップワード(STOP_WORDS_SET)を使用してアナライザーを構築します。 |
2 |
StandardAnalyzer(Version matchVersion, File stopwords) 廃止予定です。 代わりにStandardAnalyzer(Version、Reader)を使用してください。 |
3 |
StandardAnalyzer(Version matchVersion, Reader stopwords) 指定されたリーダーからのストップワードを使用してアナライザーを構築します。 |
4 |
StandardAnalyzer(Version matchVersion, Set<?> stopWords) 指定されたストップワードを使用してアナライザーを構築します。 |
クラスメソッド
次の表は、さまざまなクラスメソッドを示しています-
S.No. | Method & Description |
---|---|
1 |
protected Reusable Analyzer Base. Token Stream Components create Components(String fieldName, Reader reader) このアナライザーの新しいReusableAnalyzerBase.TokenStreamComponentsインスタンスを作成します。 |
2 | *int getMaxTokenLength() * |
3 |
トークンの最大許容長を設定します。 |
継承されるメソッド
このクラスは、次のクラスからメソッドを継承します-
- org.apache.lucene.analysis.StopwordAnalyzerBase
- org.apache.lucene.analysis.ReusableAnalyzerBase
- org.apache.lucene.analysis.Analyzer
- java.lang.Object
使用法
private void displayTokenUsingStandardAnalyzer() throws IOException {
String text
= "Lucene is simple yet powerful java based search library.";
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_36);
TokenStream tokenStream
= analyzer.tokenStream(LuceneConstants.CONTENTS,
new StringReader(text));
TermAttribute term = tokenStream.addAttribute(TermAttribute.class);
while(tokenStream.incrementToken()) {
System.out.print("[" + term.term() + "] ");
}
}
応用例
BooleanQueryを使用して検索をテストするためのテストLuceneアプリケーションを作成しましょう。
Step | Description |
---|---|
1 | Create a project with a name LuceneFirstApplication under a package com.finddevguides.lucene as explained in the Lucene - First Application chapter. You can also use the project created in Lucene - First Application chapter as such for this chapter to the understand searching process. |
2 | Create LuceneConstants.java as explained in the Lucene - First Application chapter. Keep the rest of the files unchanged. |
3 | Create LuceneTester.java as mentioned below. |
4 | Clean and Build the application to make sure the business logic is working as per the requirements. |
LuceneConstants.java
このクラスは、サンプルアプリケーション全体で使用されるさまざまな定数を提供するために使用されます。
package com.finddevguides.lucene;
public class LuceneConstants {
public static final String CONTENTS = "contents";
public static final String FILE_NAME = "filename";
public static final String FILE_PATH = "filepath";
public static final int MAX_SEARCH = 10;
}
LuceneTester.java
このクラスは、Luceneライブラリの検索機能をテストするために使用されます。
package com.finddevguides.lucene;
import java.io.IOException;
import java.io.StringReader;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.StandardAnalyzer;
import org.apache.lucene.analysis.tokenattributes.TermAttribute;
import org.apache.lucene.util.Version;
public class LuceneTester {
public static void main(String[] args) {
LuceneTester tester;
tester = new LuceneTester();
try {
tester.displayTokenUsingStandardAnalyzer();
} catch (IOException e) {
e.printStackTrace();
}
}
private void displayTokenUsingStandardAnalyzer() throws IOException {
String text
= "Lucene is simple yet powerful java based search library.";
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_36);
TokenStream tokenStream = analyzer.tokenStream(
LuceneConstants.CONTENTS, new StringReader(text));
TermAttribute term = tokenStream.addAttribute(TermAttribute.class);
while(tokenStream.incrementToken()) {
System.out.print("[" + term.term() + "] ");
}
}
}
プログラムを実行する
ソースの作成が完了したら、プログラムをコンパイルして実行します。 これを行うには、 LuceneTester.Java ファイルタブをアクティブのままにして、Eclipse IDEで使用可能な実行オプションを使用するか、 Ctrl + F11 を使用して LuceneTester アプリケーションをコンパイルおよび実行します。 アプリケーションが正常に実行されると、Eclipse IDEのコンソールに次のメッセージが出力されます-
[lucene] [simple] [yet] [powerful] [java] [based] [search] [library]