spree-typesense 6.0.2 → 6.0.3

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7cc00fcb3cec734dd1367767c407fd83adbabcd12850f17206d901f0d32c0530
4
- data.tar.gz: 2b7388e5bf38d8329d247508e7cf5cc84f3550b0fa63c62fb1ae1e0be067f8d2
3
+ metadata.gz: ff73cc28721ba603c3103e8ff91733c18b8d87229402290e109b80721ee0030a
4
+ data.tar.gz: f01f8292daaf440bc4db9e973b8b7622a34daf86a05c5e4e986892adf4a77e10
5
5
  SHA512:
6
- metadata.gz: 9fcfc9dcd95e5ccf319fcd2d6f170cef8517b49c5bbb80d9c2227046c76321fdedc61bccd90a3f8ab76926ad6bae71d5780b2b7abd84bb2c28cf6e5acdea40f9
7
- data.tar.gz: 29391d450238a1cad7459a612a3209fe42634c13a07381686156de1dcb71054bbb15f675e896b96fea3f40c469b4815e8177a62cb86ad27d751676915c79efd4
6
+ metadata.gz: d0fd5296f78ae0872fedeb7e3e3d263ee6890285ec0473c4fd96c914586932ae8f1d476357e4c8dc420fdc7b91f73daa0076f1c918590e8edbc9657b9e4c094f
7
+ data.tar.gz: 9b2f8c91432a3c891a12ee48171aba7ad2cb6a6c82574e93b63e5937f16a3ac30267e885084d93c5e16d282957c931ba2ff599080a5413a9b8c7fd1044f792e2
data/CHANGELOG.md CHANGED
@@ -1,5 +1,19 @@
1
1
  # Changelog
2
2
 
3
+ ## 6.0.3 (2026-11-20)
4
+
5
+ - **A listing is never wider than its filters.** A filter the index has no
6
+ translation for was ignored, so the listing and its facets covered more
7
+ products than asked for. This applied to `search_by_name`, `name_cont` or
8
+ another Ransack predicate, and to `cf_*`. The same happened with a
9
+ malformed id or price, or with option values none of which exists. Spree's
10
+ `Database` provider now answers such a request, applying every filter. It
11
+ is logged at info, not reported, and the circuit breaker is left alone.
12
+ - **`price_between` and `in_taxon` are applied by the index**: a price range
13
+ with both bounds included, and the older name for `in_category`.
14
+ - **`in_stock` and `out_of_stock` set to `false`, `f` or `off`** filter nothing,
15
+ as Ransack reads them, rather than filtering by stock.
16
+
3
17
  ## 6.0.2 (2026-11-20)
4
18
 
5
19
  - **Option value counts count by variant** (#19). With option values
data/README.md CHANGED
@@ -129,14 +129,22 @@ and with it the documents of products that were deleted or left the scope.
129
129
  product `scope`, such as Spree 6's buyer catalogs and audiences, is not:
130
130
  the index cannot express it. Spree's Meilisearch provider has the same
131
131
  limit. Search results are always intersected with the scope.
132
- - **Some filters and sorts are ignored.** The index answers `price_gte`,
133
- `price_lte`, `in_stock`, `out_of_stock`, `in_category`, `in_categories`,
134
- `in_collection` (Spree 6) and `with_option_value_ids`. It ignores
135
- `price_between`, `in_taxon`, `search_by_name`, other ransack predicates such
136
- as `name_cont`, and `cf_*` custom-field filters. It sorts by `price`,
137
- `-price`, `name`, `-name`, `available_on`, `-available_on` and
138
- `best_selling`; any other sort, such as `created_at` or a `cf_*` sort, gets
139
- the default ranking. `cf_*` sorts are not offered.
132
+ - **Other filters are answered by the `Database` provider.** The index
133
+ applies `price_gte`, `price_lte`, `price_between`, `in_stock`,
134
+ `out_of_stock`, `in_category`, `in_taxon`, `in_categories`, `in_collection`
135
+ and `with_option_value_ids`. A request carrying any other filter with a
136
+ value (`search_by_name`, `name_cont` or another Ransack predicate, a `cf_*`
137
+ custom-field filter), a malformed id or price in one of those, or only
138
+ option values that don't exist, is answered by Spree's `Database`
139
+ provider. That provider applies them all, so a listing is never wider than
140
+ asked for. It is logged at info, not reported, and leaves the circuit
141
+ breaker alone. `search_by_name` is not translated: it matches part of the
142
+ name, which Typesense's word matching does not, and a search has only one
143
+ text query.
144
+ - **Some sorts are ignored.** The index sorts by `price`, `-price`, `name`,
145
+ `-name`, `available_on`, `-available_on` and `best_selling`; any other
146
+ sort, such as `created_at` or a `cf_*` sort, gets the default ranking.
147
+ `cf_*` sorts are not offered.
140
148
  - **Option facet counts with a selection take one more request.** With option
141
149
  values selected, each value's count is the number of products choosing it
142
150
  would list: those with a variant carrying it together with the other types'
@@ -53,31 +53,62 @@ module Spree
53
53
  conditions
54
54
  end
55
55
 
56
+ # Returned by #condition_for for a filter the index answers by adding
57
+ # nothing: the text query, which arrives as `query`; not_discontinued,
58
+ # which the system conditions already apply; a stock flag set false;
59
+ # option values, which #option_conditions turns into conditions.
60
+ NO_CONDITION = :none
61
+
56
62
  # SECURITY: values here come from Store API query params. Numbers are
57
- # coerced with to_f; ids are validated AND stripped; anything invalid is
58
- # DROPPED, never repaired. Matches meilisearch.rb#build_filter_condition.
63
+ # parsed, never coerced; ids are validated AND stripped; anything
64
+ # invalid adds no condition, and #unanswered_filters names it, so the
65
+ # Database provider answers instead.
59
66
  def user_conditions(filters)
60
- f = filters.respond_to?(:to_unsafe_h) ? filters.to_unsafe_h : filters.to_h
61
- f = f.transform_keys(&:to_s)
67
+ present_filters(filters).filter_map do |key, value|
68
+ condition = condition_for(key, value)
69
+ condition unless condition == NO_CONDITION
70
+ end
71
+ end
62
72
 
63
- f.filter_map { |key, value| condition_for(key, value) unless value.nil? || value.to_s.empty? }
73
+ # The filters among these the index cannot apply exactly: one it has
74
+ # no translation for (`name_cont`, `search_by_name`, `cf_*`, any other
75
+ # Ransack predicate), or a translated one whose value it would have to
76
+ # drop or guess at (a malformed id, a price that is not a number). The
77
+ # provider answers a request carrying any from Spree's Database
78
+ # provider, which applies them all, rather than list more than was
79
+ # asked for. Blank values filter nothing anywhere and are not named.
80
+ #
81
+ # @param filters [Hash, ActionController::Parameters, nil]
82
+ # @return [Array<String>] their keys
83
+ def unanswered_filters(filters)
84
+ present_filters(filters).filter_map { |key, value| key if condition_for(key, value).nil? }
64
85
  end
65
86
 
87
+ # A condition (String), NO_CONDITION, or nil when the index cannot
88
+ # apply the filter exactly (see #unanswered_filters).
66
89
  def condition_for(key, value)
67
90
  case key
68
- when 'price_gte' then "price:>=#{Array(value).first.to_f}"
69
- when 'price_lte' then "price:<=#{Array(value).first.to_f}"
70
- when 'in_stock' then ('in_stock:=true' if value.to_s != '0')
71
- when 'out_of_stock' then ('in_stock:=false' if value.to_s != '0')
72
- when 'in_category'
91
+ when 'search', 'not_discontinued' then NO_CONDITION
92
+ when 'price_gte'
93
+ amount = number(Array(value).first)
94
+ "price:>=#{amount}" if amount
95
+ when 'price_lte'
96
+ amount = number(Array(value).first)
97
+ "price:<=#{amount}" if amount
98
+ when 'price_between'
99
+ low, high = Array(value).map { |bound| number(bound) }
100
+ "price:[#{low}..#{high}]" if Array(value).size == 2 && low && high
101
+ when 'in_stock' then false_flag?(value) ? NO_CONDITION : 'in_stock:=true'
102
+ when 'out_of_stock' then false_flag?(value) ? NO_CONDITION : 'in_stock:=false'
103
+ when 'in_category', 'in_taxon'
73
104
  "category_ids:=#{sanitize_id(value)}" if valid_id?(value)
74
105
  when 'in_categories'
75
- ids = valid_ids(value)
76
- "category_ids:=[#{ids.join(',')}]" if ids.any?
106
+ ids = present_values(value)
107
+ "category_ids:=[#{valid_ids(ids).join(',')}]" if ids.all? { |id| valid_id?(id) }
77
108
  when 'in_collection'
78
109
  "collection_ids:=#{sanitize_id(value)}" if valid_id?(value)
79
110
  when 'with_option_value_ids'
80
- nil # handled by grouped_option_conditions
111
+ NO_CONDITION if present_values(value).all? { |id| valid_id?(id) }
81
112
  # The records the filters endpoint passes on a category or collection
82
113
  # page, so facets and the total cover that grouping only.
83
114
  when '_category'
@@ -152,6 +183,26 @@ module Spree
152
183
  sanitize_id(id) if id
153
184
  end
154
185
 
186
+ # The filters with a value: nil, '' and a list of blanks filter
187
+ # nothing, here or in Spree's Database provider.
188
+ def present_filters(filters)
189
+ f = filters.respond_to?(:to_unsafe_h) ? filters.to_unsafe_h : (filters || {}).to_h
190
+ f.transform_keys(&:to_s).reject { |_key, value| present_values(value).empty? }
191
+ end
192
+
193
+ def present_values(value) = Array(value).reject { |v| v.nil? || v.to_s.empty? }
194
+
195
+ # What Ransack takes for false in a boolean scope, which it then
196
+ # leaves unapplied.
197
+ def false_flag?(value) = %w[0 f false off].include?(Array(value).first.to_s.downcase)
198
+
199
+ # A finite number, or nil: '10 || status:=draft' and 'Infinity' are
200
+ # not prices.
201
+ def number(value)
202
+ amount = value.is_a?(Numeric) ? value.to_f : Float(value.to_s.strip, exception: false)
203
+ amount if amount&.finite?
204
+ end
205
+
155
206
  def valid_id?(value) = PREFIXED_ID_PATTERN.match?(value.to_s)
156
207
  def sanitize_id(value) = value.to_s.gsub(/[^a-zA-Z0-9_]/, '')
157
208
  def valid_ids(values) = Array(values).filter_map { |v| sanitize_id(v) if valid_id?(v) }
@@ -290,6 +290,8 @@ module Spree
290
290
  scope: scope, query: query, filters: filters, sort: sort, page: page, limit: limit
291
291
  )
292
292
  end
293
+ return answer_from_database(filters, fallback) if answered_by_database?(filters)
294
+
293
295
  answer_through_breaker(query, fallback) do
294
296
  search_with_typesense(scope: scope, query: query, filters: filters, sort: sort, page: page, limit: limit)
295
297
  end
@@ -337,6 +339,8 @@ module Spree
337
339
  # As in #search_and_filter, only the Typesense requests are covered.
338
340
  def filters(scope:, query: nil, filters: {})
339
341
  fallback = -> { fallback_provider.filters(scope: scope, query: query, filters: filters) }
342
+ return answer_from_database(filters, fallback) if answered_by_database?(filters)
343
+
340
344
  answer_through_breaker(query, fallback) do
341
345
  result, distribution, stats = execute_faceted_search(query: query, filters: filters)
342
346
 
@@ -886,6 +890,29 @@ module Spree
886
890
  params
887
891
  end
888
892
 
893
+ # Whether the request carries a filter the index cannot apply exactly
894
+ # (see QueryBuilder.unanswered_filters), or option values none of which
895
+ # exists: core's `with_option_value_ids` then lists nothing, where the
896
+ # index, with no value to filter by, would list everything.
897
+ def answered_by_database?(filters)
898
+ return true if QueryBuilder.unanswered_filters(filters).any?
899
+
900
+ option_ids = QueryBuilder.present_values(normalize_filters(filters)['with_option_value_ids'])
901
+ option_ids.any? && group_option_values_by_type(option_ids).empty?
902
+ end
903
+
904
+ # Spree's Database provider answers a request whose filters the index
905
+ # cannot all apply, so a listing is never wider than asked for. That is
906
+ # a choice, not Typesense failing: logged at info, reported nowhere, and
907
+ # left out of the circuit breaker.
908
+ def answer_from_database(filters, fallback)
909
+ Rails.logger.info do
910
+ unanswered = QueryBuilder.unanswered_filters(filters).presence || ['with_option_value_ids']
911
+ "[spree_typesense] the index cannot apply #{unanswered.join(', ')}; answering from the Database provider"
912
+ end
913
+ fallback.call
914
+ end
915
+
889
916
  # Answers through Typesense unless the circuit breaker says it is
890
917
  # failing, in which case `fallback` (the Database provider) answers at
891
918
  # once, with nothing logged. A transient failure counts towards opening
@@ -1,6 +1,6 @@
1
1
  module Spree
2
2
  module Typesense
3
3
  # Major version tracks Spree's major version: 6.x supports Spree 6.x.
4
- VERSION = '6.0.2'.freeze
4
+ VERSION = '6.0.3'.freeze
5
5
  end
6
6
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spree-typesense
3
3
  version: !ruby/object:Gem::Version
4
- version: 6.0.2
4
+ version: 6.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aypex