nazrin 2.5.0 → 2.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3765528fe1c94e124288e3af3b4015cd929a9a93
4
- data.tar.gz: d145c6568fd01065ab0d6a78a2b48bf601e5ea42
3
+ metadata.gz: b4ce63b4f44d461d41771edbfecbe562fb5a6ee1
4
+ data.tar.gz: fd395ff1f407073c9e1d459a894abd1dae15e000
5
5
  SHA512:
6
- metadata.gz: 5c9673e792b0b9011084daf9d407a4de9f98fe1eb2f3aadd69f19b850c5656d53d73ad806ec2f53134380098f03ca2c2e107fa505b3c77ac858c29f85a996e4c
7
- data.tar.gz: b37bda7e9dc6f3e25d165dfcd2fae70380c06055b7b5a5f989487c1d732e7b9536bbd064ebfd7fa828bd83ebaec6083dfa9637bbc08cc27f3dd4d6d09947a40a
6
+ metadata.gz: 0dd0eda86c2138afac23b2f0696398a630aed40d85cc0f64122d6bcc4d56b931239cc191087a18a788a0e3892734f660ccebbb379fbd1549a8d68b1d1e8af76a
7
+ data.tar.gz: fce06d4b56e903a0b13bafb19ee78152a020dd82c3ff1db2e900b742807f51612bf6a8134e38e73093ae6bbc2e8b7ecb3ff70a8716e074642a1ecee9fa786145
@@ -1,3 +1,9 @@
1
+ ## [2.6.0](https://github.com/tsuwatch/nazrin/compare/v2.5.0...v2.6.0)
2
+
3
+ ### Features:
4
+
5
+ * Implement batch operation [#27](https://github.com/tsuwatch/nazrin/pull/27) - [@AMHOL](https://github.com/AMHOL)
6
+
1
7
  ## [2.5.0](https://github.com/tsuwatch/nazrin/compare/v2.4.0...v2.5.0)
2
8
 
3
9
  ### Features:
data/README.md CHANGED
@@ -68,6 +68,7 @@ class Post < ActiveRecord::Base
68
68
  end
69
69
  ```
70
70
 
71
+ ### `.search`
71
72
  ```ruby
72
73
  result = Post.search(where: :foo, includes: :bar).size(1).start(0).query("(and 'content')").query_parser('structured').execute
73
74
  => [#<Post id: 1, content: "content">]
@@ -76,6 +77,20 @@ result.facets
76
77
  => {}
77
78
  ```
78
79
 
80
+ ### `.batch_operation`
81
+ ```ruby
82
+ post1 = Post.create
83
+ post2 = Post.create
84
+ post3 = Post.create
85
+ post3.destroy
86
+
87
+ Post.batch_operation(
88
+ add: [post1, post2],
89
+ delete: [post3]
90
+ )
91
+ ```
92
+
93
+
79
94
  ### Supported pagination libraries
80
95
  If you want to use other supported pagination libraries, for example, `nazrin-kaminari` generates `Kaminari::PaginatableArray` instead of `Nazrin::PaginatedArray`.
81
96
 
@@ -1,5 +1,7 @@
1
1
  module Nazrin
2
2
  class DocumentClient
3
+ class InvalidBatchOperationError < StandardError; end
4
+
3
5
  attr_reader :client
4
6
 
5
7
  def initialize(config=Nazrin.config)
@@ -37,5 +39,40 @@ module Nazrin
37
39
  ].to_json,
38
40
  content_type: 'application/json')
39
41
  end
42
+
43
+ def batch(operations)
44
+ ActiveSupport::Deprecation.warn 'config.debug_mode is deprecated. Use config.mode = \'sandbox\' instead.' and return nil if Nazrin.config.debug_mode
45
+ return nil if Nazrin.config.mode == 'sandbox'
46
+
47
+ documents = operations.each_with_object([]) do |(type, tuple), arr|
48
+ case type.to_sym
49
+ when :add
50
+ tuple.each do |id, field_data|
51
+ arr.push(
52
+ type: 'add',
53
+ id: id,
54
+ fields: field_data
55
+ )
56
+ end
57
+ when :delete
58
+ tuple.each do |id|
59
+ arr.push(
60
+ type: 'delete',
61
+ id: id
62
+ )
63
+ end
64
+ else
65
+ raise(
66
+ InvalidBatchOperationError,
67
+ "`#{type}` is not a valid batch operation"
68
+ )
69
+ end
70
+ end
71
+
72
+ client.upload_documents(
73
+ documents: documents.to_json,
74
+ content_type: 'application/json'
75
+ )
76
+ end
40
77
  end
41
78
  end
@@ -2,6 +2,8 @@ require 'active_support/concern'
2
2
 
3
3
  module Nazrin
4
4
  module Searchable
5
+ class InvalidBatchOperationError < StandardError; end
6
+
5
7
  extend ActiveSupport::Concern
6
8
 
7
9
  included do
@@ -27,6 +29,7 @@ module Nazrin
27
29
  class << base
28
30
  alias_method :search, :nazrin_search unless method_defined? :search
29
31
  alias_method :searchable, :nazrin_searchable unless method_defined? :searchable
32
+ alias_method :batch_operation, :nazrin_batch_operation unless method_defined? :batch_operation
30
33
  alias_method :fields, :nazrin_fields unless method_defined? :fields
31
34
  alias_method :field, :nazrin_field unless method_defined? :field
32
35
  alias_method :searchable_configure, :nazrin_searchable_configure unless method_defined? :searchable_configure
@@ -47,6 +50,28 @@ module Nazrin
47
50
  block.call
48
51
  end
49
52
 
53
+ def nazrin_batch_operation(type_objects_mapping)
54
+ operations = type_objects_mapping.each_with_object({}) do |(type, objects), hash|
55
+ case type.to_sym
56
+ when :add
57
+ hash[:add] = objects.map do |obj|
58
+ [obj.send(:id), nazrin_eval_field_data(obj)]
59
+ end
60
+ when :delete
61
+ hash[:delete] = objects.map do |obj|
62
+ obj.send(:id)
63
+ end
64
+ else
65
+ raise(
66
+ InvalidBatchOperationError,
67
+ "`#{type}` is not a valid batch operation"
68
+ )
69
+ end
70
+ end
71
+
72
+ nazrin_doc_client.batch(operations)
73
+ end
74
+
50
75
  def nazrin_fields(fields)
51
76
  field_data = class_variable_get(:@@nazrin_search_field_data)
52
77
  fields.each do |field|
@@ -1,3 +1,3 @@
1
1
  module Nazrin
2
- VERSION = '2.5.0'
2
+ VERSION = '2.6.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nazrin
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.5.0
4
+ version: 2.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tomohiro Suwa
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-01-31 00:00:00.000000000 Z
11
+ date: 2018-02-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: aws-sdk-core