Example of indexing and searching with Apache Lucene
Apache Lucene is a high-performance text search engine library written entirely in Java.
This example application demonstrates how to perform some operations with Apache Lucene: This application parses some JSON files with Jackson, indexes their content with Lucene and performs some searches.
Building and running this application
- Open a command line window or terminal.
- Navigate to the root directory of the project, where the
pom.xml
resides. - Compile the project:
mvn clean compile
. - Package the application:
mvn package
. - Change into the
target
directory:cd target
- You should see a file with the following or a similar name:
index-and-search-with-lucene-1.0.jar
. - Execute the JAR:
java -jar index-and-search-with-lucene-1.0.jar
. - The application should be executed and the result should be displayed in the console.
Indexing and searching with Apache Lucene
A Lucene index (Directory
) is a collection of entries (Document
) that contains properties (Field
).
A writer (IndexWriter
) allows you to add entries to the index while a searcher (IndexSearcher
) allows you to execute queries (Query
) against the index and get the results (TopDocs
).
For a better on understanding how Apache Lucene works, here's an example on how to perform some simple indexing and searching operations:
Create an index
// Create an index in memory
Directory index = new RAMDirectory();
Create an index writer
// Create an index writer
StandardAnalyzer analyzer = new StandardAnalyzer();
IndexWriterConfig config = new IndexWriterConfig(analyzer);
IndexWriter indexWriter = new IndexWriter(index, config);
Add entries to the index
// Add a document to the index
Document document = new Document();
document.add(new TextField("name", "John Doe", Field.Store.YES));
document.add(new TextField("address", "80 Summer Hill", Field.Store.YES));
indexWriter.addDocument(document);
// Add a document to the index
document = new Document();
document.add(new TextField("name", "Jane Doe", Field.Store.YES));
document.add(new TextField("address", "9 Main Circle", Field.Store.YES));
indexWriter.addDocument(document);
// Add a document to the index
document = new Document();
document.add(new TextField("name", "John Smith", Field.Store.YES));
document.add(new TextField("address", "9 Dexter Avenue", Field.Store.YES));
indexWriter.addDocument(document);
indexWriter.close();
Create an index searcher
// Create an index searcher
IndexReader reader = DirectoryReader.open(index);
IndexSearcher searcher = new IndexSearcher(reader);
Create a query
// Create a query to look for people with "doe" in the name
Query query = new TermQuery(new Term("name", "doe"));
Execute the query and display the results
// Execute que query and show the results
TopDocs topDocs = searcher.search(query, 10);
// Display addresses
for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
document = searcher.doc(scoreDoc.doc);
System.out.println(document.get("address"));
}