目录

WhitespaceAnalyzer

该分析器基于空格分割文档中的文本。

Class 声明 (Class Declaration)

以下是org.apache.lucene.analysis.WhitespaceAnalyzer类的声明 -

public final class WhitespaceAnalyzer
   extends ReusableAnalyzerBase

类构造函数 (Class Constructors)

S.No. 构造函数和描述
1

WhitespaceAnalyzer()

已过时。 请改用WhitespaceAnalyzer(Version)。

2

WhitespaceAnalyzer(Version matchVersion)

创建一个新的WhitespaceAnalyzer。

Class Methods

下表显示了不同的类方法 -

S.No. 方法和描述
1

protected Reusable Analyzer Base. Token Stream Components create Components (String field Name, Reader reader)

为此分析器创建一个新的ReusableAnalyzerBase.TokenStreamComponents实例。

方法继承 (Methods Inherited)

该类继承以下类中的方法 -

  • org.apache.lucene.analysis.ReusableAnalyzerBase
  • org.apache.lucene.analysis.Analyzer
  • java.lang.Object

用法 (Usage)

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() + "] ");
   }
}

例子 Example Application

让我们创建一个测试Lucene应用程序来使用BooleanQuery测试搜索。

描述
1

Lucene - First Application章节中解释,在com.iowiki.lucene包下创建一个名为LuceneFirstApplication的项目。 您还可以使用Lucene - First Application创建的项目Lucene - First Application本章的Lucene - First Application章节来理解搜索过程。

2

按照Lucene - First Application章节中的说明创建LuceneConstants.java 。 保持其余文件不变。

3

创建LuceneTester.java ,如下所述。

4

清理并构建应用程序以确保业务逻辑按照要求运行。

LuceneConstants.java

此类用于提供跨示例应用程序使用的各种常量。

package com.iowiki.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.iowiki.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() + "] ");
      }
   }
}

运行程序 (Running the Program)

完成源的创建后,您可以继续编译和运行程序。 为此,请保持LuceneTester.Java文件选项卡处于活动状态,并使用Eclipse IDE中提供的“运行”选项或使用Ctrl + F11编译并运行LuceneTester应用程序。 如果您的应用程序成功运行,它将在Eclipse IDE的控制台中打印以下消息 -

[Lucene] [is] [simple] [yet] [powerful] [java] [based] [search] [library.]
↑回到顶部↑
WIKI教程 @2018