spree-typesense 6.0.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.
@@ -0,0 +1,252 @@
1
+ module Spree
2
+ module Typesense
3
+ # Builds the Typesense documents for a product: flat, one per
4
+ # (market × locale) the product is priced in.
5
+ #
6
+ # The fields follow spree_meilisearch 6.0's presenter (categories,
7
+ # option `label`s, storefront-visible custom fields, a plain-text
8
+ # description, `collection_ids`, `option_value_combination_ids`), as Spree
9
+ # 6.0 core ships no presenter of its own (`search_product_presenter: nil`).
10
+ # Where spree_meilisearch writes a membership document per grouping for
11
+ # manual sort, documents here carry a `pos_<grouping prefixed id>` field per
12
+ # collection and category instead.
13
+ class ProductPresenter
14
+ attr_reader :product, :store
15
+
16
+ def initialize(product, store)
17
+ @product = product
18
+ @store = store
19
+ end
20
+
21
+ # Returns an array of documents — one per market × locale combination.
22
+ # Each document has flat name, description, price fields (no dynamic suffixes).
23
+ def call
24
+ # Indexing runs in a job or a rake task, where nothing has named the
25
+ # locale the base columns are written in.
26
+ with_store_content_locale { build_documents }
27
+ end
28
+
29
+ private
30
+
31
+ def build_documents
32
+ documents = []
33
+
34
+ market_locale_pairs.each do |market, locale|
35
+ # Skip if product has no price in this currency
36
+ next unless lowest_price(market.currency)
37
+
38
+ Mobility.with_locale(locale) do
39
+ documents << build_document(locale, market.currency, default_locale)
40
+ end
41
+ end
42
+
43
+ # Fallback for stores without markets (legacy/test), read in the locale
44
+ # it is labelled with rather than whatever locale the job runs in.
45
+ if documents.empty?
46
+ fallback_currency = store.default_market&.currency || store.supported_currencies_list.first&.iso_code
47
+ if fallback_currency && lowest_price(fallback_currency)
48
+ Mobility.with_locale(default_locale) do
49
+ documents << build_document(default_locale, fallback_currency, default_locale)
50
+ end
51
+ end
52
+ end
53
+
54
+ documents
55
+ end
56
+
57
+ # Spree 6 reads a translated attribute from its base column only in
58
+ # `Spree::Current.content_locale`, the locale the store's content is
59
+ # written in. A request sets that to the store's locale; a job or a rake
60
+ # task leaves it at the application's default. So for a store whose
61
+ # default market locale is not the application's (`nl` beside `en`),
62
+ # every name, description and slug read here would come out blank. Spree
63
+ # 6.0.5 fixed its own presenter (spree/spree#15226) by naming the store's
64
+ # locale with `Spree::Current.with_content_locale`; this does the same,
65
+ # and on 6.0.0 to 6.0.4, which lack that helper, sets and restores the
66
+ # attribute itself.
67
+ def with_store_content_locale(&block)
68
+ return Spree::Current.with_content_locale(default_locale, &block) if Spree::Current.respond_to?(:with_content_locale)
69
+
70
+ previous = Spree::Current.attributes[:content_locale]
71
+ Spree::Current.content_locale = default_locale.to_s
72
+ begin
73
+ yield
74
+ ensure
75
+ Spree::Current.content_locale = previous
76
+ end
77
+ end
78
+
79
+ def build_document(locale, currency, fallback_locale)
80
+ {
81
+ # Composite ID: product + locale + currency
82
+ id: "#{product.prefixed_id}_#{locale}_#{currency}",
83
+ product_id: product.prefixed_id,
84
+ locale: locale.to_s,
85
+ currency: currency,
86
+ # Translated fields — with fallback to default locale
87
+ name: translated(product, :name, fallback_locale),
88
+ # Indexed as plain text — HTML tag names would otherwise become
89
+ # searchable tokens.
90
+ description: Spree::RichTextHelper.to_plain_text(translated(product, :description, fallback_locale)),
91
+ slug: translated(product, :slug, fallback_locale),
92
+ # Price in this currency
93
+ price: lowest_price(currency)&.to_f,
94
+ compare_at_price: compare_at_price(currency)&.to_f,
95
+ # Non-locale/currency fields
96
+ status: product.status,
97
+ sku: product.sku,
98
+ in_stock: product.in_stock?,
99
+ # True when the product has an active pre-order variant, so a
100
+ # scheduled (future-published) launch still surfaces in search.
101
+ preorder: product.preorder?,
102
+ store_ids: Array(product.store_id).map(&:to_s),
103
+ channel_ids: channel_ids_for_store,
104
+ discontinue_on: product.discontinue_on&.to_i || 0,
105
+ category_ids: category_ids_with_ancestors,
106
+ category_names: product.categories.map { |c| translated(c, :name, fallback_locale) },
107
+ collection_ids: product.collections.map(&:prefixed_id),
108
+ option_type_ids: product.option_types.map(&:prefixed_id),
109
+ option_type_names: product.option_types.map { |ot| translated(ot, :label, fallback_locale) },
110
+ option_value_ids: variant_option_value_ids,
111
+ option_value_combination_ids: variant_option_value_combination_ids,
112
+ option_values: variant_option_values_data.map { |ov| translated(ov, :label, fallback_locale) }.uniq,
113
+ tags: product.tag_list || [],
114
+ units_sold_count: product.units_sold_count || 0,
115
+ available_on: product.available_on&.iso8601,
116
+ created_at: product.created_at&.iso8601,
117
+ updated_at: product.updated_at&.iso8601
118
+ }.merge(custom_field_document_attributes).merge(position_fields)
119
+ end
120
+
121
+ # Flat cf_* hash for searchable ∪ sortable custom fields (Meilisearch
122
+ # docs). Storefront-visible ones only — the index is the storefront's,
123
+ # and a value the serializer withholds has no business in a document it
124
+ # can search.
125
+ def custom_field_document_attributes
126
+ @custom_field_document_attributes ||= product.custom_fields.filter_map do |custom_field|
127
+ definition = custom_field.custom_field_definition
128
+ next unless definition&.storefront_visible? && (definition.searchable? || definition.sortable?)
129
+
130
+ [definition.filter_key, custom_field_index_value(custom_field, definition.field_type)]
131
+ end.to_h
132
+ end
133
+
134
+ def custom_field_index_value(field, field_type)
135
+ serialized = field.serialize_value
136
+ case field_type
137
+ when 'number'
138
+ serialized.to_f
139
+ else
140
+ serialized.to_s
141
+ end
142
+ end
143
+
144
+ # Returns all market × locale pairs for this store
145
+ def market_locale_pairs
146
+ @market_locale_pairs ||= store.markets.flat_map do |market|
147
+ market.supported_locales_list.map { |locale| [market, locale] }
148
+ end
149
+ end
150
+
151
+ def default_locale
152
+ @default_locale ||= store.default_market&.default_locale || I18n.default_locale.to_s
153
+ end
154
+
155
+ # Read a translated attribute with fallback to default locale.
156
+ def translated(record, attribute, fallback_locale)
157
+ value = record.send(attribute)
158
+ return value if value.present?
159
+
160
+ record.send(attribute, locale: fallback_locale.to_sym)
161
+ rescue ArgumentError
162
+ value
163
+ end
164
+
165
+ def lowest_price(currency)
166
+ @prices_cache ||= {}
167
+ @prices_cache[currency] = product.price_in(currency)&.amount unless @prices_cache.key?(currency)
168
+ @prices_cache[currency]
169
+ end
170
+
171
+ def compare_at_price(currency)
172
+ @compare_at_cache ||= {}
173
+ @compare_at_cache[currency] = product.compare_at_amount_in(currency) unless @compare_at_cache.key?(currency)
174
+ @compare_at_cache[currency]
175
+ end
176
+
177
+ def channel_ids_for_store
178
+ @channel_ids_for_store ||= product.product_publications
179
+ .joins(:channel)
180
+ .where(spree_channels: { store_id: store.id })
181
+ .pluck(:channel_id)
182
+ .map(&:to_s)
183
+ end
184
+
185
+ def category_ids_with_ancestors
186
+ @category_ids_with_ancestors ||= product.categories.flat_map { |c|
187
+ c.self_and_ancestors.map(&:prefixed_id)
188
+ }.uniq
189
+ end
190
+
191
+ def variant_option_value_ids
192
+ variant_option_values_data.map(&:prefixed_id).uniq
193
+ end
194
+
195
+ def variant_option_values_data
196
+ @variant_option_values_data ||= product.variants.flat_map(&:option_values).uniq
197
+ end
198
+
199
+ # `pos_<prefixed id>` => position, for each collection the product is in,
200
+ # and for each category it is in and every ancestor of it. A product
201
+ # under a category AND one of its descendants takes the lowest position
202
+ # in the subtree, as core's Database provider orders a category page
203
+ # (`MIN(position)` over the subtree) and spree_meilisearch's
204
+ # `category_positions` do. A membership without a position adds nothing,
205
+ # nor does one whose collection or category is gone: Spree 6 removes a
206
+ # deleted grouping's memberships in a later job (`destroy_async`).
207
+ def position_fields
208
+ @position_fields ||= collection_positions.merge(category_positions).to_h do |prefixed_id, position|
209
+ ["pos_#{prefixed_id}", position]
210
+ end
211
+ end
212
+
213
+ def collection_positions
214
+ product.product_collections.each_with_object({}) do |membership, acc|
215
+ collection = membership.collection
216
+ next if collection.nil? || membership.position.nil?
217
+
218
+ acc[collection.prefixed_id] = membership.position
219
+ end
220
+ end
221
+
222
+ def category_positions
223
+ product.product_categories.each_with_object({}) do |membership, acc|
224
+ grouping = membership.category
225
+ next if grouping.nil? || membership.position.nil?
226
+
227
+ grouping.self_and_ancestors.each do |category|
228
+ acc[category.prefixed_id] = [acc[category.prefixed_id], membership.position].compact.min
229
+ end
230
+ end
231
+ end
232
+
233
+ # One token per set of two or more option values that sit on the SAME
234
+ # variant, as spree_meilisearch 6.0.5 writes them. `option_value_ids` is a
235
+ # union across variants, so it cannot tell a product selling a blue XL
236
+ # from one selling a blue S beside a red XL; a token per whole set can.
237
+ # Sets reach MAX_COMBINATION_AXES values: the count grows exponentially,
238
+ # and a filter across more types falls back to core's narrowing.
239
+ def variant_option_value_combination_ids
240
+ @variant_option_value_combination_ids ||= begin
241
+ query_builder = Spree::SearchProvider::Typesense::QueryBuilder
242
+ product.variants.flat_map do |variant|
243
+ ids = variant.option_values.map(&:prefixed_id)
244
+ (2..[ids.size, query_builder::MAX_COMBINATION_AXES].min).flat_map do |size|
245
+ ids.combination(size).map { |set| query_builder.combination_token(set) }
246
+ end
247
+ end.uniq
248
+ end
249
+ end
250
+ end
251
+ end
252
+ end
@@ -0,0 +1,137 @@
1
+ require 'concurrent/map'
2
+
3
+ module Spree
4
+ module Typesense
5
+ # Remembers, for the process, that Typesense is failing, so searches stop
6
+ # waiting out its timeout before falling back. Each request builds a new
7
+ # provider and client, so without this every search during an outage waits
8
+ # the full search timeout and is logged and reported.
9
+ #
10
+ # Closed: requests go through, and transient failures in a row are counted.
11
+ # At the threshold it opens: requests are refused (the caller answers from
12
+ # the Database provider) for `open_seconds`. Then it is half-open: exactly
13
+ # one request is let through to probe; success closes it, failure opens it
14
+ # for another period.
15
+ #
16
+ # Per process and in memory, so each server process learns on its own.
17
+ class CircuitBreaker
18
+ DEFAULT_THRESHOLD = 3
19
+ DEFAULT_OPEN_SECONDS = 30
20
+
21
+ REGISTRY = Concurrent::Map.new
22
+ private_constant :REGISTRY
23
+
24
+ # The breaker for a Typesense URL, made on first use with the threshold
25
+ # and period TYPESENSE_BREAKER_THRESHOLD and TYPESENSE_BREAKER_SECONDS
26
+ # give, when they are positive numbers.
27
+ #
28
+ # @param key [String] the Typesense URL
29
+ # @return [CircuitBreaker]
30
+ def self.for(key)
31
+ REGISTRY.compute_if_absent(key.to_s) { new(threshold: configured_threshold, open_seconds: configured_open_seconds) }
32
+ end
33
+
34
+ # Forgets every breaker (tests).
35
+ def self.reset_all! = REGISTRY.clear
36
+
37
+ def self.configured_threshold
38
+ value = Integer(ENV.fetch('TYPESENSE_BREAKER_THRESHOLD', nil), exception: false)
39
+ value&.positive? ? value : DEFAULT_THRESHOLD
40
+ end
41
+
42
+ def self.configured_open_seconds
43
+ value = Float(ENV.fetch('TYPESENSE_BREAKER_SECONDS', nil), exception: false)
44
+ value&.positive? ? value : DEFAULT_OPEN_SECONDS
45
+ end
46
+
47
+ attr_reader :threshold, :open_seconds
48
+
49
+ # @param threshold [Integer] transient failures in a row that open it
50
+ # @param open_seconds [Numeric] how long it stays open
51
+ # @param clock [#call] monotonic seconds
52
+ def initialize(threshold:, open_seconds:, clock: -> { Process.clock_gettime(Process::CLOCK_MONOTONIC) })
53
+ @threshold = threshold
54
+ @open_seconds = open_seconds
55
+ @clock = clock
56
+ @mutex = Mutex.new
57
+ @open = false
58
+ @failures = 0
59
+ @opened_at = nil
60
+ @probing = false
61
+ @probe_started_at = nil
62
+ end
63
+
64
+ # @return [Symbol] :closed, :open or :half_open
65
+ def state
66
+ @mutex.synchronize { current_state }
67
+ end
68
+
69
+ # Whether this request may go to Typesense. Half-open, only the first
70
+ # caller may, as the probe. A probe that reports nothing for a whole
71
+ # period (killed by a request timeout or a thread kill, which no rescue
72
+ # sees) is given up on, and the next caller probes instead.
73
+ # @return [Boolean]
74
+ def allow_request?
75
+ @mutex.synchronize do
76
+ case current_state
77
+ when :closed then true
78
+ when :open then false
79
+ else
80
+ next false if @probing && @clock.call - @probe_started_at < @open_seconds
81
+
82
+ @probing = true
83
+ @probe_started_at = @clock.call
84
+ true
85
+ end
86
+ end
87
+ end
88
+
89
+ # Typesense answered.
90
+ # @return [Symbol, nil] :closed when this closed an open breaker
91
+ def record_success
92
+ @mutex.synchronize do
93
+ was_open = @open
94
+ @open = false
95
+ @failures = 0
96
+ @opened_at = nil
97
+ @probing = false
98
+ :closed if was_open
99
+ end
100
+ end
101
+
102
+ # Typesense failed transiently.
103
+ # @return [Symbol, nil] :opened when this opened it (or the probe failed)
104
+ def record_failure
105
+ @mutex.synchronize do
106
+ if @open
107
+ # A request let through before it opened; only the probe counts.
108
+ next nil unless @probing
109
+ else
110
+ @failures += 1
111
+ next nil if @failures < @threshold
112
+ end
113
+
114
+ @open = true
115
+ @opened_at = @clock.call
116
+ @failures = 0
117
+ @probing = false
118
+ :opened
119
+ end
120
+ end
121
+
122
+ # The probe ended in neither success nor failure (an error that is not
123
+ # Typesense's): let the next request probe instead.
124
+ def release_probe
125
+ @mutex.synchronize { @probing = false }
126
+ end
127
+
128
+ private
129
+
130
+ def current_state
131
+ return :closed unless @open
132
+
133
+ @clock.call - @opened_at >= @open_seconds ? :half_open : :open
134
+ end
135
+ end
136
+ end
137
+ end
@@ -0,0 +1,15 @@
1
+ module Spree
2
+ module Typesense
3
+ class Engine < ::Rails::Engine
4
+ engine_name 'spree_typesense'
5
+ config.autoload_paths += %W[#{config.root}/app/models]
6
+
7
+ # After initialize, so an application's own
8
+ # `Spree::Dependencies.search_product_presenter = ...` (set in an
9
+ # initializer) is already recorded and wins.
10
+ config.after_initialize do
11
+ Spree::Typesense.register_search_product_presenter!
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,6 @@
1
+ module Spree
2
+ module Typesense
3
+ # Major version tracks Spree's major version: 6.x supports Spree 6.x.
4
+ VERSION = '6.0.0'.freeze
5
+ end
6
+ end
@@ -0,0 +1,42 @@
1
+ require 'spree_core'
2
+ require 'typesense'
3
+ require 'concurrent/atomic/atomic_boolean'
4
+ require 'spree/typesense/version'
5
+ require 'spree/typesense/circuit_breaker'
6
+ require 'spree/typesense/engine'
7
+
8
+ module Spree
9
+ # What the gem adds around its search provider,
10
+ # Spree::SearchProvider::Typesense: the document presenter, the circuit
11
+ # breaker and the engine.
12
+ #
13
+ # Inside `module Spree`, a bare `Typesense` now names this module, so code in
14
+ # the Spree namespace reaches the typesense-ruby client as `::Typesense`.
15
+ module Typesense
16
+ PRESENTER_CLASS_NAME = 'Spree::Typesense::ProductPresenter'.freeze
17
+
18
+ module_function
19
+
20
+ # Spree 6 core ships no search document presenter
21
+ # (`search_product_presenter: nil`); the indexing provider gem supplies one.
22
+ # Same pattern as spree_meilisearch: register ours unless the application,
23
+ # or another search gem, already named one.
24
+ #
25
+ # Only when Typesense is the selected `Spree.search_provider`, though. There
26
+ # is one presenter setting for every installed search gem, so with
27
+ # spree_meilisearch installed too, registering ours while Meilisearch is the
28
+ # selected provider would make spree_meilisearch (whose engine skips
29
+ # registering once the setting is overridden) index with our documents.
30
+ def register_search_product_presenter!
31
+ return unless typesense_selected?
32
+ return if Spree::Dependencies.overridden?(:search_product_presenter)
33
+
34
+ Spree::Dependencies.search_product_presenter = PRESENTER_CLASS_NAME
35
+ end
36
+
37
+ def typesense_selected?
38
+ provider = Spree.search_provider.to_s.safe_constantize
39
+ provider.is_a?(Class) && provider <= Spree::SearchProvider::Typesense
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,6 @@
1
+ # Bundler auto-requires a gem by its *name*, so `gem 'spree-typesense'` in a host
2
+ # Gemfile issues `require 'spree-typesense'`. The gem's real entry point is
3
+ # `spree/typesense`, matching the Spree::Typesense namespace, so this shim keeps
4
+ # the default `Bundler.require` working without every consumer having to write
5
+ # `gem 'spree-typesense', require: 'spree/typesense'`.
6
+ require 'spree/typesense'
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: spree-typesense
3
+ version: !ruby/object:Gem::Version
4
+ version: 6.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Aypex
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: spree_core
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '6.0'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - "~>"
24
+ - !ruby/object:Gem::Version
25
+ version: '6.0'
26
+ - !ruby/object:Gem::Dependency
27
+ name: typesense
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - "~>"
31
+ - !ruby/object:Gem::Version
32
+ version: '5.0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '5.0'
40
+ - !ruby/object:Gem::Dependency
41
+ name: rake
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '13.0'
47
+ type: :development
48
+ prerelease: false
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '13.0'
54
+ - !ruby/object:Gem::Dependency
55
+ name: rspec
56
+ requirement: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '3.13'
61
+ type: :development
62
+ prerelease: false
63
+ version_requirements: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '3.13'
68
+ description: 'A Typesense search provider for Spree Commerce 6: search, filters, facets
69
+ and sorting through Spree::SearchProvider::Base, with a fallback to the Database
70
+ provider.'
71
+ executables: []
72
+ extensions: []
73
+ extra_rdoc_files: []
74
+ files:
75
+ - CHANGELOG.md
76
+ - LICENSE
77
+ - README.md
78
+ - Rakefile
79
+ - app/models/spree/search_provider/typesense.rb
80
+ - app/models/spree/search_provider/typesense/facets.rb
81
+ - app/models/spree/search_provider/typesense/index_layout.rb
82
+ - app/models/spree/search_provider/typesense/query_builder.rb
83
+ - app/models/spree/search_provider/typesense/schema.rb
84
+ - app/presenters/spree/typesense/product_presenter.rb
85
+ - lib/spree-typesense.rb
86
+ - lib/spree/typesense.rb
87
+ - lib/spree/typesense/circuit_breaker.rb
88
+ - lib/spree/typesense/engine.rb
89
+ - lib/spree/typesense/version.rb
90
+ homepage: https://github.com/aypex-io/spree-typesense
91
+ licenses:
92
+ - MIT
93
+ metadata:
94
+ source_code_uri: https://github.com/aypex-io/spree-typesense
95
+ bug_tracker_uri: https://github.com/aypex-io/spree-typesense/issues
96
+ changelog_uri: https://github.com/aypex-io/spree-typesense/blob/main/CHANGELOG.md
97
+ rubygems_mfa_required: 'true'
98
+ rdoc_options: []
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '3.3'
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ requirements: []
112
+ rubygems_version: 4.0.20
113
+ specification_version: 4
114
+ summary: Typesense search provider for Spree
115
+ test_files: []