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



38
39
40
# File 'lib/blazer/adapters/elasticsearch_adapter.rb', line 38

def preview_statement
  %!// header\n{"index": "{table}"}\n\n// body\n{"query": {"match_all": {}}, "size": 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
28
29
30
31
32
# File 'lib/blazer/adapters/elasticsearch_adapter.rb', line 4

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

  begin
    header, body = statement.gsub(/\/\/.+/, "").strip.split("\n", 2)
    body = JSON.parse(body)
    body["timeout"] ||= data_source.timeout if data_source.timeout
    response = client.msearch(body: [JSON.parse(header), body])["responses"].first
    if response["error"]
      error = response["error"]
    else
      hits = response["hits"]["hits"]
      source_keys = hits.flat_map { |r| r["_source"].keys }.uniq
      hit_keys = (hits.first.try(:keys) || []) - ["_source"]
      columns = source_keys + hit_keys
      rows =
        hits.map do |r|
          source = r["_source"]
          source_keys.map { |k| source[k] } + hit_keys.map { |k| r[k] }
        end
    end
  rescue => e
    error = e.message
  end

  [columns, rows, error]
end

#tablesObject



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

def tables
  client.indices.get_aliases(name: "*").map { |k, v| [k, v["aliases"].keys] }.flatten.uniq.sort
end