
- Lucene Tutorial
- Lucene - Home
- Lucene - Overview
- Lucene - Environment Setup
- Lucene - First Application
- Lucene - Indexing Classes
- Lucene - Searching Classes
- Lucene - Indexing Process
- Lucene - Indexing Operations
- Lucene - Search Operation
- Lucene - Query Programming
- Lucene - Analysis
- Lucene - Sorting
- Lucene Useful Resources
- Lucene - Quick Guide
- Lucene - Useful Resources
- Lucene - Discussion
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Lucene - SimpleAnalyzer
This analyzer splits the text in a document based on non-letter characters and then puts them in lowercase.
Class Declaration
Following is the declaration for the org.apache.lucene.analysis.SimpleAnalyzer class −
public final class SimpleAnalyzer extends ReusableAnalyzerBase
Class Constructors
The following table shows the different class constructors −
S.No. | Constructor & Description |
---|---|
1 | SimpleAnalyzer() Deprecated. Use SimpleAnalyzer(Version) instead. |
2 | SimpleAnalyzer(Version matchVersion) Creates a new SimpleAnalyzer. |
Class Methods
The following table shows the different class methods −
S.No. | Method & Description |
---|---|
1 | protected Reusable Analyzer Base. Token Stream Components create Components (String field Name, Reader reader) Creates a new ReusableAnalyzerBase.TokenStreamComponents instance for this analyzer. |
Methods Inherited
This class inherits methods from the following classes −
- org.apache.lucene.analysis.ReusableAnalyzerBase
- org.apache.lucene.analysis.Analyzer
- java.lang.Object
Usage
private void displayTokenUsingSimpleAnalyzer() throws IOException { String text = "Lucene is simple yet powerful java based search library."; Analyzer analyzer = new SimpleAnalyzer(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
Let us create a test Lucene application to test search using BooleanQuery.
Step | Description |
---|---|
1 | Create a project with a name LuceneFirstApplication under a packagecom.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 searching process. |
2 | Create LuceneConstants.java as explained in the Lucene - First Applicationchapter. Keep 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.SimpleAnalyzer; 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.displayTokenUsingSimpleAnalyzer(); } catch (IOException e) { e.printStackTrace(); } } private void displayTokenUsingSimpleAnalyzer() throws IOException { String text = "Lucene is simple yet powerful java based search library."; Analyzer analyzer = new SimpleAnalyzer(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
Once you are done with the creation of the source, 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 −
[lucene] [is] [simple] [yet] [powerful] [java] [based] [search] [library]