- Lucene 教程
- Lucene - 首頁
- Lucene - 概述
- Lucene - 環境搭建
- Lucene - 第一個應用程式
- Lucene - 索引類
- Lucene - 搜尋類
- Lucene - 索引過程
- Lucene - 索引操作
- Lucene - 搜尋操作
- Lucene - 查詢程式設計
- Lucene - 分析
- Lucene - 排序
- Lucene 有用資源
- Lucene - 快速指南
- Lucene - 有用資源
- Lucene - 討論
Lucene - 簡單分析器
此分析器根據非字母字元分割文件中的文字,然後將其轉換為小寫。
類宣告
以下是org.apache.lucene.analysis.SimpleAnalyzer類的宣告:
public final class SimpleAnalyzer extends ReusableAnalyzerBase
類建構函式
下表顯示了不同的類建構函式:
| 序號 | 建構函式及描述 |
|---|---|
| 1 | SimpleAnalyzer() 已棄用。請改用 SimpleAnalyzer(Version) 。 |
| 2 | SimpleAnalyzer(Version matchVersion) 建立一個新的 SimpleAnalyzer。 |
類方法
下表顯示了不同的類方法:
| 序號 | 方法及描述 |
|---|---|
| 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() + "] ");
}
}
示例應用程式
讓我們建立一個測試 Lucene 應用程式來測試使用 BooleanQuery 的搜尋。
| 步驟 | 描述 |
|---|---|
| 1 | 建立一個名為LuceneFirstApplication的專案,位於packagecom.tutorialspoint.lucene下,如Lucene - 第一個應用程式章節中所述。您也可以使用Lucene - 第一個應用程式章節中建立的專案,以便理解搜尋過程。 |
| 2 | 建立LuceneConstants.java,如Lucene - 第一個應用程式章節中所述。保持其餘檔案不變。 |
| 3 | 建立如下所示的LuceneTester.java。 |
| 4 | 清理並構建應用程式,以確保業務邏輯按要求工作。 |
LuceneConstants.java
此類用於提供將在示例應用程式中使用的各種常量。
package com.tutorialspoint.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.tutorialspoint.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]