|
| 1 | +# Licensed to Elasticsearch B.V. under one or more contributor |
| 2 | +# license agreements. See the NOTICE file distributed with |
| 3 | +# this work for additional information regarding copyright |
| 4 | +# ownership. Elasticsearch B.V. licenses this file to you under |
| 5 | +# the Apache License, Version 2.0 (the "License"); you may |
| 6 | +# not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | +require_relative 'helpers_spec_helper' |
| 18 | +require 'elasticsearch/helpers/esql_helper' |
| 19 | + |
| 20 | +context 'Elasticsearch client helpers' do |
| 21 | + let(:index) { 'esql_helper_test' } |
| 22 | + let(:body) { { size: 12, query: { match_all: {} } } } |
| 23 | + let(:esql_helper) { Elasticsearch::Helpers::ESQLHelper } |
| 24 | + |
| 25 | + before do |
| 26 | + client.indices.create( |
| 27 | + index: index, |
| 28 | + body: { |
| 29 | + mappings: { |
| 30 | + properties: { 'client.ip' => { type: 'ip' }, message: { type: 'keyword' } } |
| 31 | + } |
| 32 | + } |
| 33 | + ) |
| 34 | + client.bulk( |
| 35 | + index: index, |
| 36 | + body: [ |
| 37 | + {'index': {}}, |
| 38 | + {'@timestamp' => '2023-10-23T12:15:03.360Z', 'client.ip' => '172.21.2.162', message: 'Connected to 10.1.0.3', 'event.duration' => 3450233}, |
| 39 | + {'index': {}}, |
| 40 | + {'@timestamp' => '2023-10-23T12:27:28.948Z', 'client.ip' => '172.21.2.113', message: 'Connected to 10.1.0.2', 'event.duration' => 2764889}, |
| 41 | + {'index': {}}, |
| 42 | + {'@timestamp' => '2023-10-23T13:33:34.937Z', 'client.ip' => '172.21.0.5', message: 'Disconnected', 'event.duration' => 1232382}, |
| 43 | + {'index': {}}, |
| 44 | + {'@timestamp' => '2023-10-23T13:51:54.732Z', 'client.ip' => '172.21.3.15', message: 'Connection error', 'event.duration' => 725448}, |
| 45 | + {'index': {}}, |
| 46 | + {'@timestamp' => '2023-10-23T13:52:55.015Z', 'client.ip' => '172.21.3.15', message: 'Connection error', 'event.duration' => 8268153}, |
| 47 | + {'index': {}}, |
| 48 | + {'@timestamp' => '2023-10-23T13:53:55.832Z', 'client.ip' => '172.21.3.15', message: 'Connection error', 'event.duration' => 5033755}, |
| 49 | + {'index': {}}, |
| 50 | + {'@timestamp' => '2023-10-23T13:55:01.543Z', 'client.ip' => '172.21.3.15', message: 'Connected to 10.1.0.1', 'event.duration' => 1756467} |
| 51 | + ], |
| 52 | + refresh: true |
| 53 | + ) |
| 54 | + end |
| 55 | + |
| 56 | + after do |
| 57 | + client.indices.delete(index: index) |
| 58 | + end |
| 59 | + |
| 60 | + it 'returns an ESQL response as a relational key/value object' do |
| 61 | + query = <<~ESQL |
| 62 | + FROM #{index} |
| 63 | + | EVAL duration_ms = ROUND(event.duration / 1000000.0, 1) |
| 64 | + ESQL |
| 65 | + response = esql_helper.query(client, query) |
| 66 | + |
| 67 | + expect(response.count).to eq 7 |
| 68 | + expect(response.first.keys).to eq ['duration_ms', 'message', 'event.duration', 'client.ip', '@timestamp'] |
| 69 | + end |
| 70 | +end |
0 commit comments