Skip to content
Go to file

Latest commit


Git stats


Failed to load latest commit information.
Latest commit message
Commit time

Build a query parser example code

This is example code for my tutorial Build a query parser.

How the code is organized

Each query parser is self-contained to make the tutorial easier to follow. This does mean there's a lot of duplication. In order to keep the classes separate, each query parser is contained in its own module.

The sequence of query parsers is:

  1. TermParser
  2. BooleanTermParser
  3. PhraseParser
  4. HeuristicParser

Installing and running


You will need Ruby (tested with 2.4) to run the query parsers and Java (tested with 1.8) to run Elasticsearch if you want to try out the parsers for real. I use RVM and jEnv to manage the versions, but you do not have to.

Install dependencies

bundle install

Run unit tests

bundle exec rake test

Run integration tests

The integration tests require Elasticsearch to be started.


In another terminal:

bundle exec rake integration_test

Query generation console

The parse program allows you to test out the different parser and see the parse tree and Elasticsearch DSL that is generated based on a query string you provide.

To run it:

bundle exec bin/parse

The program defaults to the HeuristicParser because it is the most featureful. To try one of the others, provide it as a command-line argument:

bundle exec bin/parse BooleanTermParser


The source code in this repository is released into the public domain.

The tutorial is under copyright and cannot be republished without my permission.


tutorial for building a query parser using Ruby, Parslet, and Elasticsearch




No releases published
You can’t perform that action at this time.