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

#cost, #explain, #initialize, #reconnect

Constructor Details

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

Instance Method Details

#preview_statementObject



32
33
34
# File 'lib/blazer/adapters/elasticsearch_adapter.rb', line 32

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
# 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)
    response = client.msearch(body: [JSON.parse(header), JSON.parse(body)])["responses"].first
    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
  rescue => e
    error = e.message
  end

  [columns, rows, error]
end

#tablesObject



28
29
30
# File 'lib/blazer/adapters/elasticsearch_adapter.rb', line 28

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