1

我有一个关于 pg_search 的问题:我在 solidus 2.9 上安装了 gem,并添加了一个 product_decorator.rb 模型,如下所示:

Spree::Product.class_eval do
  include PgSearch::Model

  pg_search_scope :search, against: [:name, :description, :meta_description, :meta_keywords],
    using: {tsearch: {dictionary: "english"}}

end

它在 Rails 控制台中运行良好。

当我在solidus的前端使用搜索字段时,如何使它工作?我尝试将其添加到产品控制器,但似乎无法使其工作。谢谢!

更新

所以在 kennyadsl 的评论之后,我有这个:

#lib/mystore/products_search.rb

module MyStore
  class ProductSearch < Spree::Core::Search::Base

    def retrieve_products
      Spree::Product.pg_search
    end
  end
end
#models/spree/product_decorator.rb

Spree::Product.class_eval do
  include PgSearch::Model

  pg_search_scope :search, against: [:name, :description, :meta_description, :meta_keywords], using: {tsearch: {dictionary: "english"}}

  def self.text_search(keywords)
        if keywords.present?
      search(keywords)
    else
      Spree::Product.all
    end
  end       

end
#controllers/products_controller.rb

def index
  @searcher = build_searcher(params.merge(include_images: true))
  @products = @searcher.retrieve_products(params)
end
4

1 回答 1

1

默认情况下,产品搜索是通过Spree::Core::Search::Base类执行的,但它是可配置的,因此您可以创建自己的继承自该类的类:

module Spree
  module Core
    module Search
      class PgSearch < Spree::Core::Search::Base
        def retrieve_products
          PgSearch.multisearch(...)
        end
      end
    end
  end
end

要查看该类中可用的内容,您可以在此处参考原始实现:

https://github.com/solidusio/solidus/blob/eff22e65691a64d2ce0bf8a919d8456010360753/core/lib/spree/core/search/base.rb

添加自己的逻辑后,您可以使用新类执行搜索,方法是将此行添加到config/initializers/spree.rb

Spree::Config.searcher_class = Spree::Core::Search::PgSearch

更新

在与 Ignacio 反复讨论之后,这是一个在 Solidus 店面中使用 PgSearch 执行基本搜索的工作版本:

# app/models/spree/product_decorator.rb

Spree::Product.class_eval do
  include PgSearch::Model

  pg_search_scope :keywords, 
    against: [:name, :description, :meta_description, :meta_keywords],
    using: { tsearch: { dictionary: "english" } }
end
# lib/spree/core/search/pg_search.rb

module Spree
  module Core
    module Search
      class PgSearch < Spree::Core::Search::Base
        def retrieve_products
          Spree::Product.pg_search_by_keywords(@properties[:keywords])
        end
      end
    end
  end
end
# config/initializers/spree.rb

Spree::Config.searcher_class = Spree::Core::Search::PgSearch
于 2019-09-05T16:05:10.223 回答