Wednesday, July 26, 2017

Lucy Queries and Plastic - A type of Lucene Query for ElasticSearch

Introduction

A Lucy Query is a query that is very similar to a Lucene query and a Solr query, however, not as robust at this time, nor do I feel it needs to be for my uses.

Plastic takes a Lucy Query and converts it at run-time into a boolean query for ElasticSearch.

Generating Lucene or Solr queries programatically is relatively straightforward, however generating ElasticSearch queries programatically is difficult due to ElasticSearch's use of a fluent (dot type) syntax where you generate objects and nest other objects inside of those, etc. It is much easier to generate a text string that specifies a query.

A text string query specification also allows for the creation of a query template and facilitates Query by Example (QBE) queries.

Lucy Query

Lucy queries are similar to Solr queries and to Lucene queries. Lucy queries do not support the syntactical sugar for AND and OR found in Solr. Lucy queries only use the operators for required (+), not required, and not (!).

For example:
+name:"john" phone:"555-2121"

The name is required and the phone is not. Run the above Lucy query through Plastic and the following ElasticSearch query is generated:

Lucy queries support grouping of query terms. If the above query is modified to use groups like this:
+(name:"john") (phone:"555-2121")

Then the results after running the Lucy query through Plastic is very different but results in a query that is equivalent to the original query. Notice how the grouping creates a new boolean query in ElasticSearch for each group, it nests the query terms exactly like the Lucy query specifies.