Skip to content

Commit a1b9976

Browse files
committed
[DOCS] Updates docs
1 parent d30155d commit a1b9976

File tree

2 files changed

+50
-22
lines changed

2 files changed

+50
-22
lines changed

docs/helpers.asciidoc

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,9 @@ scroll_helper.clear
165165
[[esql-helper]]
166166
=== ES|QL Helper
167167

168-
This helper provides an object response from the ESQL `query` API instead of the default JSON value.
168+
This functionality is Experimental and may be changed or removed completely in a future release. If you have any feedback on this helper, please https://github.com/elastic/elasticsearch-ruby/issues/new/choose[let us know].
169+
170+
The helper provides an object response from the ESQL `query` API instead of the default JSON value.
169171

170172
To use the ES|QL helper, require it in your code:
171173

@@ -208,32 +210,48 @@ The helper returns an array of hashes with the columns as keys and the respectiv
208210

209211
[source,ruby]
210212
----
211-
require 'elasticsearch/helpers/esql_helper'
212213
response = Elasticsearch::Helpers::ESQLHelper.query(client, query)
213214
214215
puts response
215-
[
216-
{"duration_ms"=>3.5, "message"=>"Connected to 10.1.0.3", "event.duration"=>3450233, "client.ip"=>"172.21.2.162", "@timestamp"=>"2023-10-23T12:15:03.360Z"}
217-
{"duration_ms"=>2.8, "message"=>"Connected to 10.1.0.2", "event.duration"=>2764889, "client.ip"=>"172.21.2.113", "@timestamp"=>"2023-10-23T12:27:28.948Z"}
218-
{"duration_ms"=>1.2, "message"=>"Disconnected", "event.duration"=>1232382, "client.ip"=>"172.21.0.5", "@timestamp"=>"2023-10-23T13:33:34.937Z"}
219-
{"duration_ms"=>0.7, "message"=>"Connection error", "event.duration"=>725448, "client.ip"=>"172.21.3.15", "@timestamp"=>"2023-10-23T13:51:54.732Z"}
220-
{"duration_ms"=>8.3, "message"=>"Connection error", "event.duration"=>8268153, "client.ip"=>"172.21.3.15", "@timestamp"=>"2023-10-23T13:52:55.015Z"}
221-
{"duration_ms"=>5.0, "message"=>"Connection error", "event.duration"=>5033755, "client.ip"=>"172.21.3.15", "@timestamp"=>"2023-10-23T13:53:55.832Z"}
222-
{"duration_ms"=>1.8, "message"=>"Connected to 10.1.0.1", "event.duration"=>1756467, "client.ip"=>"172.21.3.15", "@timestamp"=>"2023-10-23T13:55:01.543Z"}
223-
]
216+
217+
{"duration_ms"=>3.5, "message"=>"Connected to 10.1.0.3", "event.duration"=>3450233, "client.ip"=>"172.21.2.162", "@timestamp"=>"2023-10-23T12:15:03.360Z"}
218+
{"duration_ms"=>2.8, "message"=>"Connected to 10.1.0.2", "event.duration"=>2764889, "client.ip"=>"172.21.2.113", "@timestamp"=>"2023-10-23T12:27:28.948Z"}
219+
{"duration_ms"=>1.2, "message"=>"Disconnected", "event.duration"=>1232382, "client.ip"=>"172.21.0.5", "@timestamp"=>"2023-10-23T13:33:34.937Z"}
220+
{"duration_ms"=>0.7, "message"=>"Connection error", "event.duration"=>725448, "client.ip"=>"172.21.3.15", "@timestamp"=>"2023-10-23T13:51:54.732Z"}
221+
{"duration_ms"=>8.3, "message"=>"Connection error", "event.duration"=>8268153, "client.ip"=>"172.21.3.15", "@timestamp"=>"2023-10-23T13:52:55.015Z"}
224222
----
225223

226-
Additionally, a block can be specified to transform the response data. Pass in a block to `query` and it will yield each item in the array of responses.
224+
Additionally, you can transform the data in the response by passing in a Hash of `column => Proc` values. You could use this for example to convert '@timestamp' into a DateTime object. Pass in a Hash to `query` as a `parser` defining a `Proc` for each value you'd like to parse:
227225

228-
You could use this for example to convert '@timestamp' into a DateTime object:
229226
[source,ruby]
230227
----
231228
require 'elasticsearch/helpers/esql_helper'
232229
233-
response = esql_helper.query(client, query).each do |r|
234-
r['@timestamp'] = DateTime.parse(r['@timestamp'])
235-
end
236-
230+
parser = {
231+
'@timestamp' => Proc.new { |t| DateTime.parse(t) }
232+
}
233+
response = Elasticsearch::Helpers::ESQLHelper.query(client, query, parser: parser)
237234
response.first['@timestamp']
238235
# <DateTime: 2023-10-23T12:15:03+00:00 ((2460241j,44103s,360000000n),+0s,2299161j)>
239236
----
237+
238+
You can pass in as many Procs as there are columns in the response. For example:
239+
240+
[source,ruby]
241+
----
242+
parser = {
243+
'@timestamp' => Proc.new { |t| DateTime.parse(t) },
244+
'client.ip' => Proc.new { |i| IPAddr.new(i) },
245+
'event.duration' => Proc.new { |d| d.to_s }
246+
}
247+
248+
response = Elasticsearch::Helpers::ESQLHelper.query(client, query, parser: parser)
249+
250+
puts response
251+
252+
{"duration_ms"=>3.5, "message"=>"Connected to 10.1.0.3", "event.duration"=>"3450233", "client.ip"=>#<IPAddr: IPv4:172.21.2.162/255.255.255.255>, "@timestamp"=>#<DateTime: 2023-10-23T12:15:03+00:00 ((2460241j,44103s,360000000n),+0s,2299161j)>}
253+
{"duration_ms"=>2.8, "message"=>"Connected to 10.1.0.2", "event.duration"=>"2764889", "client.ip"=>#<IPAddr: IPv4:172.21.2.113/255.255.255.255>, "@timestamp"=>#<DateTime: 2023-10-23T12:27:28+00:00 ((2460241j,44848s,948000000n),+0s,2299161j)>}
254+
{"duration_ms"=>1.2, "message"=>"Disconnected", "event.duration"=>"1232382", "client.ip"=>#<IPAddr: IPv4:172.21.0.5/255.255.255.255>, "@timestamp"=>#<DateTime: 2023-10-23T13:33:34+00:00 ((2460241j,48814s,937000000n),+0s,2299161j)>}
255+
{"duration_ms"=>0.7, "message"=>"Connection error", "event.duration"=>"725448", "client.ip"=>#<IPAddr: IPv4:172.21.3.15/255.255.255.255>, "@timestamp"=>#<DateTime: 2023-10-23T13:51:54+00:00 ((2460241j,49914s,732000000n),+0s,2299161j)>}
256+
{"duration_ms"=>8.3, "message"=>"Connection error", "event.duration"=>"8268153", "client.ip"=>#<IPAddr: IPv4:172.21.3.15/255.255.255.255>, "@timestamp"=>#<DateTime: 2023-10-23T13:52:55+00:00 ((2460241j,49975s,15000000n),+0s,2299161j)>}
257+
----

elasticsearch/lib/elasticsearch/helpers/esql_helper.rb

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,26 @@ module ESQLHelper
2525
# Query helper for ES|QL
2626
#
2727
# By default, the `esql.query` API returns a Hash response with the following keys:
28-
# - `columns` with the value being an Array of { name: type } Hashes for each column
29-
# - `values` with the value being an Array of Arrays with the values for each row.
28+
#
29+
# * `columns` with the value being an Array of `{ name: type }` Hashes for each column.
30+
#
31+
# * `values` with the value being an Array of Arrays with the values for each row.
32+
#
3033
# This helper function returns an Array of hashes with the columns as keys and the respective
31-
# values: { column['name'] => value }.
34+
# values: `{ column['name'] => value }`.
3235
#
3336
# @param client [Elasticsearch::Client] an instance of the Client to use for the query.
3437
# @param query [Hash, String] The query to be passed to the ES|QL query API.
3538
# @param params [Hash] options to pass to the ES|QL query API.
36-
# @param convert_datetime [Boolean] Converts datetime values from the response into DateTime
37-
# objects in Ruby. Default: true.
39+
# @param parser [Hash] Hash of column name keys and Proc values to transform the value of
40+
# a given column.
41+
# @example Using the ES|QL helper
42+
# require 'elasticsearch/helpers/esql_helper'
43+
# query = <<~ESQL
44+
# FROM sample_data
45+
# | EVAL duration_ms = ROUND(event.duration / 1000000.0, 1)
46+
# ESQL
47+
# response = Elasticsearch::Helpers::ESQLHelper.query(client, query)
3848
#
3949
# @example Using the ES|QL helper with a parser
4050
# response = Elasticsearch::Helpers::ESQLHelper.query(

0 commit comments

Comments
 (0)