add elasticsearch adapter #44

Merged
merged 4 commits into from May 12, 2014

3 participants

@salimane

This pr add an elasticsearch adapter for applications that are already using elasticsearch in a multi server environment.
Thanks

@dewski dewski commented on an outdated diff Feb 11, 2014
lib/peek/adapters/elasticsearch.rb
+
+ if result
+ result.to_json
+ else
+ nil
+ end
+ end
+
+ def save
+ begin
+ @client.index index: 'peek_requests_index',
+ type: 'peek_request',
+ id: "#{Peek.request_id}",
+ body: Peek.results.to_json,
+ ttl: @expires_in
+ rescue ::Elasticsearch::Transport::Transport::Errors::BadRequest
@dewski
peek member
dewski added a line comment Feb 11, 2014

The begin block can be avoided here and just using an inline rescue:

def save
  # code
rescue ::Elasticsearch::Transport::Transport::Errors::BadRequest
  false
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@dewski dewski commented on an outdated diff Feb 11, 2014
lib/peek/adapters/elasticsearch.rb
@@ -0,0 +1,40 @@
+require 'peek/adapters/base'
+require 'elasticsearch'
+
+module Peek
+ module Adapters
+ class Elasticsearch < Base
+ def initialize(options = {})
+ @client = options.fetch(:client, ::Elasticsearch::Client.new)
+ @expires_in = Integer(options.fetch(:expires_in, 60 * 30) * 1000)
+ end
+
+ def get(request_id)
+ begin
@dewski
peek member
dewski added a line comment Feb 11, 2014

This can be simplified a bit:

def get(request_id)
  result = @client.get_source :index => 'peek_requests_index', :type => 'peek_request', :id => request_id
  result.to_json
rescue ::Elasticsearch::Transport::Transport::Errors::NotFound
  # pass
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@dewski dewski commented on an outdated diff Feb 11, 2014
lib/peek/adapters/elasticsearch.rb
@@ -0,0 +1,40 @@
+require 'peek/adapters/base'
+require 'elasticsearch'
+
+module Peek
+ module Adapters
+ class Elasticsearch < Base
+ def initialize(options = {})
+ @client = options.fetch(:client, ::Elasticsearch::Client.new)
+ @expires_in = Integer(options.fetch(:expires_in, 60 * 30) * 1000)
+ end
+
+ def get(request_id)
+ begin
+ result = @client.get_source index: 'peek_requests_index', type: 'peek_request', id: "#{request_id}"
@dewski
peek member
dewski added a line comment Feb 11, 2014

Ideally the index and type can be configurable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@dewski
peek member

Thanks for submitting this pull request! I left a few feedback comments just on style and configuration, once they're address I can merge this.

@salimane

@dewski feedback addressed :)

@oleander

Any plan on merging this?

@dewski dewski merged commit 68a1ca5 into peek:master May 12, 2014
@salimane salimane deleted the salimane:pr_es_adapter branch May 12, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment