I use Elasticsearch a lot at work. One of the nicest (and also most dangerous features) of Elasticsearch is the ability to write ad-hoc queries to see what’s inside.

This is useful for a lot of reasons. Looking at the internal state of elasticsearch can be helpful for debugging issues with your applications. You can also get at interesting insights about your data with a single query instead of writing a whole program.

SQL databases also have this property.

I find that writing SQL is a lot more pleasant than writing ad-hoc elasticsearch queries. SQL almost reads like English, and Elasticsearch queries are deeply nested JSON objects, where getting the braces and quotes just right often requires breaking out a text editor.

So I wrote esq, a tool which turns most simple SQL queries into Elasticsearch queries.

The nice thing about esq, is that emits a complete curl command. You can pipe the output right into sh, or copy the output into an ssh session. It works nicely with port forwarding, but lets you work around it when that’s not an easy option.

So for example, if I had elasticsearch running locally, I could run:

esq 'SELECT * FROM people WHERE age >= 18 AND age <= 25 AND username = "john"' | sh

And this significantly harder-to-write query would run:

curl -XPOST http://localhost:9200/people/_search?pretty=true -H "Content-Type: application/json" -d '{ "query" : { "bool" : { "must" : [ { "range" : { "age" : { "gte" : 18 } } } , { "range" : { "age" : { "lte" : 25 } } } , { "term" : { "username" : "john" } } ] } } }'

Easy! There are many more examples in the readme.