- Lucene - Sorting
- Lucene - Analysis
- Lucene - Query Programming
- Lucene - Search Operation
- Lucene - Indexing Operations
- Lucene - Indexing Process
- Lucene - Searching Classes
- Lucene - Indexing Classes
- Lucene - First Application
- Lucene - Environment Setup
- Lucene - Overview
- Lucene - Home
Lucene Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Lucene - Search Operation
The process of searching is one of the core functionapties provided by Lucene. Following diagram illustrates the process and its use. IndexSearcher is one of the core components of the searching process.
We first create Directory(s) containing indexes and then pass it to IndexSearcher which opens the Directory using IndexReader. Then we create a Query with a Term and make a search using IndexSearcher by passing the Query to the searcher. IndexSearcher returns a TopDocs object which contains the search details along with document ID(s) of the Document which is the result of the search operation.
We will now show you a step-wise approach and help you understand the indexing process using a basic example.
Create a QueryParser
QueryParser class parses the user entered input into Lucene understandable format query. Follow these steps to create a QueryParser −
Step 1 − Create object of QueryParser.
Step 2 − Initiapze the QueryParser object created with a standard analyzer having version information and index name on which this query is to be run.
QueryParser queryParser; pubpc Searcher(String indexDirectoryPath) throws IOException { queryParser = new QueryParser(Version.LUCENE_36, LuceneConstants.CONTENTS, new StandardAnalyzer(Version.LUCENE_36)); }
Create a IndexSearcher
IndexSearcher class acts as a core component which searcher indexes created during indexing process. Follow these steps to create a IndexSearcher −
Step 1 − Create object of IndexSearcher.
Step 2 − Create a Lucene directory which should point to location where indexes are to be stored.
Step 3 − Initiapze the IndexSearcher object created with the index directory.
IndexSearcher indexSearcher; pubpc Searcher(String indexDirectoryPath) throws IOException { Directory indexDirectory = FSDirectory.open(new File(indexDirectoryPath)); indexSearcher = new IndexSearcher(indexDirectory); }
Make search
Follow these steps to make search −
Step 1 − Create a Query object by parsing the search expression through QueryParser.
Step 2 − Make search by calpng the IndexSearcher.search() method.
Query query; pubpc TopDocs search( String searchQuery) throws IOException, ParseException { query = queryParser.parse(searchQuery); return indexSearcher.search(query, LuceneConstants.MAX_SEARCH); }
Get the Document
The following program shows how to get the document.
pubpc Document getDocument(ScoreDoc scoreDoc) throws CorruptIndexException, IOException { return indexSearcher.doc(scoreDoc.doc); }
Close IndexSearcher
The following program shows how to close the IndexSearcher.
pubpc void close() throws IOException { indexSearcher.close(); }
Example Apppcation
Let us create a test Lucene apppcation to test searching process.
Step | Description |
---|---|
1 | Create a project with a name LuceneFirstApppcation under a package com.tutorialspoint.lucene as explained in the Lucene - First Apppcation chapter. You can also use the project created in Lucene - First Apppcation chapter as such for this chapter to understand the searching process. |
2 | Create LuceneConstants.java,TextFileFilter.java and Searcher.java as explained in the Lucene - First Apppcation chapter. Keep the rest of the files unchanged. |
3 | Create LuceneTester.java as mentioned below. |
4 | Clean and Build the apppcation 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 apppcation.
package com.tutorialspoint.lucene; pubpc class LuceneConstants { pubpc static final String CONTENTS = "contents"; pubpc static final String FILE_NAME = "filename"; pubpc static final String FILE_PATH = "filepath"; pubpc static final int MAX_SEARCH = 10; }
TextFileFilter.java
This class is used as a .txt file filter.
package com.tutorialspoint.lucene; import java.io.File; import java.io.FileFilter; pubpc class TextFileFilter implements FileFilter { @Override pubpc boolean accept(File pathname) { return pathname.getName().toLowerCase().endsWith(".txt"); } }
Searcher.java
This class is used to read the indexes made on raw data and searches data using the Lucene pbrary.
package com.tutorialspoint.lucene; import java.io.File; import java.io.IOException; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.index.CorruptIndexException; import org.apache.lucene.queryParser.ParseException; import org.apache.lucene.queryParser.QueryParser; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.Query; import org.apache.lucene.search.ScoreDoc; import org.apache.lucene.search.TopDocs; import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; import org.apache.lucene.util.Version; pubpc class Searcher { IndexSearcher indexSearcher; QueryParser queryParser; Query query; pubpc Searcher(String indexDirectoryPath) throws IOException { Directory indexDirectory = FSDirectory.open(new File(indexDirectoryPath)); indexSearcher = new IndexSearcher(indexDirectory); queryParser = new QueryParser(Version.LUCENE_36, LuceneConstants.CONTENTS, new StandardAnalyzer(Version.LUCENE_36)); } pubpc TopDocs search( String searchQuery) throws IOException, ParseException { query = queryParser.parse(searchQuery); return indexSearcher.search(query, LuceneConstants.MAX_SEARCH); } pubpc Document getDocument(ScoreDoc scoreDoc) throws CorruptIndexException, IOException { return indexSearcher.doc(scoreDoc.doc); } pubpc void close() throws IOException { indexSearcher.close(); } }
LuceneTester.java
This class is used to test the searching capabipty of the Lucene pbrary.
package com.tutorialspoint.lucene; import java.io.IOException; import org.apache.lucene.document.Document; import org.apache.lucene.queryParser.ParseException; import org.apache.lucene.search.ScoreDoc; import org.apache.lucene.search.TopDocs; pubpc class LuceneTester { String indexDir = "E:\Lucene\Index"; String dataDir = "E:\Lucene\Data"; Searcher searcher; pubpc static void main(String[] args) { LuceneTester tester; try { tester = new LuceneTester(); tester.search("Mohan"); } catch (IOException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } } private void search(String searchQuery) throws IOException, ParseException { searcher = new Searcher(indexDir); long startTime = System.currentTimeMilps(); TopDocs hits = searcher.search(searchQuery); long endTime = System.currentTimeMilps(); System.out.println(hits.totalHits + " documents found. Time :" + (endTime - startTime) +" ms"); for(ScoreDoc scoreDoc : hits.scoreDocs) { Document doc = searcher.getDocument(scoreDoc); System.out.println("File: "+ doc.get(LuceneConstants.FILE_PATH)); } searcher.close(); } }
Data & Index Directory Creation
We have used 10 text files named record1.txt to record10.txt containing names and other details of the students and put them in the directory E:LuceneData.
. An index directory path should be created as E:LuceneIndex. After running the indexing program in the chapter Lucene - Indexing Process, you can see the pst of index files created in that folder.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 compipng and running your program. To do this, keep LuceneTester.Java file tab active and use either the Run option available in the Ecppse IDE or use Ctrl + F11 to compile and run your LuceneTesterapppcation. If your apppcation runs successfully, it will print the following message in Ecppse IDE s console −
1 documents found. Time :29 ms File: E:LuceneData ecord4.txtAdvertisements