Skip to content

Fixes Indexer README to work with ES 7+ #968

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 13 additions & 16 deletions elasticsearch-model/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -506,8 +506,8 @@ with a tool like [_Resque_](https://github.com/resque/resque) or [_Sidekiq_](htt
class Article
include Elasticsearch::Model

after_save { Indexer.perform_async(:index, self.id) }
after_destroy { Indexer.perform_async(:delete, self.id) }
after_save { Indexer.perform_async(:index, self.class.to_s self.id) }
after_destroy { Indexer.perform_async(:delete, self.class.to_s, self.id) }
end
```

Expand All @@ -521,20 +521,17 @@ class Indexer
Logger = Sidekiq.logger.level == Logger::DEBUG ? Sidekiq.logger : nil
Client = Elasticsearch::Client.new host: 'localhost:9200', logger: Logger

def perform(operation, record_id)
logger.debug [operation, "ID: #{record_id}"]
def perform(operation, klass, record_id, options={})
logger.debug [operation, "#{klass}##{record_id} #{options.inspect}"]

case operation.to_s
when /index/
record = Article.find(record_id)
Client.index index: 'articles', type: 'article', id: record.id, body: record.__elasticsearch__.as_indexed_json
when /delete/
begin
Client.delete index: 'articles', type: 'article', id: record_id
rescue Elasticsearch::Transport::Transport::Errors::NotFound
logger.debug "Article not found, ID: #{record_id}"
end
else raise ArgumentError, "Unknown operation '#{operation}'"
when /index|update/
record = klass.constantize.find(record_id)
record.__elasticsearch__.client = Client
record.__elasticsearch__.__send__ "#{operation}_document"
when /delete/
Client.delete index: klass.constantize.index_name, type: klass.constantize.document_type, id: record_id
else raise ArgumentError, "Unknown operation '#{operation}'"
end
end
end
Expand All @@ -550,10 +547,10 @@ Article.first.update_attribute :title, 'Updated'
You'll see the job being processed in the console where you started the _Sidekiq_ worker:

```
Indexer JID-eb7e2daf389a1e5e83697128 DEBUG: ["index", "ID: 7"]
Indexer JID-eb7e2daf389a1e5e83697128 DEBUG: [:index, "Article#1 {}"]
Indexer JID-eb7e2daf389a1e5e83697128 INFO: PUT http://localhost:9200/articles/article/1 [status:200, request:0.004s, query:n/a]
Indexer JID-eb7e2daf389a1e5e83697128 DEBUG: > {"id":1,"title":"Updated", ...}
Indexer JID-eb7e2daf389a1e5e83697128 DEBUG: < {"ok":true,"_index":"articles","_type":"article","_id":"1","_version":6}
Indexer JID-eb7e2daf389a1e5e83697128 DEBUG: < {"ok":true,"_index":"articles","_type":"_doc","_id":"1","_version":6}
Indexer JID-eb7e2daf389a1e5e83697128 INFO: done: 0.006 sec
```

Expand Down