Skip to content
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

Add batch operations #27

Merged
merged 4 commits into from
Feb 7, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 29 additions & 0 deletions lib/nazrin/document_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,34 @@ def delete_document(id)
].to_json,
content_type: 'application/json')
end

def batch(operations)
ActiveSupport::Deprecation.warn 'config.debug_mode is deprecated. Use config.mode = \'sandbox\' instead.' and return nil if Nazrin.config.debug_mode
return nil if Nazrin.config.mode == 'sandbox'

documents = operations.each_with_object([]) do |(type, tuple), arr|
if type.to_sym == :add
tuple.each do |id, field_data|
arr.push(
type: 'add',
id: id,
fields: field_data
)
end
else
tuple.map do |id|
Copy link
Contributor Author

@AMHOL AMHOL Feb 7, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gonna change this to each rather than map as map implies that the return value matters.

arr.push(
type: 'delete',
id: id
)
end
end
end

client.upload_documents(
documents: documents.to_json,
content_type: 'application/json'
)
end
end
end
16 changes: 16 additions & 0 deletions lib/nazrin/searchable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,22 @@ def nazrin_add_document(obj)
obj.send(:id), nazrin_eval_field_data(obj))
end

def nazrin_batch_operation(type_objects_mapping)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

operations = type_objects_mapping.each_with_object({}) do |(type, objects), hash|
if type.to_sym == :add
hash[:add] = objects.map do |obj|
[obj.send(:id), nazrin_eval_field_data(obj)]
end
else
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like you to specify delete for typo

hash[:delete] = objects.map do |obj|
obj.send(:id)
end
end
end

nazrin_doc_client.batch(operations)
end

def nazrin_update_document(obj)
nazrin_add_document(obj)
end
Expand Down
59 changes: 59 additions & 0 deletions spec/nazrin/active_record/searchable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,65 @@
it { expect(post).to be_respond_to :update_in_index }
it { expect(post).to be_respond_to :delete_from_index }

describe '.nazrin_batch_operation' do
let!(:other_post) { Post.create(content: 'other', created_at: Time.now) }
let!(:deleted_post) { Post.create(content: 'deleted', created_at: Time.now) }
let(:domain_client) do
instance_double(Aws::CloudSearchDomain::Client)
end

before do
allow(Aws::CloudSearchDomain::Client).to receive(:new)
.and_return(domain_client)
allow(domain_client).to receive(:upload_documents)

# Hacky method to reset CS domain client
Post.class_eval do
searchable do
fields [:content]
field(:created_at) { created_at.utc.iso8601 }
end
end
end

subject do
Post.nazrin_batch_operation(
add: [post, other_post],
delete: [deleted_post]
)
end

it do
expect(domain_client).to receive(:upload_documents).with(
documents: [
{
type: 'add',
id: post.id,
fields: {
content: post.content,
created_at: post.created_at.utc.iso8601
}
},
{
type: 'add',
id: other_post.id,
fields: {
content: other_post.content,
created_at: other_post.created_at.utc.iso8601
}
},
{
type: 'delete',
id: deleted_post.id
}
].to_json,
content_type: 'application/json'
)

subject
end
end

describe '#search' do
let(:response) { FakeResponse.new }
before { allow_any_instance_of(Nazrin::SearchClient).to receive(:search).and_return(response) }
Expand Down
6 changes: 3 additions & 3 deletions spec/nazrin/nazrin_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
it { expect(config.mode).to eq 'sandbox' }
it { expect(config.search_endpoint).to eq 'http://search.com' }
it { expect(config.document_endpoint).to eq 'http://document.com' }
it { expect(config.region).to eq :region }
it { expect(config.access_key_id).to eq :access_key_id }
it { expect(config.secret_access_key).to eq :secret_access_key }
it { expect(config.region).to eq 'region' }
it { expect(config.access_key_id).to eq 'access_key_id' }
it { expect(config.secret_access_key).to eq 'secret_access_key' }
it { expect(config.logger).to be nil }
end
end
79 changes: 79 additions & 0 deletions spec/nazrin/struct/searchable_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'spec_helper'

describe Nazrin::Searchable do
let(:clazz) do
Class.new do
Expand All @@ -17,6 +19,83 @@ def initialize(attributes)
end
let(:data_accessor) { Nazrin::DataAccessor.for(clazz) }

describe '.nazrin_batch_operation' do
let(:clazz) do
Class.new(super()) do
%i(id content created_at).each do |attr|
define_method(attr) { attributes[attr] }
end
end
end
let(:added_struct_1) do
clazz.new(id: 1, content: 'added_1', created_at: Time.now)
end
let(:added_struct_2) do
clazz.new(id: 2, content: 'added_2', created_at: Time.now)
end
let(:deleted_struct_1) do
clazz.new(id: 3, content: 'deleted_1', created_at: Time.now)
end
let(:domain_client) do
instance_double(Aws::CloudSearchDomain::Client)
end

before do
allow(data_accessor).to receive(:field_types).and_return(
'id' => 'int',
'content' => 'text',
'created_at' => 'date'
)
allow(Aws::CloudSearchDomain::Client).to receive(:new)
.and_return(domain_client)
allow(domain_client).to receive(:upload_documents)
# Hacky method to reset CS domain client
clazz.class_eval do
searchable do
fields [:content]
field(:created_at) { created_at.utc.iso8601 }
end
end
end

subject do
clazz.nazrin_batch_operation(
add: [added_struct_1, added_struct_2],
delete: [deleted_struct_1]
)
end

it do
expect(domain_client).to receive(:upload_documents).with(
documents: [
{
type: 'add',
id: added_struct_1.id,
fields: {
content: added_struct_1.content,
created_at: added_struct_1.created_at.utc.iso8601
}
},
{
type: 'add',
id: added_struct_2.id,
fields: {
content: added_struct_2.content,
created_at: added_struct_2.created_at.utc.iso8601
}
},
{
type: 'delete',
id: deleted_struct_1.id
}
].to_json,
content_type: 'application/json'
)

subject
end
end

describe '#search' do
let(:result) do
clazz.search.query_parser('structured').query('matchall').execute
Expand Down
6 changes: 3 additions & 3 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
config.mode = 'production'
config.search_endpoint = 'http://search.com'
config.document_endpoint = 'http://document.com'
config.region = :region
config.access_key_id = :access_key_id
config.secret_access_key = :secret_access_key
config.region = 'region'
config.access_key_id = 'access_key_id'
config.secret_access_key = 'secret_access_key'
end

class FakeResponse
Expand Down