Lucene - EnglishAnalyzer



EnglishAnalyzer is a specific analyzer for English language.

Class Declaration

Following is the declaration for org.apache.lucene.analysis.en.EnglishAnalyzer class −

public final class EnglishAnalyzer
   extends StopwordAnalyzerBase
S.No. Field & Description
1

static final CharArraySet ENGLISH_STOP_WORDS_SET

An unmodifiable set containing some common English words that are not usually useful for searching.

S.No. Constructor & Description
1

EnglishAnalyzer()

Builds an analyzer with the default stop words: getDefaultStopSet().

2

EnglishAnalyzer(CharArraySet stopwords)

Builds an analyzer with the given stop words.

3

EnglishAnalyzer(CharArraySet stopwords, CharArraySet stemExclusionSet)

Builds an analyzer with the given stop words.

S.No. Method & Description
1

protected Analyzer.TokenStreamComponents createComponents(String fieldName)

Creates a Analyzer.TokenStreamComponents which tokenizes all the text in the provided Reader.

2

static CharArraySet getDefaultStopSet()

Returns an unmodifiable instance of the default stop words set.

3

protected TokenStream normalize(String fieldName, TokenStream in)

 

Methods Inherited

This class inherits methods from the following classes −

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

Usage of EnglishAnalyzer

private void displayTokenUsingEnglishAnalyzer() throws IOException {
   String text = "Lucene is simple yet powerful java based search library.";
   Analyzer analyzer = new EnglishAnalyzer();
   TokenStream tokenStream = analyzer.tokenStream(
   LuceneConstants.CONTENTS, new StringReader(text));
   CharTermAttribute term = tokenStream.addAttribute(CharTermAttribute.class);
   tokenStream.reset();
   while(tokenStream.incrementToken()) {
      System.out.print("[" + term.toString() + "] ");
   }
   analyzer.close();
}

Example Application

To test search using EnglishAnalyzer, let us create a test Lucene application.

Step Description
1

Create a project with a name LuceneFirstApplication under a package com.tutorialspoint.lucene as explained in the Lucene - First Application chapter. You can also use the project created in Lucene - First Application chapter as such for this chapter to understand the searching process.

2

Create LuceneConstants.java as explained in the Lucene - First Application chapter. Keep the rest of the files unchanged.

3

Create LuceneTester.java as mentioned below.

4

Clean and Build the application to make sure business logic is working as per the requirements.

LuceneConstants.java

This class is used to provide various constants to be used across the sample application.

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

This class is used to test the searching capability of the Lucene library.

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.en.EnglishAnalyzer;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;

public class LuceneTester {
	
   public static void main(String[] args) {
      LuceneTester tester;

      tester = new LuceneTester();
   
      try {
         tester.displayTokenUsingEnglishAnalyzer();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }

   private void displayTokenUsingEnglishAnalyzer() throws IOException {
      String text = "Lucene is simple yet powerful java based search library.";
      Analyzer analyzer = new EnglishAnalyzer();
      TokenStream tokenStream = analyzer.tokenStream(
      LuceneConstants.CONTENTS, new StringReader(text));
      CharTermAttribute term = tokenStream.addAttribute(CharTermAttribute.class);
      tokenStream.reset();
      while(tokenStream.incrementToken()) {
         System.out.print("[" + term.toString() + "] ");
      }
      analyzer.close();
   }
}

Running the Program

Once you are done with the creation of the source, the raw data, the data directory, the index directory and the indexes, you can proceed by compiling and running your program. To do this, keep the LuceneTester.Java file tab active and use either the Run option available in the Eclipse IDE or use Ctrl + F11 to compile and run your LuceneTester application. If your application runs successfully, it will print the following message in Eclipse IDE's console −

Output

[lucen] [simpl] [yet] [power] [java] [base] [search] [librari] 
Advertisements