ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

Using the Lucene Query Parser Without Lucene
Pages: 1, 2, 3, 4

Parse the Query!

There are many interesting articles, so this one will concentrate only on parts of the Lucene project. In particular, we will concentrate on the indexing of documents, which is the heart of Lucene. Each document has to be indexed in advance to be searched. Let's walk through this process very quickly. Lucene analyzes input and creates Document objects. This is a composition of many Field objects, each composed of name-value pairs. You may think about these as "properties" associated with the document. When you want to find a document, all you need is a set of name-value pairs to be used as search criteria. Lucene will find all documents that meet this criteria.

Let's look at an example. Say we have an address book application. Each entry is represented with the Friend class, as shown in Figure 3.

Friend class
Figure 3. Friend class

Here are a few example search queries you might want to run. All are written in query language supported by Lucene QueryParser:

Query Meaning
name:John Find all entries where attribute "name" is equal "John".
name:J* Find all entries where attribute "name" starts with "J".
name: John AND phoneNumber: 1234 Find all entries where attribute "name" is equal to "John" and attribute "phoneNumber" is equal to "1234".
name:J* NOT name: John Find all entries where attribute "name" starts with "J" but is not equal to "John".

A Plan

We assume all entries in the address book application are stored in a database accessed with Hibernate. This is a typical example, used in plenty of real-world applications. All we need is some code between the search form and the Hibernate API that can understand the query language and produce Hibernate criteria. Figure 4 shows what we want to achieve.

Idea
Figure 4. Solution overview

For each query string entered by the user, we invoke QueryParser to build an object representation. Next QueryInterpreter walks through the object tree and uses HibernateQueryBuilder to create the Hibernate criteria. Hibernate will then execute the query and return the result. Simple, isn't it? We are only missing the amber parts shown on the diagram.

Pages: 1, 2, 3, 4

Next Pagearrow