Creating and writing documents to an index

This recipe shows you how to index a document. In fact, here we are putting together all that we learned so far from the previous recipes. Let's see how it is done.

How to do it...

The following code sample shows you an example of adding a simple document to an index:

public class LuceneTest { public static void main(String[] args) throws IOException { Analyzer analyzer = new WhitespaceAnalyzer(); Directory directory = new RAMDirectory(); IndexWriterConfig config = new IndexWriterConfig(Version.LATEST, analyzer); IndexWriter indexWriter = new IndexWriter(directory, config); Document doc = new Document(); String text = "Lucene is an Information Retrieval library written in Java"; doc.add(new TextField("fieldname", ...

Get Lucene 4 Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.