目录

StopAnalyzer

此分析器的工作方式类似于SimpleAnalyzer,并删除常用词,如'a','an','the'等。

Class 声明 (Class Declaration)

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

public final class StopAnalyzer
   extends StopwordAnalyzerBase

字段 (Fields)

以下是org.apache.lucene.analysis.StopAnalyzer类的字段 -

  • static Set《?》 ENGLISH_STOP_WORDS_SET - 一个不可修改的集合,包含一些通常不适合搜索的常用英语单词。

类构造函数 (Class Constructors)

下表显示了不同的类构造函数 -

S.No. 构造函数和描述
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)

使用给定集中的停用词构建分析器。

Class Methods

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

S.No. 方法和描述
1

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

创建一个新的ReusableAnalyzerBase.TokenStreamComponents,用于标记所提供的Reader中的所有文本。

方法继承 (Methods Inherited)

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

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

用法 (Usage)

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

例子 Example Application

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

描述
1Lucene - 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.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() + "] ");
      }
   }
}

运行程序 (Running the Program)

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

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