Lucene - 空格分析器



此分析器根據空格將文件中的文字分割。

類宣告

以下是org.apache.lucene.analysis.WhitespaceAnalyzer類的宣告:

public final class WhitespaceAnalyzer
   extends ReusableAnalyzerBase

類建構函式

序號 建構函式及描述
1

WhitespaceAnalyzer()

已棄用。請改用 WhitespaceAnalyzer(Version) 代替。

2

WhitespaceAnalyzer(Version matchVersion)

建立一個新的 WhitespaceAnalyzer。

類方法

下表顯示了不同的類方法:

序號 方法及描述
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 displayTokenUsingWhitespaceAnalyzer() throws IOException {
   String text = "Lucene is simple yet powerful java based search library.";
   Analyzer analyzer = new WhitespaceAnalyzer(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.TokenStream;
import org.apache.lucene.analysis.WhitespaceAnalyzer;
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.displayTokenUsingWhitespaceAnalyzer();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }

   private void displayTokenUsingWhitespaceAnalyzer() throws IOException {
      String text 
         = "Lucene is simple yet powerful java based search library.";
      Analyzer analyzer = new WhitespaceAnalyzer(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.]
lucene_analysis.htm
廣告

© . All rights reserved.