Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

SD-1446 use pg_search for full text search of porduct names when pg_search is available. (#11416)

Kacper Mękarski1ef46751358d
diff --git a/core/app/models/concerns/spree/product_scopes.rb b/core/app/models/concerns/spree/product_scopes.rb
index 5caceb5cd1..711b0b30ee 100644
--- a/core/app/models/concerns/spree/product_scopes.rb
+++ b/core/app/models/concerns/spree/product_scopes.rb
@@ -277,6 +277,26 @@ module Spree
group('spree_products.id').joins(:taxons).where(Taxon.arel_table[:name].eq(name))
end
+ # .search_by_name
+ if defined?(PgSearch)
+ include PgSearch::Model
+
+ if defined?(SpreeGlobalize)
+ pg_search_scope :search_by_name, associated_against: { translations: :name }, using: :tsearch
+ else
+ pg_search_scope :search_by_name, against: :name, using: :tsearch
+ end
+ else
+ def self.search_by_name(query)
+ if defined?(SpreeGlobalize)
+ joins(:translations).order(:name).where("LOWER(#{Product::Translation.table_name}.name) LIKE LOWER(:query)", query: "%#{query}%").distinct
+ else
+ where("LOWER(#{Product.table_name}.name) LIKE LOWER(:query)", query: "%#{query}%")
+ end
+ end
+ end
+ search_scopes << :search_by_name
+
def self.price_table_name
Price.quoted_table_name
end
diff --git a/core/app/models/spree/product.rb b/core/app/models/spree/product.rb
index 5d7ce03b99..1e589c9985 100644
--- a/core/app/models/spree/product.rb
+++ b/core/app/models/spree/product.rb
@@ -264,14 +264,6 @@ module Spree
where conditions.inject(:or)
end
- def self.search_by_name(query)
- if defined?(SpreeGlobalize)
- joins(:translations).order(:name).where("LOWER(#{Product::Translation.table_name}.name) LIKE LOWER(:query)", query: "%#{query}%").distinct
- else
- where("LOWER(#{Product.table_name}.name) LIKE LOWER(:query)", query: "%#{query}%")
- end
- end
-
# Suitable for displaying only variants that has at least one option value.
# There may be scenarios where an option type is removed and along with it
# all option values. At that point all variants associated with only those