【问题标题】:How to use a "where not" clause with searchkick如何在 searchkick 中使用“where not”子句
【发布时间】:2020-09-25 05:46:15
【问题描述】:

我正在使用带有 Elasticsearch 的 Searchkick 来获取带有搜索词的产品。

我正在尝试向其中添加 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,
      )

UPDATE:

我还没有找到解决这个问题的方法,我继续采用不同的方法。通过过滤掉 regular_price 为 0 的产品。

【问题讨论】:

  • 根据the README,“where not”查询的语法是:where: { _not: { regular_price: 'null' } }。你试过了吗?
  • 我也对您的问题感到有些困惑——您是要排除价格为零的产品还是零价格的产品?零和空不是一回事。 (例如,null 可能表示“联系经销商获取报价”,而0 可能表示“免费”。)
  • 我的产品有一个 regular_price 值和一个 price 值。我想从搜索中排除 regular_price 为空值的产品,因为这些产品仅适用于商业客户。我会尽快使用_not。
  • 我的意思是您帖子的第二行说“...它不会返回任何具有 0 的常规价格的产品”,但您的代码表明您想要排除 null 值。我不知道你真正想要哪个,因为你的问题前后矛盾。
  • 是的,请原谅我的困惑。我想排除 regular_price 值为 null 的产品。

标签: ruby-on-rails ruby postgresql elasticsearch searchkick


【解决方案1】:
@products = Product.search(
    search,
    where: {
      regular_price: {
        _not: nil 
      }
    },
    page: params[:page],
    per_page: per_page,
  )

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

【讨论】:

    【解决方案2】:

    还没有找到答案。因为我想用这个查询过滤掉的产品并不多,所以我只是在渲染时过滤掉它们。

    【讨论】:

      猜你喜欢
      • 2012-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-17
      • 1970-01-01
      • 2011-12-23
      相关资源
      最近更新 更多