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
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Constructing queries


Now that we have an IndexSearcher, we are ready to proceed to querying. To begin, we will need to construct a Query object to pass into IndexSearch's search method. There are a couple ways to construct a Query. Using a QueryParser class or constructing a Query programmatically, QueryParser provides the utility to interpret text and convert it into a Query. If you need to use search modifiers (for example, term must exist), there is a certain syntax to follow in order to form a query string. By default, a search phrase will return any documents that have matches on any of the query terms. We will demonstrate both QueryParser and constructing your own Query in this section.

Note

Note that if you use QueryParser, you must also use an analyzer to analyze the query text.

How to do it...

Let's take a look at the following code snippets:

Code snippet 1:

Directory directory = 
    FSDirectory.open(new File("/data/index"));
IndexReader indexReader = DirectoryReader.open(directory...