stretchy-model 0.5.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. checksums.yaml +4 -4
  2. data/lib/rails/instrumentation/railtie.rb +2 -0
  3. data/lib/stretchy/associations.rb +1 -1
  4. data/lib/stretchy/attributes/type/array.rb +16 -11
  5. data/lib/stretchy/attributes/type/base.rb +42 -0
  6. data/lib/stretchy/attributes/type/binary.rb +45 -0
  7. data/lib/stretchy/attributes/type/boolean.rb +48 -0
  8. data/lib/stretchy/attributes/type/completion.rb +25 -0
  9. data/lib/stretchy/attributes/type/constant_keyword.rb +38 -0
  10. data/lib/stretchy/attributes/type/date_time.rb +35 -0
  11. data/lib/stretchy/attributes/type/dense_vector.rb +59 -0
  12. data/lib/stretchy/attributes/type/flattened.rb +31 -0
  13. data/lib/stretchy/attributes/type/geo_point.rb +27 -0
  14. data/lib/stretchy/attributes/type/geo_shape.rb +27 -0
  15. data/lib/stretchy/attributes/type/hash.rb +36 -13
  16. data/lib/stretchy/attributes/type/histogram.rb +7 -0
  17. data/lib/stretchy/attributes/type/ip.rb +29 -0
  18. data/lib/stretchy/attributes/type/join.rb +22 -0
  19. data/lib/stretchy/attributes/type/keyword.rb +36 -10
  20. data/lib/stretchy/attributes/type/match_only_text.rb +8 -0
  21. data/lib/stretchy/attributes/type/nested.rb +25 -0
  22. data/lib/stretchy/attributes/type/numeric/base.rb +32 -0
  23. data/lib/stretchy/attributes/type/numeric/byte.rb +7 -0
  24. data/lib/stretchy/attributes/type/numeric/double.rb +7 -0
  25. data/lib/stretchy/attributes/type/numeric/float.rb +7 -0
  26. data/lib/stretchy/attributes/type/numeric/half_float.rb +7 -0
  27. data/lib/stretchy/attributes/type/numeric/integer.rb +7 -0
  28. data/lib/stretchy/attributes/type/numeric/long.rb +7 -0
  29. data/lib/stretchy/attributes/type/numeric/scaled_float.rb +23 -0
  30. data/lib/stretchy/attributes/type/numeric/short.rb +7 -0
  31. data/lib/stretchy/attributes/type/numeric/unsigned_long.rb +7 -0
  32. data/lib/stretchy/attributes/type/percolator.rb +23 -0
  33. data/lib/stretchy/attributes/type/point.rb +24 -0
  34. data/lib/stretchy/attributes/type/range/base.rb +9 -0
  35. data/lib/stretchy/attributes/type/range/date_range.rb +17 -0
  36. data/lib/stretchy/attributes/type/range/double_range.rb +17 -0
  37. data/lib/stretchy/attributes/type/range/float_range.rb +16 -0
  38. data/lib/stretchy/attributes/type/range/integer_range.rb +16 -0
  39. data/lib/stretchy/attributes/type/range/ip_range.rb +16 -0
  40. data/lib/stretchy/attributes/type/range/long_range.rb +16 -0
  41. data/lib/stretchy/attributes/type/rank_feature.rb +21 -0
  42. data/lib/stretchy/attributes/type/rank_features.rb +24 -0
  43. data/lib/stretchy/attributes/type/search_as_you_type.rb +30 -0
  44. data/lib/stretchy/attributes/type/shape.rb +24 -0
  45. data/lib/stretchy/attributes/type/sparse_vector.rb +37 -0
  46. data/lib/stretchy/attributes/type/string.rb +7 -0
  47. data/lib/stretchy/attributes/type/text.rb +50 -11
  48. data/lib/stretchy/attributes/type/token_count.rb +26 -0
  49. data/lib/stretchy/attributes/type/version.rb +21 -0
  50. data/lib/stretchy/attributes/type/wildcard.rb +35 -0
  51. data/lib/stretchy/attributes.rb +46 -0
  52. data/lib/stretchy/common.rb +3 -2
  53. data/lib/stretchy/delegation/gateway_delegation.rb +2 -4
  54. data/lib/stretchy/querying.rb +3 -3
  55. data/lib/stretchy/record.rb +1 -1
  56. data/lib/stretchy/relation.rb +1 -1
  57. data/lib/stretchy/relations/finder_methods.rb +19 -2
  58. data/lib/stretchy/relations/query_builder.rb +7 -1
  59. data/lib/stretchy/scoping/named.rb +1 -1
  60. data/lib/stretchy/version.rb +1 -1
  61. data/lib/stretchy.rb +5 -1
  62. metadata +45 -2
@@ -15,8 +15,9 @@ module Stretchy
15
15
  @default_sort_key
16
16
  end
17
17
 
18
- def default_size(size = 10000)
19
- @default_size = size
18
+ def default_size(size = nil)
19
+ @default_size = size unless size.nil?
20
+ @default_size
20
21
  end
21
22
 
22
23
  private
@@ -18,8 +18,6 @@ module Stretchy
18
18
  :count,
19
19
  to: :gateway
20
20
 
21
- include Rails::Instrumentation::Publishers::Record
22
-
23
21
  def index_name(name=nil, &block)
24
22
  if name || block_given?
25
23
  return (@index_name = name || block)
@@ -39,8 +37,8 @@ module Stretchy
39
37
  def gateway(&block)
40
38
  reload_gateway_configuration! if @gateway && @gateway.client != Stretchy.configuration.client
41
39
 
42
- @gateway ||= Stretchy::Repository.create(client: Stretchy.configuration.client, index_name: index_name, klass: base_class)
43
- block.arity < 1 ? @gateway.instance_eval(&block) : block.call(@gateway) if block_given?
40
+ @gateway ||= Stretchy::Repository.create(client: Stretchy.configuration.client, index_name: index_name, klass: base_class, mapping: base_class.attribute_mappings.merge(dynamic: true))
41
+ # block.arity < 1 ? @gateway.instance_eval(&block) : block.call(@gateway) if block_given?
44
42
  @gateway
45
43
  end
46
44
 
@@ -9,10 +9,10 @@ module Stretchy
9
9
  delegate :must, :must_not, :should, :where_not, :where, :filter_query, :query_string, :regexp, to: :all
10
10
 
11
11
  def fetch_results(es)
12
- unless es.count?
13
- base_class.search(es.to_elastic, es.search_options)
14
- else
12
+ if es.count?
15
13
  base_class.count(es.to_elastic, es.search_options)
14
+ else
15
+ base_class.search(es.to_elastic, es.search_options)
16
16
  end
17
17
  end
18
18
 
@@ -30,7 +30,7 @@ module Stretchy
30
30
  extend Stretchy::Querying
31
31
 
32
32
  # Set up common attributes
33
- attribute :id, :string #, default: lambda { SecureRandom.uuid }
33
+ attribute :id, :keyword #, default: lambda { SecureRandom.uuid }
34
34
  attribute :created_at, :datetime, default: lambda { Time.now.utc }
35
35
  attribute :updated_at, :datetime, default: lambda { Time.now.utc }
36
36
 
@@ -28,7 +28,7 @@ module Stretchy
28
28
  # @param values [Hash] The initial values for the relation.
29
29
  def initialize(klass, values={})
30
30
  @klass = klass
31
- @values = values
31
+ @values = values.merge(default_size: klass.default_size)
32
32
  @offsets = {}
33
33
  @loaded = false
34
34
  end
@@ -39,14 +39,31 @@ module Stretchy
39
39
  self
40
40
  end
41
41
 
42
+ # size is not permitted to the count API but queries are.
43
+ #
44
+ # if a query is supplied with `.count` then the user wants a count of the results
45
+ # matching the query
46
+ #
47
+ # however, the default_size is used to limit the number of results returned
48
+ # so we remove the size from the query and then call `.count` API
49
+ #
50
+ # but if the user supplies a limit, then we should assume they want a count of the results
51
+ # which could lead to some confusion
52
+ # suppose the user calls `.size(100).count` and the default_size is 100
53
+ # if we remove size from the query, then the count could be greater than 100
54
+ #
55
+ # I think the best way to handle this is to remove the size from the query only if it was
56
+ # applied by the default_size
57
+ # If the user supplies a limit, then we should assume they want a count of the results
58
+ #
59
+ # if size is called with a limit,
42
60
  def count
43
- return results.count if @loaded
61
+ return results.count if @loaded || @values[:size].present?
44
62
  spawn.count!
45
63
  end
46
64
 
47
65
  def count!
48
66
  @values[:count] = true
49
- @values.delete(:size)
50
67
  spawned = spawn
51
68
  spawned.order_values.clear
52
69
  spawned.results
@@ -2,11 +2,12 @@ module Stretchy
2
2
  module Relations
3
3
  class QueryBuilder
4
4
 
5
- attr_reader :structure, :values, :attribute_types
5
+ attr_reader :structure, :values, :attribute_types, :default_size
6
6
 
7
7
  def initialize(values, attribute_types = nil)
8
8
  @attribute_types = attribute_types
9
9
  @structure = Jbuilder.new ignore_nil: true
10
+ @default_size = values.delete(:default_size)
10
11
  @values = values
11
12
  end
12
13
 
@@ -208,6 +209,11 @@ module Stretchy
208
209
  end
209
210
 
210
211
  def extra_search_options
212
+ unless self.count?
213
+ values[:size] = size.present? ? size : values[:default_size]
214
+ else
215
+ values[:size] = nil
216
+ end
211
217
  [:size].inject(Hash.new) { |h,k| h[k] = self.send(k) unless self.send(k).nil?; h}
212
218
  end
213
219
 
@@ -8,7 +8,7 @@ module Stretchy
8
8
  if current_scope
9
9
  current_scope.clone
10
10
  else
11
- default_scoped.size(default_size)
11
+ default_scoped
12
12
  end
13
13
  end
14
14
 
@@ -1,3 +1,3 @@
1
1
  module Stretchy
2
- VERSION = '0.5.0'
2
+ VERSION = '0.6.0'
3
3
  end
data/lib/stretchy.rb CHANGED
@@ -12,7 +12,6 @@ require_relative "stretchy/version"
12
12
  require_relative "rails/instrumentation/railtie" if defined?(Rails)
13
13
 
14
14
 
15
-
16
15
  module Stretchy
17
16
 
18
17
  def self.loader
@@ -21,6 +20,7 @@ module Stretchy
21
20
  loader.tag = File.basename(__FILE__, ".rb")
22
21
  loader.inflector = Zeitwerk::GemInflector.new(__FILE__)
23
22
  loader.push_dir(__dir__)
23
+ loader.inflector.inflect("ip" => "IP")
24
24
  loader
25
25
  end
26
26
  end
@@ -30,6 +30,10 @@ module Stretchy
30
30
  Stretchy::Attributes.register!
31
31
  end
32
32
 
33
+ def self.instrument!
34
+ Stretchy::Delegation::GatewayDelegation.send(:include, Rails::Instrumentation::Publishers::Record)
35
+ end
36
+
33
37
  module Errors
34
38
  class QueryOptionMissing < StandardError; end
35
39
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stretchy-model
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Spencer Markowski
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-03-14 00:00:00.000000000 Z
11
+ date: 2024-03-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: zeitwerk
@@ -233,9 +233,52 @@ files:
233
233
  - lib/stretchy/attributes.rb
234
234
  - lib/stretchy/attributes/transformers/keyword_transformer.rb
235
235
  - lib/stretchy/attributes/type/array.rb
236
+ - lib/stretchy/attributes/type/base.rb
237
+ - lib/stretchy/attributes/type/binary.rb
238
+ - lib/stretchy/attributes/type/boolean.rb
239
+ - lib/stretchy/attributes/type/completion.rb
240
+ - lib/stretchy/attributes/type/constant_keyword.rb
241
+ - lib/stretchy/attributes/type/date_time.rb
242
+ - lib/stretchy/attributes/type/dense_vector.rb
243
+ - lib/stretchy/attributes/type/flattened.rb
244
+ - lib/stretchy/attributes/type/geo_point.rb
245
+ - lib/stretchy/attributes/type/geo_shape.rb
236
246
  - lib/stretchy/attributes/type/hash.rb
247
+ - lib/stretchy/attributes/type/histogram.rb
248
+ - lib/stretchy/attributes/type/ip.rb
249
+ - lib/stretchy/attributes/type/join.rb
237
250
  - lib/stretchy/attributes/type/keyword.rb
251
+ - lib/stretchy/attributes/type/match_only_text.rb
252
+ - lib/stretchy/attributes/type/nested.rb
253
+ - lib/stretchy/attributes/type/numeric/base.rb
254
+ - lib/stretchy/attributes/type/numeric/byte.rb
255
+ - lib/stretchy/attributes/type/numeric/double.rb
256
+ - lib/stretchy/attributes/type/numeric/float.rb
257
+ - lib/stretchy/attributes/type/numeric/half_float.rb
258
+ - lib/stretchy/attributes/type/numeric/integer.rb
259
+ - lib/stretchy/attributes/type/numeric/long.rb
260
+ - lib/stretchy/attributes/type/numeric/scaled_float.rb
261
+ - lib/stretchy/attributes/type/numeric/short.rb
262
+ - lib/stretchy/attributes/type/numeric/unsigned_long.rb
263
+ - lib/stretchy/attributes/type/percolator.rb
264
+ - lib/stretchy/attributes/type/point.rb
265
+ - lib/stretchy/attributes/type/range/base.rb
266
+ - lib/stretchy/attributes/type/range/date_range.rb
267
+ - lib/stretchy/attributes/type/range/double_range.rb
268
+ - lib/stretchy/attributes/type/range/float_range.rb
269
+ - lib/stretchy/attributes/type/range/integer_range.rb
270
+ - lib/stretchy/attributes/type/range/ip_range.rb
271
+ - lib/stretchy/attributes/type/range/long_range.rb
272
+ - lib/stretchy/attributes/type/rank_feature.rb
273
+ - lib/stretchy/attributes/type/rank_features.rb
274
+ - lib/stretchy/attributes/type/search_as_you_type.rb
275
+ - lib/stretchy/attributes/type/shape.rb
276
+ - lib/stretchy/attributes/type/sparse_vector.rb
277
+ - lib/stretchy/attributes/type/string.rb
238
278
  - lib/stretchy/attributes/type/text.rb
279
+ - lib/stretchy/attributes/type/token_count.rb
280
+ - lib/stretchy/attributes/type/version.rb
281
+ - lib/stretchy/attributes/type/wildcard.rb
239
282
  - lib/stretchy/common.rb
240
283
  - lib/stretchy/delegation/delegate_cache.rb
241
284
  - lib/stretchy/delegation/gateway_delegation.rb