lucene-core contains IndexWriter, IndexSearcher, Directory, and the document/field types. The lucene-analysis-common module provides StandardAnalyzer. The lucene-queryparser module provides QueryParser used in step 4.
2
Create an index
A Directory is Lucene’s abstraction for index storage. Use ByteBuffersDirectory for an in-memory index during development or testing. Use FSDirectory.open(path) to persist an index to disk, as shown in the demo’s IndexFiles.java.
import org.apache.lucene.analysis.standard.StandardAnalyzer;import org.apache.lucene.index.IndexWriter;import org.apache.lucene.index.IndexWriterConfig;import org.apache.lucene.store.ByteBuffersDirectory;import org.apache.lucene.store.Directory;// In-memory directory — no files written to diskDirectory directory = new ByteBuffersDirectory();// StandardAnalyzer tokenizes, lowercases, and removes common stop wordsStandardAnalyzer analyzer = new StandardAnalyzer();IndexWriterConfig config = new IndexWriterConfig(analyzer);IndexWriter writer = new IndexWriter(directory, config);
To write to disk instead, replace the directory line:
A Document is a collection of Fields. Choose the right field type for each piece of data:
TextField — tokenized full-text content (body, title, description). The analyzer runs on the value.
StringField — exact-match string stored and indexed as a single token (IDs, paths, categories). Not analyzed.
LongField — numeric value suitable for range queries and sorting.
import org.apache.lucene.document.Document;import org.apache.lucene.document.Field;import org.apache.lucene.document.StringField;import org.apache.lucene.document.TextField;// First documentDocument doc1 = new Document();doc1.add(new StringField("id", "1", Field.Store.YES));doc1.add(new TextField("title", "Apache Lucene Getting Started", Field.Store.YES));doc1.add(new TextField("body", "Lucene is a high-performance search library written in Java.", Field.Store.YES));writer.addDocument(doc1);// Second documentDocument doc2 = new Document();doc2.add(new StringField("id", "2", Field.Store.YES));doc2.add(new TextField("title", "Indexing and Searching with Lucene", Field.Store.YES));doc2.add(new TextField("body", "IndexWriter writes documents into segments. IndexSearcher queries them.", Field.Store.YES));writer.addDocument(doc2);// Commit makes the documents visible to new readerswriter.commit();writer.close();
Field.Store.YES stores the raw value so you can retrieve it from search results. Field.Store.NO indexes the field for searching but does not store it — useful when you only need to know which document matched, not the original value.
4
Search the index
Open a DirectoryReader to get a read-only view of the index, then wrap it in an IndexSearcher. Use QueryParser to parse a query string against a default field, or construct Query objects directly.
import org.apache.lucene.index.DirectoryReader;import org.apache.lucene.index.StoredFields;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;// Open the index for readingDirectoryReader reader = DirectoryReader.open(directory);IndexSearcher searcher = new IndexSearcher(reader);// Parse a query against the "body" fieldQueryParser parser = new QueryParser("body", new StandardAnalyzer());Query query = parser.parse("Lucene search library");// Search and retrieve the top 10 resultsTopDocs results = searcher.search(query, 10);System.out.println("Found " + results.totalHits.value() + " document(s).");// Retrieve stored field values for each hitStoredFields storedFields = searcher.storedFields();for (ScoreDoc hit : results.scoreDocs) { Document doc = storedFields.document(hit.doc); System.out.printf(" score=%.4f id=%s title=%s%n", hit.score, doc.get("id"), doc.get("title"));}reader.close();
Running this prints something like:
Found 2 document(s). score=0.4855 id=1 title=Apache Lucene Getting Started score=0.3128 id=2 title=Indexing and Searching with Lucene