Lucene-simpleanalyzer
Lucene-SimpleAnalyzer
このアナライザーは、非文字文字に基づいて文書内のテキストを分割し、小文字に変換します。
クラス宣言
以下は org.apache.lucene.analysis.SimpleAnalyzer クラスの宣言です-
public final class SimpleAnalyzer
extends ReusableAnalyzerBase
クラスコンストラクター
次の表は、さまざまなクラスのコンストラクタを示しています-
S.No. | Constructor & Description |
---|---|
1 |
SimpleAnalyzer() 廃止予定です。 代わりにSimpleAnalyzer(Version)を使用してください。 |
2 |
SimpleAnalyzer(Version matchVersion) 新しいSimpleAnalyzerを作成します。 |
クラスメソッド
次の表は、さまざまなクラスメソッドを示しています-
S.No. | Method & Description |
---|---|
1 |
protected Reusable Analyzer Base. Token Stream Components create Components (String field Name, Reader reader) このアナライザーの新しいReusableAnalyzerBase.TokenStreamComponentsインスタンスを作成します。 |
継承されるメソッド
このクラスは、次のクラスからメソッドを継承します-
- org.apache.lucene.analysis.ReusableAnalyzerBase
- org.apache.lucene.analysis.Analyzer
- java.lang.Object
使用法
private void displayTokenUsingSimpleAnalyzer() throws IOException {
String text = "Lucene is simple yet powerful java based search library.";
Analyzer analyzer = new SimpleAnalyzer(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 packagecom.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 understand searching process. |
2 | Create LuceneConstants.java as explained in the Lucene - First Applicationchapter. Keep rest of the files unchanged. |
3 | Create LuceneTester.java as mentioned below. |
4 | Clean and Build the application to make sure 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.SimpleAnalyzer;
import org.apache.lucene.analysis.TokenStream;
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.displayTokenUsingSimpleAnalyzer();
} catch (IOException e) {
e.printStackTrace();
}
}
private void displayTokenUsingSimpleAnalyzer() throws IOException {
String text =
"Lucene is simple yet powerful java based search library.";
Analyzer analyzer = new SimpleAnalyzer(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] [is] [simple] [yet] [powerful] [java] [based] [search] [library]