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.
- checksums.yaml +7 -0
- data/CHANGELOG.md +203 -0
- data/LICENSE +9 -0
- data/README.md +311 -0
- data/Rakefile +6 -0
- data/app/models/spree/search_provider/typesense/facets.rb +147 -0
- data/app/models/spree/search_provider/typesense/index_layout.rb +91 -0
- data/app/models/spree/search_provider/typesense/query_builder.rb +161 -0
- data/app/models/spree/search_provider/typesense/schema.rb +128 -0
- data/app/models/spree/search_provider/typesense.rb +901 -0
- data/app/presenters/spree/typesense/product_presenter.rb +252 -0
- data/lib/spree/typesense/circuit_breaker.rb +137 -0
- data/lib/spree/typesense/engine.rb +15 -0
- data/lib/spree/typesense/version.rb +6 -0
- data/lib/spree/typesense.rb +42 -0
- data/lib/spree-typesense.rb +6 -0
- metadata +115 -0
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
module Spree
|
|
2
|
+
module SearchProvider
|
|
3
|
+
class Typesense < Base
|
|
4
|
+
module Facets
|
|
5
|
+
module_function
|
|
6
|
+
|
|
7
|
+
# Typesense returns an array; Meilisearch returns a hash keyed by field.
|
|
8
|
+
# Normalise to the hash shape so the builders below read like upstream.
|
|
9
|
+
def normalize(facet_counts)
|
|
10
|
+
Array(facet_counts).each_with_object({}) do |facet, acc|
|
|
11
|
+
acc[facet['field_name']] = Array(facet['counts']).to_h { |c| [c['value'], c['count']] }
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
# Sibling to +normalize+, kept separate so the counts shape every builder
|
|
16
|
+
# reads stays a plain { value => count } hash.
|
|
17
|
+
#
|
|
18
|
+
# EVERY facet carries a `stats` block, but only numeric fields get
|
|
19
|
+
# min/max in it — string[] and bool facets return `{total_values: N}`
|
|
20
|
+
# and nothing else, as does a numeric field whose search matched no
|
|
21
|
+
# documents. So consumers must test for the KEY, not for the hash.
|
|
22
|
+
#
|
|
23
|
+
# Of the numeric keys only min/max are trustworthy: they are computed
|
|
24
|
+
# over the FULL matching set and respect filter_by, whereas
|
|
25
|
+
# sum/avg/total_values describe only the truncated set of returned
|
|
26
|
+
# values. Junk blocks are kept rather than filtered — the consumer
|
|
27
|
+
# guards on the key, and dropping them here would hide what the engine
|
|
28
|
+
# actually returned.
|
|
29
|
+
def normalize_stats(facet_counts)
|
|
30
|
+
Array(facet_counts).each_with_object({}) do |facet, acc|
|
|
31
|
+
acc[facet['field_name']] = facet['stats'] if facet['stats'].present?
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# Order matters — it is the order the storefront renders filters in, and
|
|
36
|
+
# mirrors meilisearch.rb#build_facet_response. No `.compact`: every
|
|
37
|
+
# builder below returns a hash or is skipped by its guard, so a nil can
|
|
38
|
+
# never reach the array. Re-add it if that stops being true.
|
|
39
|
+
def build(distribution, currency:, stats: {})
|
|
40
|
+
facets = []
|
|
41
|
+
facets << price_facet(distribution['price'], stats['price'], currency) if distribution['price'].present?
|
|
42
|
+
facets << availability_facet(distribution['in_stock']) if distribution['in_stock'].present?
|
|
43
|
+
facets.concat(option_facets(distribution['option_value_ids'])) if distribution['option_value_ids'].present?
|
|
44
|
+
facets << category_facet(distribution['category_ids']) if distribution['category_ids'].present?
|
|
45
|
+
facets
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# Range comes from `stats`, NOT from the facet keys. Typesense truncates
|
|
49
|
+
# the returned values (keeping the highest) once a search matches more
|
|
50
|
+
# distinct prices than the facet cap, so a key-derived min is silently
|
|
51
|
+
# wrong on any decent-sized catalogue — 120 prices from 3.50 reported a
|
|
52
|
+
# min of 23.50. stats.min/max are exact at any cardinality and honour
|
|
53
|
+
# filter_by, so they narrow correctly as the shopper filters.
|
|
54
|
+
#
|
|
55
|
+
# Guard on the KEY, not on the hash: Typesense returns a stats block with
|
|
56
|
+
# only `total_values` for non-numeric facets AND for a numeric facet whose
|
|
57
|
+
# search matched nothing. Testing `stats ? ...` emitted min: nil, max: nil
|
|
58
|
+
# for those and broke the storefront's price slider. The key fallback
|
|
59
|
+
# yields an approximate range, which beats no range.
|
|
60
|
+
def price_facet(distribution, stats, currency)
|
|
61
|
+
amounts = distribution.keys.map(&:to_f)
|
|
62
|
+
{
|
|
63
|
+
id: 'price',
|
|
64
|
+
type: 'price_range',
|
|
65
|
+
min: stats&.key?('min') ? stats['min'] : amounts.min,
|
|
66
|
+
max: stats&.key?('max') ? stats['max'] : amounts.max,
|
|
67
|
+
currency: currency
|
|
68
|
+
}
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def availability_facet(distribution)
|
|
72
|
+
{
|
|
73
|
+
id: 'availability',
|
|
74
|
+
type: 'availability',
|
|
75
|
+
options: [
|
|
76
|
+
{ id: 'in_stock', count: distribution['true'] || 0 },
|
|
77
|
+
{ id: 'out_of_stock', count: distribution['false'] || 0 }
|
|
78
|
+
]
|
|
79
|
+
}
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
# ONE ENTRY PER OPTION TYPE (not per value), options sorted by position.
|
|
83
|
+
# Needs the database for names, labels, positions and image URLs — the
|
|
84
|
+
# search index only carries prefixed ids.
|
|
85
|
+
def option_facets(distribution)
|
|
86
|
+
option_values = lookup(Spree::OptionValue, distribution.keys) { |rel| rel.includes(:option_type).preload_associations_lazily }
|
|
87
|
+
|
|
88
|
+
by_option_type = distribution.each_with_object({}) do |(prefixed_id, count), acc|
|
|
89
|
+
# A stale index can name values that no longer exist — skip, never guess.
|
|
90
|
+
option_value = option_values[prefixed_id] or next
|
|
91
|
+
|
|
92
|
+
(acc[option_value.option_type] ||= []) << {
|
|
93
|
+
id: option_value.prefixed_id,
|
|
94
|
+
name: option_value.name,
|
|
95
|
+
label: option_value.label,
|
|
96
|
+
position: option_value.position,
|
|
97
|
+
color_code: option_value.color_code,
|
|
98
|
+
image_url: image_url(option_value),
|
|
99
|
+
count: count
|
|
100
|
+
}
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
by_option_type.map do |option_type, values|
|
|
104
|
+
{
|
|
105
|
+
id: option_type.prefixed_id,
|
|
106
|
+
type: 'option',
|
|
107
|
+
name: option_type.name,
|
|
108
|
+
label: option_type.label,
|
|
109
|
+
kind: option_type.kind,
|
|
110
|
+
options: values.sort_by { |o| o[:position] }
|
|
111
|
+
}
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
# All categories collapse into ONE facet whose id is the literal
|
|
116
|
+
# 'categories' — unlike options, which get one facet per type.
|
|
117
|
+
def category_facet(distribution)
|
|
118
|
+
categories = lookup(Spree::Category, distribution.keys)
|
|
119
|
+
|
|
120
|
+
{
|
|
121
|
+
id: 'categories',
|
|
122
|
+
type: 'category',
|
|
123
|
+
options: distribution.filter_map do |prefixed_id, count|
|
|
124
|
+
category = categories[prefixed_id] or next
|
|
125
|
+
|
|
126
|
+
{ id: category.prefixed_id, name: category.name, permalink: category.permalink, count: count }
|
|
127
|
+
end
|
|
128
|
+
}
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
# Decode prefixed ids -> one query -> index back by prefixed id.
|
|
132
|
+
def lookup(klass, prefixed_ids)
|
|
133
|
+
raw_ids = prefixed_ids.filter_map { |pid| klass.decode_prefixed_id(pid) }
|
|
134
|
+
relation = klass.where(id: raw_ids)
|
|
135
|
+
relation = yield(relation) if block_given?
|
|
136
|
+
relation.index_by(&:prefixed_id)
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
def image_url(option_value)
|
|
140
|
+
return nil unless option_value.image&.attached?
|
|
141
|
+
|
|
142
|
+
Rails.application.routes.url_helpers.cdn_image_url(option_value.image)
|
|
143
|
+
end
|
|
144
|
+
end
|
|
145
|
+
end
|
|
146
|
+
end
|
|
147
|
+
end
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
module Spree
|
|
2
|
+
module SearchProvider
|
|
3
|
+
class Typesense < Base
|
|
4
|
+
# Where a store's documents live. Searches and single-product writes go
|
|
5
|
+
# to the live alias, `products_<code>`; a full reindex builds a fresh
|
|
6
|
+
# versioned collection, `products_<code>_v<epoch ms>`, marks it with the
|
|
7
|
+
# `products_<code>__building` alias while it fills, then points the live
|
|
8
|
+
# alias at it. 0.2.0 kept the documents in a real collection holding the
|
|
9
|
+
# live name; that is the legacy collection the first reindex replaces.
|
|
10
|
+
class IndexLayout
|
|
11
|
+
attr_reader :client
|
|
12
|
+
|
|
13
|
+
# @param client [Typesense::Client]
|
|
14
|
+
# @param store_code [String]
|
|
15
|
+
def initialize(client, store_code)
|
|
16
|
+
@client = client
|
|
17
|
+
@store_code = store_code.to_s
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# @return [String] the alias searches and writes go through
|
|
21
|
+
def live_name = "products_#{@store_code}"
|
|
22
|
+
|
|
23
|
+
# @return [String] the alias naming the collection a reindex is filling
|
|
24
|
+
def building_name = "#{live_name}__building"
|
|
25
|
+
|
|
26
|
+
# @param now_ms [Integer] epoch milliseconds (13 digits)
|
|
27
|
+
# @return [String]
|
|
28
|
+
def new_version_name(now_ms) = "#{live_name}_v#{now_ms}"
|
|
29
|
+
|
|
30
|
+
# Anchored, and the timestamp is exactly 13 digits, so a store whose
|
|
31
|
+
# code extends this one's (`shop_v2` beside `shop`) never matches.
|
|
32
|
+
# @return [Regexp]
|
|
33
|
+
def version_pattern = /\A#{Regexp.escape(live_name)}_v\d{13}\z/
|
|
34
|
+
|
|
35
|
+
# @param alias_name [String]
|
|
36
|
+
# @return [String, nil] the collection the alias points at
|
|
37
|
+
def alias_target(alias_name)
|
|
38
|
+
client.aliases[alias_name].retrieve['collection_name']
|
|
39
|
+
rescue ::Typesense::Error::ObjectNotFound
|
|
40
|
+
nil
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# Whether a real collection holds the live name. 0.2.0 created one,
|
|
44
|
+
# and rolling back to 0.2.0 creates one beside the alias, where it
|
|
45
|
+
# takes requests by that name ahead of the alias.
|
|
46
|
+
# @return [Boolean]
|
|
47
|
+
def legacy_collection? = collection_names.include?(live_name)
|
|
48
|
+
|
|
49
|
+
# Where a single product's writes go: the live name, and the
|
|
50
|
+
# collection being built when a reindex is under way, so the change
|
|
51
|
+
# survives the swap.
|
|
52
|
+
# @return [Array<String>]
|
|
53
|
+
def write_targets
|
|
54
|
+
building = alias_target(building_name)
|
|
55
|
+
return [live_name] if building.nil? || building == alias_target(live_name)
|
|
56
|
+
|
|
57
|
+
[live_name, building]
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# @param alias_name [String]
|
|
61
|
+
# @param collection_name [String]
|
|
62
|
+
def point!(alias_name, collection_name)
|
|
63
|
+
client.aliases.upsert(alias_name, collection_name: collection_name)
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# Removes the alias; one already gone is fine.
|
|
67
|
+
# @param alias_name [String]
|
|
68
|
+
def unpoint!(alias_name)
|
|
69
|
+
client.aliases[alias_name].delete
|
|
70
|
+
rescue ::Typesense::Error::ObjectNotFound
|
|
71
|
+
nil
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# Drops the collection; one already gone is fine.
|
|
75
|
+
# @param collection_name [String]
|
|
76
|
+
def drop!(collection_name)
|
|
77
|
+
client.collections[collection_name].delete
|
|
78
|
+
rescue ::Typesense::Error::ObjectNotFound
|
|
79
|
+
nil
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
# @return [Array<String>] this store's versioned collections
|
|
83
|
+
def versioned_collections = collection_names.grep(version_pattern)
|
|
84
|
+
|
|
85
|
+
private
|
|
86
|
+
|
|
87
|
+
def collection_names = client.collections.retrieve.map { |collection| collection['name'] }
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
end
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
module Spree
|
|
2
|
+
module SearchProvider
|
|
3
|
+
class Typesense < Base
|
|
4
|
+
module QueryBuilder
|
|
5
|
+
PREFIXED_ID_PATTERN = /\A[a-z]+_[A-Za-z0-9]+\z/
|
|
6
|
+
|
|
7
|
+
# Spree 6 documents carry one token per set of 2..6 option values one
|
|
8
|
+
# variant has (option_value_combination_ids). A selection across more
|
|
9
|
+
# types than that, or one making more tokens than the filter should
|
|
10
|
+
# carry, falls back to the per-type pre-filter narrowed by core.
|
|
11
|
+
MAX_COMBINATION_AXES = 6
|
|
12
|
+
MAX_COMBINATION_TOKENS = 1000
|
|
13
|
+
|
|
14
|
+
SORT_MAP = {
|
|
15
|
+
'price' => 'price:asc',
|
|
16
|
+
'-price' => 'price:desc',
|
|
17
|
+
'name' => 'name:asc',
|
|
18
|
+
'-name' => 'name:desc',
|
|
19
|
+
'available_on' => 'available_on:asc',
|
|
20
|
+
'-available_on' => 'available_on:desc',
|
|
21
|
+
'best_selling' => 'units_sold_count:desc'
|
|
22
|
+
}.freeze
|
|
23
|
+
|
|
24
|
+
module_function
|
|
25
|
+
|
|
26
|
+
# Always applied. Mirrors meilisearch.rb#system_filter_conditions.
|
|
27
|
+
# locale/currency are NOT optional — documents are composite-keyed per
|
|
28
|
+
# locale x currency, so omitting them returns every market's copy.
|
|
29
|
+
#
|
|
30
|
+
# `channel_id` narrows to the products published on that channel, as
|
|
31
|
+
# core's storefront scope does (`for_channel`). Documents carry raw
|
|
32
|
+
# store and channel ids as strings. String values go into the filter
|
|
33
|
+
# in backticks, Typesense's string literal, so a UUID or a hyphenated
|
|
34
|
+
# locale (`en-US`) stays one value; anything that could close the
|
|
35
|
+
# quote is stripped first.
|
|
36
|
+
def system_conditions(store:, locale:, currency:, channel_id: nil, now: Time.current)
|
|
37
|
+
conditions = ["store_ids:=`#{store.id.to_s.gsub(/[^a-zA-Z0-9_-]/, '')}`"]
|
|
38
|
+
conditions << "channel_ids:=`#{channel_id.to_s.gsub(/[^a-zA-Z0-9_-]/, '')}`" if channel_id.present?
|
|
39
|
+
conditions << "status:=active"
|
|
40
|
+
conditions << "locale:=`#{locale.to_s.gsub(/[^a-zA-Z_-]/, '')}`"
|
|
41
|
+
conditions << "currency:=#{currency.to_s.upcase.gsub(/[^A-Z]/, '')}"
|
|
42
|
+
# available_on_i, NOT available_on: Typesense silently matches zero
|
|
43
|
+
# documents for range operators on string fields. A product
|
|
44
|
+
# scheduled to publish later still shows while it is on pre-order,
|
|
45
|
+
# as core's storefront scope (`include_preorderable: true`) and
|
|
46
|
+
# Meilisearch's `OR preorder = true` keep it.
|
|
47
|
+
conditions << "(available_on_i:<=#{now.to_i} || preorder:=true)"
|
|
48
|
+
# 0 is the sentinel for "never discontinued"; a future timestamp
|
|
49
|
+
# means discontinued LATER, so those products must still be
|
|
50
|
+
# visible now. Mirrors meilisearch.rb's
|
|
51
|
+
# "(discontinue_on = 0 OR discontinue_on > now)".
|
|
52
|
+
conditions << "(discontinue_on:=0 || discontinue_on:>#{now.to_i})"
|
|
53
|
+
conditions
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# 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.
|
|
59
|
+
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)
|
|
62
|
+
|
|
63
|
+
f.filter_map { |key, value| condition_for(key, value) unless value.nil? || value.to_s.empty? }
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def condition_for(key, value)
|
|
67
|
+
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'
|
|
73
|
+
"category_ids:=#{sanitize_id(value)}" if valid_id?(value)
|
|
74
|
+
when 'in_categories'
|
|
75
|
+
ids = valid_ids(value)
|
|
76
|
+
"category_ids:=[#{ids.join(',')}]" if ids.any?
|
|
77
|
+
when 'in_collection'
|
|
78
|
+
"collection_ids:=#{sanitize_id(value)}" if valid_id?(value)
|
|
79
|
+
when 'with_option_value_ids'
|
|
80
|
+
nil # handled by grouped_option_conditions
|
|
81
|
+
# The records the filters endpoint passes on a category or collection
|
|
82
|
+
# page, so facets and the total cover that grouping only.
|
|
83
|
+
when '_category'
|
|
84
|
+
grouping_condition('category_ids', value)
|
|
85
|
+
when '_collection'
|
|
86
|
+
grouping_condition('collection_ids', value)
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def grouping_condition(field, record)
|
|
91
|
+
return unless record.respond_to?(:prefixed_id) && valid_id?(record.prefixed_id)
|
|
92
|
+
|
|
93
|
+
"#{field}:=#{sanitize_id(record.prefixed_id)}"
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
# The token a set of option values makes, the same whichever order
|
|
97
|
+
# they come in. The presenter writes these, the query asks for them.
|
|
98
|
+
def combination_token(prefixed_ids) = prefixed_ids.map(&:to_s).sort.join('|')
|
|
99
|
+
|
|
100
|
+
# The conditions a selection of option values (grouped by option type)
|
|
101
|
+
# adds, and whether they answer it exactly. Values of one type are a
|
|
102
|
+
# union, which the index answers. Values of several types must sit on
|
|
103
|
+
# one variant (core's `with_option_value_ids`): there the condition
|
|
104
|
+
# asks for any token over the cartesian product of the selected values.
|
|
105
|
+
# Beyond what the tokens cover, the per-type conditions are only a
|
|
106
|
+
# pre-filter.
|
|
107
|
+
#
|
|
108
|
+
# @param grouped [Hash{Object => Array<String>}] prefixed ids by option type
|
|
109
|
+
# @return [Array(Array<String>, Boolean)] conditions, exact
|
|
110
|
+
def option_conditions(grouped)
|
|
111
|
+
axes = grouped.values.map { |ids| valid_ids(ids) }.reject(&:empty?)
|
|
112
|
+
return [[], true] if axes.empty?
|
|
113
|
+
|
|
114
|
+
per_type = grouped_option_conditions(grouped)
|
|
115
|
+
return [per_type, true] if axes.size == 1
|
|
116
|
+
return [per_type, false] if axes.size > MAX_COMBINATION_AXES
|
|
117
|
+
return [per_type, false] if axes.map(&:size).reduce(:*) > MAX_COMBINATION_TOKENS
|
|
118
|
+
|
|
119
|
+
tokens = axes.first.product(*axes.drop(1)).map { |values| combination_token(values) }.uniq
|
|
120
|
+
[["option_value_combination_ids:=[#{tokens.map { |token| "`#{token}`" }.join(',')}]"], true]
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
# OR within an option type, AND across option types.
|
|
124
|
+
def grouped_option_conditions(grouped)
|
|
125
|
+
grouped.filter_map do |_type_id, prefixed_ids|
|
|
126
|
+
ids = valid_ids(prefixed_ids)
|
|
127
|
+
next if ids.empty?
|
|
128
|
+
|
|
129
|
+
ids.length > 1 ? "option_value_ids:=[#{ids.join(',')}]" : "option_value_ids:=#{ids.first}"
|
|
130
|
+
end
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
# The Typesense sort for the API's sort param. A listing of one
|
|
134
|
+
# collection or category in the merchant's order ('manual', or no sort
|
|
135
|
+
# there, whatever the text query, as core's Database provider decides)
|
|
136
|
+
# sorts by the product's position in it; the collection wins when both
|
|
137
|
+
# are given, as in core's `manual_sort_grouping`. 'manual' with no
|
|
138
|
+
# single grouping keeps the default ranking.
|
|
139
|
+
def sort_by(sort_param, filters = {})
|
|
140
|
+
if sort_param.blank? || sort_param.to_s == 'manual'
|
|
141
|
+
grouping = manual_grouping(filters)
|
|
142
|
+
return "pos_#{grouping}:asc" if grouping
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
SORT_MAP[sort_param.to_s]
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
def manual_grouping(filters)
|
|
149
|
+
f = filters.respond_to?(:to_unsafe_h) ? filters.to_unsafe_h : (filters || {}).to_h
|
|
150
|
+
f = f.transform_keys(&:to_s)
|
|
151
|
+
id = [f['in_collection'], f['in_category']].find { |value| valid_id?(value) }
|
|
152
|
+
sanitize_id(id) if id
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
def valid_id?(value) = PREFIXED_ID_PATTERN.match?(value.to_s)
|
|
156
|
+
def sanitize_id(value) = value.to_s.gsub(/[^a-zA-Z0-9_]/, '')
|
|
157
|
+
def valid_ids(values) = Array(values).filter_map { |v| sanitize_id(v) if valid_id?(v) }
|
|
158
|
+
end
|
|
159
|
+
end
|
|
160
|
+
end
|
|
161
|
+
end
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
module Spree
|
|
2
|
+
module SearchProvider
|
|
3
|
+
class Typesense < Base
|
|
4
|
+
module Schema
|
|
5
|
+
# Transcribed from Spree::Typesense::ProductPresenter#build_document.
|
|
6
|
+
# `cf_*` keys are dynamic and left undeclared — Typesense stores them
|
|
7
|
+
# without indexing them, so they can be neither searched, filtered nor
|
|
8
|
+
# sorted on.
|
|
9
|
+
# Do not add fields the presenter does not emit.
|
|
10
|
+
SEARCHABLE_FIELDS = %w[name description sku option_values category_names tags].freeze
|
|
11
|
+
|
|
12
|
+
# Mirrors meilisearch.rb#facet_attributes (== filterable_attributes).
|
|
13
|
+
FACET_FIELDS = %w[
|
|
14
|
+
product_id status in_stock store_ids channel_ids locale currency
|
|
15
|
+
price category_ids tags option_value_ids preorder
|
|
16
|
+
].freeze
|
|
17
|
+
|
|
18
|
+
SORT_FIELDS = %w[name price created_at available_on units_sold_count].freeze
|
|
19
|
+
|
|
20
|
+
# MUST be numeric — Typesense rejects a string default_sorting_field.
|
|
21
|
+
# created_at is an ISO8601 string, so units_sold_count serves instead.
|
|
22
|
+
DEFAULT_SORTING_FIELD = 'units_sold_count'.freeze
|
|
23
|
+
|
|
24
|
+
module_function
|
|
25
|
+
|
|
26
|
+
def definition(collection_name)
|
|
27
|
+
{
|
|
28
|
+
name: collection_name,
|
|
29
|
+
default_sorting_field: DEFAULT_SORTING_FIELD,
|
|
30
|
+
fields: [
|
|
31
|
+
{ name: 'product_id', type: 'string', facet: true },
|
|
32
|
+
{ name: 'locale', type: 'string', facet: true },
|
|
33
|
+
{ name: 'currency', type: 'string', facet: true },
|
|
34
|
+
|
|
35
|
+
{ name: 'name', type: 'string', facet: false, sort: true },
|
|
36
|
+
{ name: 'description', type: 'string', facet: false, optional: true },
|
|
37
|
+
{ name: 'slug', type: 'string', facet: false, optional: true },
|
|
38
|
+
|
|
39
|
+
{ name: 'price', type: 'float', facet: true, optional: true, sort: true },
|
|
40
|
+
{ name: 'compare_at_price', type: 'float', facet: false, optional: true },
|
|
41
|
+
|
|
42
|
+
{ name: 'status', type: 'string', facet: true },
|
|
43
|
+
{ name: 'sku', type: 'string', facet: false, optional: true },
|
|
44
|
+
{ name: 'in_stock', type: 'bool', facet: true },
|
|
45
|
+
|
|
46
|
+
{ name: 'store_ids', type: 'string[]', facet: true },
|
|
47
|
+
{ name: 'channel_ids', type: 'string[]', facet: true, optional: true },
|
|
48
|
+
{ name: 'discontinue_on', type: 'int64', facet: false },
|
|
49
|
+
|
|
50
|
+
{ name: 'category_ids', type: 'string[]', facet: true, optional: true },
|
|
51
|
+
{ name: 'category_names', type: 'string[]', facet: false, optional: true },
|
|
52
|
+
# For the in_collection filter. Typesense filters on any indexed
|
|
53
|
+
# field, so no facet: collection facets are not offered.
|
|
54
|
+
{ name: 'collection_ids', type: 'string[]', facet: false, optional: true },
|
|
55
|
+
|
|
56
|
+
# The product's position in each collection and
|
|
57
|
+
# category it belongs to, `pos_<prefixed id>` (an ancestor
|
|
58
|
+
# category takes the lowest position below it). A manual listing
|
|
59
|
+
# of one grouping sorts by its field. Declared by pattern, so
|
|
60
|
+
# each grouping's field appears as documents carry it.
|
|
61
|
+
{ name: 'pos_.*', type: 'int32', facet: false, optional: true, sort: true },
|
|
62
|
+
|
|
63
|
+
{ name: 'option_type_ids', type: 'string[]', facet: false, optional: true },
|
|
64
|
+
{ name: 'option_type_names', type: 'string[]', facet: false, optional: true },
|
|
65
|
+
{ name: 'option_value_ids', type: 'string[]', facet: true, optional: true },
|
|
66
|
+
# One token per set of 2..6 option values one
|
|
67
|
+
# variant carries (`ov_a|ov_b`), so a filter on several option
|
|
68
|
+
# types matches a single variant, as core's scope does.
|
|
69
|
+
{ name: 'option_value_combination_ids', type: 'string[]', facet: false, optional: true },
|
|
70
|
+
{ name: 'option_values', type: 'string[]', facet: false, optional: true },
|
|
71
|
+
|
|
72
|
+
{ name: 'tags', type: 'string[]', facet: true, optional: true },
|
|
73
|
+
{ name: 'units_sold_count', type: 'int32', facet: false },
|
|
74
|
+
|
|
75
|
+
# ISO8601 STRINGS. They sort lexicographically in chronological
|
|
76
|
+
# order, so sort: true is sound. Never retype these to int.
|
|
77
|
+
{ name: 'available_on', type: 'string', facet: false, optional: true, sort: true },
|
|
78
|
+
{ name: 'created_at', type: 'string', facet: false, optional: true, sort: true },
|
|
79
|
+
{ name: 'updated_at', type: 'string', facet: false, optional: true, sort: true },
|
|
80
|
+
|
|
81
|
+
# DERIVED, not emitted by the presenter. Typesense silently returns ZERO
|
|
82
|
+
# results for range filters (<=, >) on string fields — verified against
|
|
83
|
+
# v30.2 — so availability filtering needs an int64. Derived at index time;
|
|
84
|
+
# 0 means "always available", which also removes the need for upstream's
|
|
85
|
+
# NOT EXISTS / IS NULL three-way clause.
|
|
86
|
+
{ name: 'available_on_i', type: 'int64', facet: false },
|
|
87
|
+
|
|
88
|
+
# DERIVED: when the index job read the product, in epoch ms.
|
|
89
|
+
# Stale-document removal deletes only documents stamped before
|
|
90
|
+
# its own read, so an older job finishing after a newer one
|
|
91
|
+
# cannot drop what the newer one wrote.
|
|
92
|
+
{ name: 'indexed_at', type: 'int64', facet: false, optional: true },
|
|
93
|
+
|
|
94
|
+
{ name: 'preorder', type: 'bool', facet: true, optional: true }
|
|
95
|
+
]
|
|
96
|
+
}
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
# The fields #definition declares that a live collection lacks, as
|
|
100
|
+
# their declared definitions, ready to PATCH onto it. A collection
|
|
101
|
+
# created by an earlier release gains them in place this way.
|
|
102
|
+
#
|
|
103
|
+
# @param live_fields [Array<Hash>] the `fields` of a retrieved collection
|
|
104
|
+
# @return [Array<Hash>]
|
|
105
|
+
def missing_fields(live_fields)
|
|
106
|
+
live_names = live_fields.map { |f| f['name'] }
|
|
107
|
+
declared_fields.reject { |f| live_names.include?(f[:name]) }
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
# Fields the live collection declares with a different type from
|
|
111
|
+
# #definition. Typesense cannot change a field's type in place, so
|
|
112
|
+
# these need the collection dropped and rebuilt.
|
|
113
|
+
#
|
|
114
|
+
# @param live_fields [Array<Hash>] the `fields` of a retrieved collection
|
|
115
|
+
# @return [Array<Hash>] `{ name:, live_type:, type: }` per field
|
|
116
|
+
def type_mismatches(live_fields)
|
|
117
|
+
live_types = live_fields.to_h { |f| [f['name'], f['type']] }
|
|
118
|
+
declared_fields.filter_map do |f|
|
|
119
|
+
live_type = live_types[f[:name]]
|
|
120
|
+
{ name: f[:name], live_type: live_type, type: f[:type] } if live_type && live_type != f[:type]
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
def declared_fields = definition(nil)[:fields]
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
end
|