简体   繁体   English

Rails - 使用模型方法过滤Searchkick结果

[英]Rails - Filtering Searchkick Results using a model method

I'm using Searchkick on a Rails ecommerce project. 我在Rails电子商务项目中使用Searchkick。 Users can search product listings. 用户可以搜索产品列表。

I have some conditions under which certain products are not displayed on the site (example, if inventory = 0, etc). 我有一些条件,在这些条件下某些产品不会在网站上显示(例如,如果库存= 0等)。

Based on the searchkick docs, I have my search method in the controller as @listings = Listing.search(params[:search]) . 基于searchkick文档,我将控制器中的搜索方法作为@listings = Listing.search(params[:search]) This works as intended. 这按预期工作。

In my listing model, I have the below method that defines which listings are ok to be displayed. 在我的列表模型中,我有以下方法来定义哪些列表可以显示。

class Listing < ActiveRecord::Base

    scope :listable, -> { 
    joins("INNER JOIN users
           ON users.id = listings.user_id
           AND users.hidelistings = 'f'") }

    def self.not_expired 
        listable.where('(listings.updated_at >= ? or user_id = ?) and inventory > ?', Date.current - 30.day, 24, 0)
    end

Based on the above, I want my searchkick method to say @listings = Listing.not_expired.search(params[:search]) but this doesn't work. 基于以上所述,我希望我的searchkick方法可以说@listings = Listing.not_expired.search(params[:search])但这不起作用。 How do I tell searchkick to only display results that meet the criteria? 如何告诉searchkick仅显示符合条件的结果?

I faced the same issue. 我遇到了同样的问题。 finally, had to add relevant fields and include that filtering logic into the Searchkick query. 最后,必须添加相关字段并将该过滤逻辑包含在Searchkick查询中。

however, that's really good. 然而,这真的很棒。 because it is super fast just search using elastic search. 因为它是超级快速的搜索使用弹性搜索。 or else, when data get large, run two queries in both servers is unnecessary overhead. 否则,当数据变大时,在两个服务器中运行两个查询是不必要的开销。

in my first solution, I manually pluck filtered id and passed that to elasticsearch where clause, which we hit many issues. 在我的第一个解决方案中,我手动选择过滤后的id并将其传递给elasticsearch where子句,我们遇到了很多问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM