- Move elasticsearch support into LogStash::Net::Clients::ElasticSearch

This commit is contained in:
Jordan Sissel 2010-04-26 09:26:06 +00:00
parent 877e527d91
commit b4865b76b0
2 changed files with 52 additions and 66 deletions

View file

@ -0,0 +1,52 @@
#!/usr/bin/ruby
require "rubygems"
require "lib/config/base"
require "uri"
require "json"
require "ap"
require "logger"
require "httpclient"
module LogStash; module Net; module Clients
class ElasticSearch
def initialize(config_file)
@config = LogStash::Config::BaseConfig.new(config_file)
@http = HTTPClient.new
@logger = Logger.new(STDERR)
end
def _get(params, what, path = "")
path.gsub!(/\/+$/, "")
uri = URI.escape("http://#{@config.elasticsearch_host}#{path}/_#{what}")
@logger.info("URL for #{what}: #{uri}")
@logger.info("Body: #{params.to_json}");
# ElasticSearch uses "GET" with body, so we can't call .get() here.
response = @http.request(:get, uri, query = nil, body = params.to_json)
if response.status != 200
ap JSON.parse(response.content)
raise "Search failure (http code #{response.code})"
end
return JSON.parse(response.content)
end
def query(query, path = "")
return ElasticSearch::SearchResults.new(_get(query, "search", path))
end # def query
def count(query, path = "")
return _get(query, "count", path)["count"]
end
end
class ElasticSearch::SearchResults
attr_reader :hits
attr_reader :results
def initialize(data)
@hits = data["hits"]["total"]
@results = data["hits"]["hits"]
end
end # class Logstash::Net::Clients::ElasticSearch::SearchResults
end; end; end # module LogStash::Net::Clients

View file

@ -1,66 +0,0 @@
require "rubygems"
require "uri"
require "json"
require "ap"
require "logger"
require "httpclient"
class ElasticSearch
def initialize(host)
@host = host
@http = HTTPClient.new
@logger = Logger.new(STDERR)
end
def _get(params, what, path = "")
path.gsub!(/\/+$/, "")
uri = URI.escape("http://#{@host}#{path}/_#{what}")
@logger.info("URL for #{what}: #{uri}")
@logger.info("Body: #{params.to_json}");
# ElasticSearch uses "GET" with body, so we can't call .get() here.
response = @http.request(:get, uri, query = nil, body = params.to_json)
if response.status != 200
ap JSON.parse(response.content)
raise "Search failure (http code #{response.code})"
end
return JSON.parse(response.content)
end
def query(query, path = "")
return ElasticSearch::SearchResults.new(_get(query, "search", path))
end # def query
def count(query, path = "")
return _get(query, "count", path)["count"]
end
end
class ElasticSearch::SearchResults
attr_reader :hits
attr_reader :results
def initialize(data)
@hits = data["hits"]["total"]
@results = data["hits"]["hits"]
end
end
if __FILE__ == $0
require "ap"
es = ElasticSearch.new("localhost:9200")
#ap es.query( { :query => { :field => { :progname => "etl-cron"} } }).results
#ap es.query( { :query => { :field => { :@DATE => 1272164175} } }).results
ap es.query(
{ :query =>
{"bool" => {"must" => [{"query_string" => {"query" => ARGV[0],"default_field" => "@LINE"}},{"range" => {"@DATE" => {"to" => Time.now.to_i,"from" => Time.now.to_i - 600}}}]}}
}
)
end
#:bool => { :must => [
#{ :range => { :@DATE => { :from => 1272164175, :to => 1272164176,} } },
##{ :field => { :progname => "etl-cron" } },
#{ :query_string => { :query => "progname:etl-cron", :default_field => "@LINE" } },
#] },
#}