Learn to use Apache Lucene 6 to index and search documents. Lucene is used by many different modern search platforms, such as Apache Solr and ElasticSearch, or crawling platforms, such as Apache Nutch for data indexing and searching.
Table of Contents Lucene Maven Dependency Lucene Write Index Example Lucene Search Example Download Sourcecode
Lucene Maven Dependency
Once you create maven project in eclipse, include following lucene dependencies in pom.xml
. I am using Lucene 6.6.0 version.
<properties> <lucene.version>6.6.0</lucene.version> </properties> <dependency> <groupId>org.apache.lucene</groupId> <artifactId>lucene-core</artifactId> <version>${lucene.version}</version> </dependency> <dependency> <groupId>org.apache.lucene</groupId> <artifactId>lucene-analyzers-common</artifactId> <version>${lucene.version}</version> </dependency> <dependency> <groupId>org.apache.lucene</groupId> <artifactId>lucene-queryparser</artifactId> <version>${lucene.version}</version> </dependency>
Lucene Write Index Example
Create IndexWriter
org.apache.lucene.index.IndexWriter
class provides functionality to create and manage index. It’s constructor takes two arguments: FSDirectory
and IndexWriterConfig
. Please note that after the writer is created, the given configuration instance cannot be passed to another writer.
private static IndexWriter createWriter() throws IOException { FSDirectory dir = FSDirectory.open(Paths.get(INDEX_DIR)); IndexWriterConfig config = new IndexWriterConfig(new StandardAnalyzer()); IndexWriter writer = new IndexWriter(dir, config); return writer; }
Create Document
org.apache.lucene.document.Document
class represent Lucene indexed document.
private static Document createDocument(Integer id, String firstName, String lastName, String website) { Document document = new Document(); document.add(new StringField("id", id.toString() , Field.Store.YES)); document.add(new TextField("firstName", firstName , Field.Store.YES)); document.add(new TextField("lastName", lastName , Field.Store.YES)); document.add(new TextField("website", website , Field.Store.YES)); return document; }
Write Document to Index
To write lucene documents to index, use IndexWriter.addDocuments(documents)
method.
package com.howtodoinjava.demo.lucene; import java.io.IOException; import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.document.StringField; import org.apache.lucene.document.TextField; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriterConfig; import org.apache.lucene.store.FSDirectory; public class LuceneWriteIndexExample { private static final String INDEX_DIR = "c:/temp/lucene6index"; public static void main(String[] args) throws Exception { IndexWriter writer = createWriter(); List<Document> documents = new ArrayList<>(); Document document1 = createDocument(1, "Lokesh", "Gupta", "howtodoinjava.com"); documents.add(document1); Document document2 = createDocument(2, "Brian", "Schultz", "example.com"); documents.add(document2); //Let's clean everything first writer.deleteAll(); writer.addDocuments(documents); writer.commit(); writer.close(); } private static Document createDocument(Integer id, String firstName, String lastName, String website) { Document document = new Document(); document.add(new StringField("id", id.toString() , Field.Store.YES)); document.add(new TextField("firstName", firstName , Field.Store.YES)); document.add(new TextField("lastName", lastName , Field.Store.YES)); document.add(new TextField("website", website , Field.Store.YES)); return document; } private static IndexWriter createWriter() throws IOException { FSDirectory dir = FSDirectory.open(Paths.get(INDEX_DIR)); IndexWriterConfig config = new IndexWriterConfig(new StandardAnalyzer()); IndexWriter writer = new IndexWriter(dir, config); return writer; } }
Once you execute above code in your computer, you will see lucene indexes created in configured folder path.

Lucene Search Example
Create IndexSearcher
org.apache.lucene.search.IndexSearcher
is used to search lucene documents from indexes. It takes one argument Directory
, which points to index folder.
private static IndexSearcher createSearcher() throws IOException { Directory dir = FSDirectory.open(Paths.get(INDEX_DIR)); IndexReader reader = DirectoryReader.open(dir); IndexSearcher searcher = new IndexSearcher(reader); return searcher; }
Search Lucene Docs
To search lucene documents, you need to create org.apache.lucene.search.Query
instance using org.apache.lucene.queryparser.classic.QueryParser
class. IndexSearcher.seach(Query)
returns org.apache.lucene.search.TopDocs
which represents the query result.
IndexSearcher searcher = createSearcher(); TopDocs foundDocs = searchById(1, searcher); private static TopDocs searchById(Integer id, IndexSearcher searcher) throws Exception { QueryParser qp = new QueryParser("id", new StandardAnalyzer()); Query idQuery = qp.parse(id.toString()); TopDocs hits = searcher.search(idQuery, 10); return hits; }
Search Index Example
package com.howtodoinjava.demo.lucene; import java.io.IOException; import java.nio.file.Paths; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.index.DirectoryReader; import org.apache.lucene.index.IndexReader; import org.apache.lucene.queryparser.classic.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; public class LuceneReadIndexExample { private static final String INDEX_DIR = "c:/temp/lucene6index"; public static void main(String[] args) throws Exception { IndexSearcher searcher = createSearcher(); //Search by ID TopDocs foundDocs = searchById(1, searcher); System.out.println("Total Results :: " + foundDocs.totalHits); for (ScoreDoc sd : foundDocs.scoreDocs) { Document d = searcher.doc(sd.doc); System.out.println(String.format(d.get("firstName"))); } //Search by firstName TopDocs foundDocs2 = searchByFirstName("Brian", searcher); System.out.println("Total Results :: " + foundDocs2.totalHits); for (ScoreDoc sd : foundDocs2.scoreDocs) { Document d = searcher.doc(sd.doc); System.out.println(String.format(d.get("id"))); } } private static TopDocs searchByFirstName(String firstName, IndexSearcher searcher) throws Exception { QueryParser qp = new QueryParser("firstName", new StandardAnalyzer()); Query firstNameQuery = qp.parse(firstName); TopDocs hits = searcher.search(firstNameQuery, 10); return hits; } private static TopDocs searchById(Integer id, IndexSearcher searcher) throws Exception { QueryParser qp = new QueryParser("id", new StandardAnalyzer()); Query idQuery = qp.parse(id.toString()); TopDocs hits = searcher.search(idQuery, 10); return hits; } private static IndexSearcher createSearcher() throws IOException { Directory dir = FSDirectory.open(Paths.get(INDEX_DIR)); IndexReader reader = DirectoryReader.open(dir); IndexSearcher searcher = new IndexSearcher(reader); return searcher; } }
Output:
Total Results :: 1 Lokesh Total Results :: 1 2
Download Sourcecode
Download sourcecode of this tutorial using below given link.
Happy Learning !!
Reference: Apache Lucene Website
Comments