Pārlūkot izejas kodu

Added support for Elasticsearch timeouts

Andrew Kane 8 gadi atpakaļ
vecāks
revīzija
284499ca55
1 mainītis faili ar 16 papildinājumiem un 10 dzēšanām
  1. 16 10
      lib/blazer/adapters/elasticsearch_adapter.rb

+ 16 - 10
lib/blazer/adapters/elasticsearch_adapter.rb

@@ -8,16 +8,22 @@ module Blazer
 
         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
+          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