【问题标题】:Searchkick Aggregation ORSearchkick 聚合或
【发布时间】:2019-06-04 08:56:36
【问题描述】:

我正在使用 Searchkick 搜索产品。它工作得很好,但现在我正在添加过滤器。由于产品的性质,我总是想为选定的过滤器显示,所以它总是需要使用 OR,而不是使用 AND。

我正在构建这样的搜索:

    Product.search(
          "shower",
          fields: [:name]
          where: {showers_ids: [46,41], accessories_ids: [104, 102]},
          aggs: [: showers_ids, : accessories_ids, : bath_ids]
    )

这是尝试查询:

其中 IN (46 OR 41) AND (104 OR 102)

这将始终不带回任何产品,因为产品只属于一个类别。但我想展示所有类别的产品,所以它必须是这样的:

其中 IN (46 OR 41) OR (104 OR 102)

【问题讨论】:

标签: ruby-on-rails elasticsearch searchkick


【解决方案1】:

您应该能够在 where 子句中使用 or_ 参数来实现您想要的结果。 Searchkick docs 中有一个这样的例子,但是对于您的特定场景,以下应该可以工作:

Product.search(
  "shower",
  fields: [:name],
  aggs:   [:showers_ids, :accessories_ids, :bath_ids],
  where:  {
            _or: [
                   {showers_ids:     [46, 41]},
                   {accessories_ids: [104, 102]}
                 ]
          }
)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-20
    • 1970-01-01
    • 2015-10-31
    • 1970-01-01
    相关资源
    最近更新 更多