|
160 | 160 | scroll_helper.clear
|
161 | 161 | ----
|
162 | 162 | --
|
| 163 | + |
| 164 | +[discrete] |
| 165 | +[[esql-helper]] |
| 166 | +=== ES|QL Helper |
| 167 | + |
| 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. |
| 171 | + |
| 172 | +To use the ES|QL helper, require it in your code: |
| 173 | + |
| 174 | +[source,ruby] |
| 175 | +---- |
| 176 | +require 'elasticsearch/helpers/esql_helper' |
| 177 | +---- |
| 178 | + |
| 179 | +By default, the `query` API returns a Hash response with `columns` and `values` like so: |
| 180 | + |
| 181 | +[source,ruby] |
| 182 | +---- |
| 183 | +query = <<ESQL |
| 184 | + FROM sample_data |
| 185 | + | EVAL duration_ms = ROUND(event.duration / 1000000.0, 1) |
| 186 | +ESQL |
| 187 | +
|
| 188 | +response = client.esql.query(body: { query: query}) |
| 189 | +puts response |
| 190 | +
|
| 191 | +{"columns"=>[ |
| 192 | + {"name"=>"@timestamp", "type"=>"date"}, |
| 193 | + {"name"=>"client.ip", "type"=>"ip"}, |
| 194 | + {"name"=>"event.duration", "type"=>"long"}, |
| 195 | + {"name"=>"message", "type"=>"keyword"}, |
| 196 | + {"name"=>"duration_ms", "type"=>"double"} |
| 197 | +], |
| 198 | +"values"=>[ |
| 199 | + ["2023-10-23T12:15:03.360Z", "172.21.2.162", 3450233, "Connected to 10.1.0.3", 3.5], |
| 200 | + ["2023-10-23T12:27:28.948Z", "172.21.2.113", 2764889, "Connected to 10.1.0.2", 2.8], |
| 201 | + ["2023-10-23T13:33:34.937Z", "172.21.0.5", 1232382, "Disconnected", 1.2], |
| 202 | + ["2023-10-23T13:51:54.732Z", "172.21.3.15", 725448, "Connection error", 0.7], |
| 203 | + ["2023-10-23T13:52:55.015Z", "172.21.3.15", 8268153, "Connection error", 8.3], |
| 204 | + ["2023-10-23T13:53:55.832Z", "172.21.3.15", 5033755, "Connection error", 5.0], |
| 205 | + ["2023-10-23T13:55:01.543Z", "172.21.3.15", 1756467, "Connected to 10.1.0.1", 1.8] |
| 206 | +]} |
| 207 | +---- |
| 208 | + |
| 209 | +The helper returns an array of hashes with the columns as keys and the respective values. So for the previous example, it would return the following: |
| 210 | + |
| 211 | +[source,ruby] |
| 212 | +---- |
| 213 | +response = Elasticsearch::Helpers::ESQLHelper.query(client, query) |
| 214 | +
|
| 215 | +puts response |
| 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"} |
| 222 | +---- |
| 223 | + |
| 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: |
| 225 | + |
| 226 | +[source,ruby] |
| 227 | +---- |
| 228 | +require 'elasticsearch/helpers/esql_helper' |
| 229 | +
|
| 230 | +parser = { |
| 231 | + '@timestamp' => Proc.new { |t| DateTime.parse(t) } |
| 232 | +} |
| 233 | +response = Elasticsearch::Helpers::ESQLHelper.query(client, query, parser: parser) |
| 234 | +response.first['@timestamp'] |
| 235 | +# <DateTime: 2023-10-23T12:15:03+00:00 ((2460241j,44103s,360000000n),+0s,2299161j)> |
| 236 | +---- |
| 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 | +---- |
0 commit comments