ruby-on-rails - 如何将 "where not"子句与 searchkick 一起使用

标签 ruby-on-rails ruby postgresql elasticsearch searchkick

我将 Searchkick 与 Elasticsearch 结合使用来获取带有搜索词的产品。

我正在尝试向其中添加一个 WHERE NOT 子句,这样它就不会返回任何 regular_price 为空的产品。

@products = Product.search(
        search,
        where: {
          regular_price: {
            not: "null" # I have also tried nil, both won't work
          }
        },
        page: params[:page],
        per_page: per_page,
      )

搜索忽略我的 WHERE NOT 子句。

我也将不胜感激一些调试技巧。

编辑:

所以我尝试使用以下内容,但现在它只返回适合搜索的每个产品。忽略搜索的 WHERE NOT 部分。

@products = Product.search(
        search,
        where: {
          regular_price: {
            _not: "null" 
          }
        },
        page: params[:page],
        per_page: per_page,
      )

更新:

我尚未找到此问题的解决方案,因此我继续采用不同的方法。过滤掉 regular_price 为 0 的产品。

最佳答案

@products = Product.search(
    search,
    where: {
      regular_price: {
        _not: nil 
      }
    },
    page: params[:page],
    per_page: per_page,
  )

只需将"null"替换为nil,它就会过滤掉没有regular_price的产品。

关于ruby-on-rails - 如何将 "where not"子句与 searchkick 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62225170/

相关文章:

ruby-on-rails - 为整个 Rails 应用程序更改 CSV 的列分隔符

ruby-on-rails - RoR 中的简单舍入在一个非常奇怪的实例中不起作用

ruby-on-rails - 有没有办法使 before_save 成为条件?

ruby-on-rails - 如何使用form_for将参数传递给 Controller ​​ruby on rails

ruby-on-rails - 使用 Active Record Reputation System gem,当我按投票排序时不会发生排序

ruby - 验证时间向前或向后一小时

Ruby 线程与 Watir

sql - 对多个表中的多个值求和

postgresql - PostgreSQL COPY 可以从远程位置读取 CSV 吗?

node.js - Sequelize 连接到 heroku postgres 数据库时卡住了