Business intelligence made simple
Use SQL for Elasticsearch
Andrewdd3b18a1c7fd
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex df14993f..412ca9d0 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -1,5 +1,6 @@## 1.9.1 [unreleased]+- Use SQL for Elasticsearch- Fixed error with latest `google-cloud-bigquery`## 1.9.0diff --git a/README.md b/README.mdindex befbeed6..fdda0fff 100644--- a/README.md+++ b/README.md@@ -479,7 +479,7 @@ data_sources:### Elasticsearch-Add [elasticsearch](https://github.com/elastic/elasticsearch-ruby) to your Gemfile and set:+Add [elasticsearch](https://github.com/elastic/elasticsearch-ruby) and [elasticsearch-xpack](https://github.com/elastic/elasticsearch-ruby/tree/master/elasticsearch-xpack) to your Gemfile and set:```ymldata_sources:diff --git a/lib/blazer/adapters/elasticsearch_adapter.rb b/lib/blazer/adapters/elasticsearch_adapter.rbindex b5f2d7b0..01fae86a 100644--- a/lib/blazer/adapters/elasticsearch_adapter.rb+++ b/lib/blazer/adapters/elasticsearch_adapter.rb@@ -7,22 +7,17 @@ module Blazererror = nilbegin- 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] }+ response = client.xpack.sql.query(body: {query: statement})+ 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+ endendrescue => eerror = e.message@@ -32,11 +27,13 @@ module Blazerenddef tables- client.indices.get_aliases(name: "*").map { |k, v| [k, v["aliases"].keys] }.flatten.uniq.sort+ indices = client.cat.indices(format: "json").map { |v| v["index"] }+ aliases = client.cat.aliases(format: "json").map { |v| v["alias"] }+ (indices + aliases).uniq.sortenddef preview_statement- %!// header\n{"index": "{table}"}\n\n// body\n{"query": {"match_all": {}}, "size": 10}!+ "SELECT * FROM \"{table}\" LIMIT 10"endprotected