Book Image

Lucene 4 Cookbook

By : Edwood Ng, Vineeth Mohan
Book Image

Lucene 4 Cookbook

By: Edwood Ng, Vineeth Mohan

Overview of this book

Table of Contents (16 chapters)
Lucene 4 Cookbook
About the Authors
About the Reviewers


Apache Lucene 4 is the backbone of many search engine implementations, including big names such as LinkedIn, Twitter, and IBM. It also serves as a foundation for other open source projects such as Solr and Elasticsearch. We will dive into all the features that Lucene offers and show you easy-to-understand recipes to get you started using Lucene.

What this book covers

Chapter 1, Introducing Lucene, introduces you to the core components of Lucene and gives you the know-how to set up Lucene on your own.

Chapter 2, Analyzing Your Text, explores a key feature of Lucene called Analyzers. We will show you how text analyzing works and how to customize it to suit your needs.

Chapter 3, Indexing Your Data, looks into the data injection process in Lucene and reviews the core concepts, such as norms and similarity.

Chapter 4, Searching Your Indexes, will cover the core search components such as FieldCache and TermVectors, and will give you all the knowledge you need to build an effective search engine.

Chapter 5, Near Real-time Searching, will show you how near real-time search is achieved via various methods and their trade-offs, so you can make educated decisions when designing your search application.

Chapter 6, Querying and Filtering Data, gives you a glimpse of the various querying and filtering features that have been proven to build successful search engines.

Chapter 7, Flexible Scoring, takes a technical dive into the mechanics of scoring, how scores are determined, how it affects ranking positions and what you can do to customize it.

Chapter 8, Introducing Elasticsearch, gives an introduction to a Lucene-based open source search solution that gives you everything you need to build a search application in no time.

Chapter 9, Extending Lucene with Modules, explores the additional features, such as spatial search and faceting, that extend Lucene functionalities beyond just text search.

What you need for this book

Lucene is built in Java so all you need is a Java-supported operating system with JDK 1.6 installed. It's preferable to use Oracle JDK; more information can be found here: Lucene's project page can be found at

Lucene is a library, so there are no installation steps other than copying the library files. One way to include Lucene into your project is through a Maven repository. Here is a sample Maven dependency description to include in the lucene-core library.


Who this book is for

This book is for software developers who are new to Lucene and who want to explore the more advanced topics to build a search engine. Knowledge of Java is necessary to follow the code samples. You will learn core concepts, best practices, and also advanced features, in order to build an effective search application.


In this book, you will find several headings that appear frequently (Getting ready, How to do it…, How it works…, There's more…, and See also).

To give clear instructions on how to complete a recipe, we use these sections as follows:

Getting ready

This section tells you what to expect in the recipe, and describes how to set up any software or any preliminary settings required for the recipe.

How to do it…

This section contains the steps required to follow the recipe.

How it works…

This section usually consists of a detailed explanation of what happened in the previous section.

There's more…

This section consists of additional information about the recipe in order to make the reader more knowledgeable about the recipe.

See also

This section provides helpful links to other useful information for the recipe.


In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "we should let Lucene decide which implementation to use by calling (File path)."

A block of code is set as follows:

indexWriter.deleteDocuments(new Term("id", "1"));"));

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

Reader reader = new StringReader("Text to be passed");
Analyzer analyzer = new SimpleAnalyzer();
TokenStream tokenStream = analyzer.tokenStream("myField", reader);

Any command-line input or output is written as follows:

curl -XDELETE 'http://localhost:9200/news/article/1'

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "The Download button will take you to all available Apache mirrors where you can download Lucene."


Warnings or important notes appear in a box like this.


Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail , and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files from your account at for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit and register to have the files e-mailed directly to you.


Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to and enter the name of the book in the search field. The required information will appear under the Errata section.


Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.


If you have a problem with any aspect of this book, you can contact us at , and we will do our best to address the problem.