nazrin 2.4.0 → 2.5.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: f98b0576f7417a5acab58c60adf492414ce08a2e
4
- data.tar.gz: c745e6f9830896c0fdf08206e83fa9b9ce7a3e04
3
+ metadata.gz: 3765528fe1c94e124288e3af3b4015cd929a9a93
4
+ data.tar.gz: d145c6568fd01065ab0d6a78a2b48bf601e5ea42
5
5
  SHA512:
6
- metadata.gz: 68875e9dde8ec03648a17add6d131b7ca2093150ddade67cfe6c246f30154d9eb17ee558ae5629b0dc629d89d959f270921b0e2eda9c51692d19322626a4cdeb
7
- data.tar.gz: e07771ba1ed25375d58b7692026472bc1fd62c4a9a3b72a6b2293150dbc2938635df145b0d1d64971f6eda9f91b65fe36dc3d097549b0ea90a9fc9e352b20eef
6
+ metadata.gz: 5c9673e792b0b9011084daf9d407a4de9f98fe1eb2f3aadd69f19b850c5656d53d73ad806ec2f53134380098f03ca2c2e107fa505b3c77ac858c29f85a996e4c
7
+ data.tar.gz: b37bda7e9dc6f3e25d165dfcd2fae70380c06055b7b5a5f989487c1d732e7b9536bbd064ebfd7fa828bd83ebaec6083dfa9637bbc08cc27f3dd4d6d09947a40a
@@ -1,3 +1,11 @@
1
+ ## [2.5.0](https://github.com/tsuwatch/nazrin/compare/v2.4.0...v2.5.0)
2
+
3
+ ### Features:
4
+
5
+ * CloudSearch data also available [#25](https://github.com/tsuwatch/nazrin/pull/25) - [@AMHOL](https://github.com/AMHOL)
6
+
7
+ * Enable accessing facets [#26](https://github.com/tsuwatch/nazrin/pull/26) - [@AMHOL](https://github.com/AMHOL)
8
+
1
9
  ## [2.4.0](https://github.com/tsuwatch/nazrin/compare/v2.3.0...v2.4.0)
2
10
 
3
11
  ### Features:
data/README.md CHANGED
@@ -51,6 +51,10 @@ class Post < ActiveRecord::Base
51
51
  searchable_configure do |config|
52
52
  config.search_endpoint = 'http://example.com/override-search-endpoint'
53
53
  config.document_endpoint = 'http://example.com/override-document-endpoint'
54
+
55
+ # If you set domain_name, CloudSearch data using index_fields configured for the search domain is loaded, not a database.
56
+ # So you can use nazrin for plain object
57
+ config.domain_name = 'my-cloudsearch-domain-name'
54
58
  end
55
59
 
56
60
  searchable do
@@ -65,8 +69,11 @@ end
65
69
  ```
66
70
 
67
71
  ```ruby
68
- Post.search(where: :foo, includes: :bar).size(1).start(0).query("(and 'content')").query_parser('structured').execute
72
+ result = Post.search(where: :foo, includes: :bar).size(1).start(0).query("(and 'content')").query_parser('structured').execute
69
73
  => [#<Post id: 1, content: "content">]
74
+ # You can access facets
75
+ result.facets
76
+ => {}
70
77
  ```
71
78
 
72
79
  ### Supported pagination libraries
@@ -1,3 +1,5 @@
1
+ require 'nazrin/result'
2
+
1
3
  module Nazrin
2
4
  class DataAccessor
3
5
  class NoAccessorError < StandardError; end
@@ -26,7 +28,11 @@ module Nazrin
26
28
 
27
29
  def accessor_for(clazz)
28
30
  return nil if clazz.name.nil? || clazz.name.empty?
29
- if defined?(::ActiveRecord::Base) && clazz.ancestors.include?(::ActiveRecord::Base)
31
+
32
+ if clazz.respond_to?(:nazrin_searchable_config) && clazz.nazrin_searchable_config.domain_name
33
+ require 'nazrin/data_accessor/struct'
34
+ return Nazrin::DataAccessor::Struct[clazz.nazrin_searchable_config]
35
+ elsif defined?(::ActiveRecord::Base) && clazz.ancestors.include?(::ActiveRecord::Base)
30
36
  require 'nazrin/data_accessor/active_record'
31
37
  return Nazrin::DataAccessor::ActiveRecord
32
38
  elsif defined?(::Mongoid::Document) && clazz.ancestors.include?(::Mongoid::Document)
@@ -45,43 +51,55 @@ module Nazrin
45
51
  end
46
52
  end
47
53
 
54
+ attr_reader :model
55
+ attr_reader :options
56
+
48
57
  def initialize(model, options)
49
58
  @model = model
50
59
  @options = options
51
60
  end
52
61
 
53
62
  def results(client)
54
- @client = client
55
-
56
- res = @client.search
57
- collection = load_all(res.data.hits.hit.map(&:id))
63
+ res = client.search
64
+ collection = load_all(data_from_response(res))
65
+ start = client.parameters[:start]
66
+ size = client.parameters[:size]
58
67
 
59
- if @client.parameters[:size] && @client.parameters[:start]
68
+ if size && start
60
69
  total_count = res.data.hits.found
61
70
 
62
- Nazrin::PaginationGenerator.generate(
71
+ records = Nazrin::PaginationGenerator.generate(
63
72
  collection,
64
- current_page: current_page,
65
- per_page: @client.parameters[:size],
73
+ current_page: current_page(start, size),
74
+ per_page: size,
66
75
  total_count: total_count,
67
- last_page: last_page(total_count))
76
+ last_page: last_page(size, total_count))
68
77
  else
69
- collection
78
+ records = collection
70
79
  end
80
+
81
+ Result.new(
82
+ records,
83
+ res.facets
84
+ )
71
85
  end
72
86
 
73
87
  def load_all
74
88
  raise NotImplementedError
75
89
  end
76
90
 
91
+ def data_from_response
92
+ raise NotImplementedError
93
+ end
94
+
77
95
  private
78
96
 
79
- def last_page(total_count)
80
- (total_count / @client.parameters[:size].to_f).ceil
97
+ def last_page(size, total_count)
98
+ (total_count / size.to_f).ceil
81
99
  end
82
100
 
83
- def current_page
84
- (@client.parameters[:start] / @client.parameters[:size].to_f).ceil + 1
101
+ def current_page(start, size)
102
+ (start / size.to_f).ceil + 1
85
103
  end
86
104
  end
87
105
  end
@@ -4,16 +4,20 @@ module Nazrin
4
4
  # load from activerecord
5
5
  def load_all(ids)
6
6
  records_table = {}
7
- @options.each do |k, v|
8
- @model = @model.send(k, v)
7
+ options.each do |k, v|
8
+ model = model.send(k, v)
9
9
  end
10
- @model.where(id: ids).each do |record|
10
+ model.where(id: ids).each do |record|
11
11
  records_table[record.id] = record
12
12
  end
13
13
  ids.map do |id|
14
14
  records_table.select { |k, _| k == id.to_i }[id.to_i]
15
15
  end.reject(&:nil?)
16
16
  end
17
+
18
+ def data_from_response(res)
19
+ res.data.hits.hit.map(&:id)
20
+ end
17
21
  end
18
22
  end
19
23
  end
@@ -3,20 +3,24 @@ module Nazrin
3
3
  class Mongoid < Nazrin::DataAccessor
4
4
  def load_all(ids)
5
5
  documents_table = {}
6
- @options.each do |k, v|
7
- @model = if v.nil?
8
- @model.send(k)
6
+ options.each do |k, v|
7
+ model = if v.nil?
8
+ model.send(k)
9
9
  else
10
- @model.send(k, v)
10
+ model.send(k, v)
11
11
  end
12
12
  end
13
- @model.where('_id' => { '$in' => ids }).each do |document|
13
+ model.where('_id' => { '$in' => ids }).each do |document|
14
14
  documents_table[document._id.to_s] = document
15
15
  end
16
16
  ids.map do |id|
17
17
  documents_table[id]
18
18
  end.reject(&:nil?)
19
19
  end
20
+
21
+ def data_from_response(res)
22
+ res.data.hits.hit.map(&:id)
23
+ end
20
24
  end
21
25
  end
22
26
  end
@@ -0,0 +1,68 @@
1
+ require 'aws-sdk-cloudsearch'
2
+
3
+ module Nazrin
4
+ class DataAccessor
5
+ class Struct < Nazrin::DataAccessor
6
+ class MissingDomainNameConfigError < StandardError; end
7
+
8
+ class << self
9
+ attr_reader :config
10
+
11
+ def [](config)
12
+ Class.new(self).tap do |clazz|
13
+ clazz.instance_variable_set(:@config, config)
14
+ end
15
+ end
16
+
17
+ def transform_attributes(attributes)
18
+ attributes.each_with_object({}) do |(name, value), hash|
19
+ type = field_types[name]
20
+
21
+ if type.end_with?('array')
22
+ hash[name] = value
23
+ else
24
+ hash[name] = value.first
25
+ end
26
+ end
27
+ end
28
+
29
+ def field_types
30
+ return @field_types if defined?(@field_types)
31
+
32
+ response = cloudsearch_client.describe_index_fields(
33
+ domain_name: config.domain_name
34
+ )
35
+
36
+ @field_types = response.index_fields.each_with_object({}) do |field, fields|
37
+ name = field.options[:index_field_name]
38
+ type = field.options[:index_field_type]
39
+
40
+ fields[name] = type
41
+ end
42
+ end
43
+
44
+ private
45
+
46
+ def cloudsearch_client
47
+ @cloudsearch_client ||= Aws::CloudSearch::Client.new(
48
+ access_key_id: config.access_key_id,
49
+ secret_access_key: config.secret_access_key,
50
+ logger: config.logger
51
+ )
52
+ end
53
+ end
54
+
55
+ def load_all(data)
56
+ data.map do |attributes|
57
+ model.new(attributes)
58
+ end
59
+ end
60
+
61
+ def data_from_response(res)
62
+ res.data[:hits][:hit].map do |hit|
63
+ self.class.transform_attributes(hit[:fields])
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,10 @@
1
+ module Nazrin
2
+ class Result < SimpleDelegator
3
+ attr_reader :facets
4
+
5
+ def initialize(result, facets)
6
+ super(result)
7
+ @facets = facets
8
+ end
9
+ end
10
+ end
@@ -1,6 +1,8 @@
1
1
  module Nazrin
2
2
  module Searchable
3
3
  class Configuration
4
+ attr_accessor :domain_name
5
+
4
6
  %i(
5
7
  search_endpoint
6
8
  document_endpoint
@@ -1,3 +1,3 @@
1
1
  module Nazrin
2
- VERSION = '2.4.0'
2
+ VERSION = '2.5.0'
3
3
  end
@@ -18,6 +18,7 @@ Gem::Specification.new do |spec|
18
18
  spec.require_paths = ['lib']
19
19
 
20
20
  spec.add_dependency 'aws-sdk-core', '~> 3'
21
+ spec.add_dependency 'aws-sdk-cloudsearch', '~> 1.0'
21
22
  spec.add_dependency 'aws-sdk-cloudsearchdomain', '~> 1.0'
22
23
  spec.add_dependency 'activesupport', '>= 4.0.0'
23
24
 
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.4.0
4
+ version: 2.5.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-24 00:00:00.000000000 Z
11
+ date: 2018-01-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: aws-sdk-core
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: aws-sdk-cloudsearch
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: aws-sdk-cloudsearchdomain
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -202,9 +216,11 @@ files:
202
216
  - lib/nazrin/data_accessor.rb
203
217
  - lib/nazrin/data_accessor/active_record.rb
204
218
  - lib/nazrin/data_accessor/mongoid.rb
219
+ - lib/nazrin/data_accessor/struct.rb
205
220
  - lib/nazrin/document_client.rb
206
221
  - lib/nazrin/paginated_array.rb
207
222
  - lib/nazrin/pagination_generator.rb
223
+ - lib/nazrin/result.rb
208
224
  - lib/nazrin/search_client.rb
209
225
  - lib/nazrin/searchable.rb
210
226
  - lib/nazrin/searchable/config.rb