Lucene provides a rich query syntax allowing users to easily create simple queries and scale up to complex ones. A number of search back-ends are built on top of Lucene nowadays such as ElasticSearch and Solr. In this article we will explore using Lucene’s query syntax against relational databases.

We used Groovy here, but it would be straightforward to convert this to Java. For the purposes of keeping things simple, we will work with two main classes as shown below.

 

In the code above, QueryBuilder is the base class where Lucene query segments are processed. For each segment type, an abstract method is defined. QueryBuilder also allows defining field name mapping. This allows your Lucene field names to be different from your SQL column names. SqlQueryBuilder extends this class, providing the actual conversion to SQL.

An example of running this code is shown with the following script

 

To build and run this code:

$GROOVY_HOME/bin/groovyc -cp lucene-core-3.3.0.jar QueryBuilder.groovy SqlQueryBuilder.groovy TestQuery.groovy

$GROOVY_HOME/bin/groovy -cp lucene-core-3.3.0.jar:. TestQuery

In the samples above the output from SqlQueryBuilder would create the WHEREclause. This would be joined with a standard SQL SELECT statement to form the complete query to execute.