Class: Blazer::Adapters::ElasticsearchAdapter

Inherits:
BaseAdapter
  • Object
show all
Defined in:
lib/blazer/adapters/elasticsearch_adapter.rb

Instance Attribute Summary

Attributes inherited from BaseAdapter

#data_source

Instance Method Summary collapse

Methods inherited from BaseAdapter

#cachable?, #cancel, #cost, #explain, #initialize, #reconnect, #schema

Constructor Details

This class inherits a constructor from Blazer::Adapters::BaseAdapter

Instance Method Details

#preview_statementObject



35
36
37
# File 'lib/blazer/adapters/elasticsearch_adapter.rb', line 35

def preview_statement
  "SELECT * FROM \"{table}\" LIMIT 10"
end

#run_statement(statement, comment) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/blazer/adapters/elasticsearch_adapter.rb', line 4

def run_statement(statement, comment)
  columns = []
  rows = []
  error = nil

  begin
    response = client.xpack.sql.query(body: {query: "#{statement} /*#{comment}*/"})
    columns = response["columns"].map { |v| v["name"] }
    # Elasticsearch does not differentiate between dates and times
    date_indexes = response["columns"].each_index.select { |i| response["columns"][i]["type"] == "date" }
    if columns.any?
      rows = response["rows"]
      date_indexes.each do |i|
        rows.each do |row|
          row[i] = Time.parse(row[i])
        end
      end
    end
  rescue => e
    error = e.message
  end

  [columns, rows, error]
end

#tablesObject



29
30
31
32
33
# File 'lib/blazer/adapters/elasticsearch_adapter.rb', line 29

def tables
  indices = client.cat.indices(format: "json").map { |v| v["index"] }
  aliases = client.cat.aliases(format: "json").map { |v| v["alias"] }
  (indices + aliases).uniq.sort
end