Introduction
The Elasticsearch Ruby client can be used to craft EQ|QL queries and make it easier to work with the data returned from esql.query
. ES|QL allows developers to filter, transform, and analyze data stored in Elasticsearch via queries. It uses "pipes" (|
) to work with the data step by step.
The esql.query
API has been supported in the Elasticsearch Ruby Client since it was available as experimental in version 8.11.0.
You can execute an ES|QL request with the following code:
The default response is parsed from JSON (you can also get a CSV or text by passing in the format
parameter), and it looks like this:
ES|QL Helper
In Elasticsearch Ruby v8.13.0, the client introduced the ES|QL Helper for the esql.query
API. Instead of the default response, the helper returns an array of hashes with the columns as keys and the respective values instead of the default JSON value.
Additionally, you can iterate through the response values and transform the data in by passing in a Hash of column => Proc
values. You could use this for example to convert a @timestamp
column value into a DateTime
object. We'll take a look at how to use this with example data.
Setup and ingesting data
For this example, we're using the JSON dump from TheGamesDB, a community driven crowd-sourced games information website. Once we've downloaded the JSON file, we can ingest it into Elasticsearch by using another Helper form the Ruby client, the Bulk Helper.
The data includes the list of all games in the database within the data.games
keys. It also includes platforms and box art information, but for the purpose of this example, we're only going to use the games data. The BulkHelper provides a way to ingest a JSON file directly into Elasticsearch.
To use the helper, we need to require it in our code, and instantiate it with a client and an index on which to perform the bulk action (we can change the index later on an already instantiated helper). We can use ingest_json
and pass in the JSON file, the keys where it can find the data, and slice
to separate the documents in batches before sending them to Elasticsearch:
This will ingest all the game titles with their respective information into the videogames
index.
Using the ES|QL Helper
With the data loaded, we can now query it with ES|QL:
If we run this query with the esql.query
API directly, we'll get the columns/values result:
The helper however, returns an Array of Hashes with the columns as keys and the respective values. So we can work with the response, and access the value for each Hash in the Array with the name of a column as the key:
The ESQLHelper also provides the ability to transform the data in the response. We can do this by passing in a Hash of column => Proc
values. For example, let's say we want to format the release date in this previous query to show a more human friendly date. We can run this:
If we run the same code from before, we'll get this result:
You can pass in as many Procs as there are columns in the response. For example, the data includes a youtube
field, where sometimes the URL for a video on YouTube is stored, other times just the video hash (e.g. U4bKxcV5hsg
). The URL for a YouTube video follows the convention https://youtube.com/watch?v=VIDEOHASH
. So we could also add a parser to prepend the URL to the values that only include the hash:
If we then run response.map { |a| a['youtube'] }.compact
, we'll get just the URLs for YouTube videos for the videogames we're looking for.
Conclusion
As you can see, the ESQLHelper class can make it easier to work with the data returned from esql.query
. You can learn more about the Elasticsearch Ruby Client and its helpers in the official documentation. And if you have any feedback, questions or requests, don't hesitate to create a new issue in the client's repository.
Elasticsearch is packed with new features to help you build the best search solutions for your use case. Dive into our sample notebooks to learn more, start a free cloud trial, or try Elastic on your local machine now.