diff --git a/lib/logstash/search/elasticsearch.rb b/lib/logstash/search/elasticsearch.rb index f821cf20c..534e12e27 100644 --- a/lib/logstash/search/elasticsearch.rb +++ b/lib/logstash/search/elasticsearch.rb @@ -122,14 +122,22 @@ class LogStash::Search::ElasticSearch < LogStash::Search::Base result.duration = Time.now - start_time @logger.info(["Got search results", - { :query => query.query_string, :duration => data["duration"], - :results => data["hits"]["hits"].size }]) + { :query => query.query_string, :duration => data["duration"] }]) if req.response_header.status != 200 result.error_message = data["error"] || req.inspect @error = data["error"] || req.inspect end - data["facets"]["amazingpants"]["entries"].each do |entry| + entries = data["facets"]["amazingpants"]["entries"] rescue nil + + if entries.nil? or !data["error"].nil? + # Use the error message if any, otherwise, return the whole + # data object as json as the error message for debugging later. + result.error_message = (data["error"] rescue false) || data.to_json + yield result + next + end + entries.each do |entry| # entry is a hash of keys 'total', 'mean', 'count', and 'key' hist_entry = LogStash::Search::FacetResult::Histogram.new hist_entry.key = entry["key"] diff --git a/lib/logstash/search/facetresult/histogram.rb b/lib/logstash/search/facetresult/histogram.rb index 0581d880a..1851334d5 100644 --- a/lib/logstash/search/facetresult/histogram.rb +++ b/lib/logstash/search/facetresult/histogram.rb @@ -9,8 +9,8 @@ class LogStash::Search::FacetResult::Histogram < LogStash::Search::FacetResult:: attr_accessor :total attr_accessor :count + # sometimes a parent call to to_json calls us with args? def to_json(*args) - p :to_json => args return { "key" => @key, "mean" => @mean, diff --git a/lib/logstash/web/server.rb b/lib/logstash/web/server.rb index 98bbbe526..42ccc849d 100755 --- a/lib/logstash/web/server.rb +++ b/lib/logstash/web/server.rb @@ -163,6 +163,11 @@ class LogStash::Web::Server < Sinatra::Base aget '/api/histogram' do headers({"Content-Type" => "text/plain" }) + if params[:q].nil? + status 500 + body({ "error" => "No query given (missing 'q' parameter)" }.to_json) + next + end format = (params[:format] or "json") field = (params[:field] or "@timestamp") interval = (params[:interval] or 3600 * 1000) @@ -175,15 +180,18 @@ class LogStash::Web::Server < Sinatra::Base end begin - p results.results.class a = results.results.to_json rescue => e + status 500 + body e.inspect + p :exception => e p e raise e end + status 200 body a end # @backend.search - end # apost '/api/search' + end # aget '/api/histogram' aget '/*' do status 404 if @error