Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Lucene 4 Cookbook

You're reading from  Lucene 4 Cookbook

Product type Book
Published in Jun 2015
Publisher
ISBN-13 9781782162285
Pages 220 pages
Edition 1st Edition
Languages
Authors (2):
Edwood Ng Edwood Ng
Profile icon Edwood Ng
Vineeth Mohan Vineeth Mohan
Profile icon Vineeth Mohan
View More author details

Table of Contents (16) Chapters

Lucene 4 Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Introducing Lucene Analyzing Your Text Indexing Your Data Searching Your Indexes Near Real-time Searching Querying and Filtering Data Flexible Scoring Introducing Elasticsearch Extending Lucene with Modules Index

Obtaining an IndexWriter


The IndexWriter class provides functionality to create and manage index. The class can be found in Lucene-core. It handles basic operations where you can add, delete, and update documents. It also handles more complex use cases that we will cover during the course of this book.

An IndexWriter constructor takes two arguments:

IndexWriterDirectoryIndexWriterConfig
https://lucene.apache.org/core/4_10_0/core/org/apache/lucene/index /IndexWriter.htmlIndexWriter(org.apache.lucene.store.Directory, org.apache.lucene.index.IndexWriterConfig)IndexWriter(https://lucene.apache.org/core/4_10_0/core/org/apache/lucene/store/Directory.html
Directory d, https://lucene.apache.org/core/4_10_0/core/org/apache/lucene/index/IndexWriterConfig.html"IndexWriterConfig conf)

Construct a new IndexWriter as per the settings given in the conf file.

The first argument is a Directory object. Directory is a location where the Lucene index is stored. The second argument is an IndexWriterConfig object where it holds the configuration information. Lucene provides a number of directory implementations. For performance or quick prototyping, we can use RAMDirectory to store the index entirely in memory. Otherwise, the index is typically stored in FSDirectory on a file system. Lucene has several FSDirectory implementations that have different strengths and weaknesses depending on your hardware and environment. In most cases, we should let Lucene decide which implementation to use by calling FSDirectory.open (File path).

How to do it…

We need to first define an analyzer to initialize IndexWriterConfig. Then, a Directory should be created to tell Lucene where to store the index. With these two objects defined, we are ready to instantiate an IndexWriter.

The following is a code snippet that shows you how to obtain an IndexWriter:

  Analyzer analyzer = new WhitespaceAnalyzer();
  Directory directory = new RAMDirectory();
  IndexWriterConfig config = new IndexWriterConfig(Version.LATEST, analyzer);
  IndexWriter indexWriter = new IndexWriter(directory, config);

How it works…

First, we instantiate a WhitespaceAnalyzer to parse input text, and tokenize text into word tokens. Then, we create an in-memory Directory by instantiating RAMDirectory. We configure IndexWriterConfig with the WhitespaceAnalyzer we just created and finally, we pass both Directory and IndexWriterConfig to create an IndexWriter. The IndexWriter is now ready to update the index.

An IndexWriter consists of two major components, directory and analyzer. These are necessary so that Lucene knows where to persist indexing information and what treatment to apply to the documents before they are indexed. Analyzer's treatment is especially important because it maintains data consistency. If an index already exists in the specified directory, Lucene will update the existing index. Otherwise, a new index is created.

You have been reading a chapter from
Lucene 4 Cookbook
Published in: Jun 2015 Publisher: ISBN-13: 9781782162285
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}