- Lucene 教程
- Lucene - 首頁
- Lucene - 概述
- Lucene - 環境設定
- Lucene - 第一個應用程式
- Lucene - 索引類
- Lucene - 搜尋類
- Lucene - 索引過程
- Lucene - 索引操作
- Lucene - 搜尋操作
- Lucene - 查詢程式設計
- Lucene - 分析
- Lucene - 排序
- Lucene 有用資源
- Lucene - 快速指南
- Lucene - 有用資源
- Lucene - 討論
Lucene - StopAnalyzer
此分析器的工作方式類似於 SimpleAnalyzer,並刪除諸如“a”、“an”、“the”等常用詞。
類宣告
以下是org.apache.lucene.analysis.StopAnalyzer類的宣告:
public final class StopAnalyzer extends StopwordAnalyzerBase
欄位
以下是 org.apache.lucene.analysis.StopAnalyzer 類的欄位:
static Set<?> ENGLISH_STOP_WORDS_SET - 一個不可修改的集合,包含一些通常對搜尋無用的常用英語單詞。
類建構函式
下表顯示了不同的類建構函式:
| 序號 | 建構函式和描述 |
|---|---|
| 1 | StopAnalyzer(Version matchVersion) 構建一個移除 ENGLISH_STOP_WORDS_SET 中單詞的分析器。 |
| 2 | StopAnalyzer(Version matchVersion, File stopwordsFile) 使用給定檔案中的停用詞構建一個分析器。 |
| 3 | StopAnalyzer(Version matchVersion, Reader stopwords) 使用給定讀取器中的停用詞構建一個分析器。 |
| 4 | StopAnalyzer(Version matchVersion, Set<?> stopWords) 使用給定集合中的停用詞構建一個分析器。 |
類方法
下表顯示了不同的類方法:
| 序號 | 方法和描述 |
|---|---|
| 1 | protected Reusable Analyzer Base. Token Stream Components create Components (String field Name, Reader reader) 建立一個新的 ReusableAnalyzerBase.TokenStreamComponents,用於對提供的 Reader 中的所有文字進行標記化。 |
繼承的方法
此類繼承自以下類的方法:
- org.apache.lucene.analysis.StopwordAnalyzerBase
- org.apache.lucene.analysis.ReusableAnalyzerBase
- org.apache.lucene.analysis.Analyzer
- java.lang.Object
用法
private void displayTokenUsingStopAnalyzer() throws IOException {
String text
= "Lucene is simple yet powerful java based search library.";
Analyzer analyzer = new StopAnalyzer(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 | 在Lucene - 第一個應用程式章節中解釋的com.tutorialspoint.lucene包下建立一個名為LuceneFirstApplication的專案。您也可以使用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.StopAnalyzer;
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.displayTokenUsingStopAnalyzer();
} catch (IOException e) {
e.printStackTrace();
}
}
private void displayTokenUsingStopAnalyzer() throws IOException {
String text
= "Lucene is simple yet powerful java based search library.";
Analyzer analyzer = new StopAnalyzer(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]