首页 文章

过滤elasticsearch嵌套模型上的空值

提问于
浏览
0

我有 Product 模型和 Sku 模型, Product 有多个 Skus .

在elasticsearch中,我可以通过 Sku 定价过滤 Products ,其查询结构如下:

query:
  bool:
    must: [
      nested:
        path: 'skus'
        query:
          bool:
            must: [
              range:
                baseline_price:
                  gte: opts.min_price
                  lte: opts.max_price
            ]
    ]

但是,我在 Sku 模型上为 lengthwidthheight 值执行类似操作时遇到了麻烦 .

我想找到所有没有 lengthwidthheightSkus . 我正在尝试这样的事情:

query:
  bool:
    must: [
      nested:
        path: 'skus'
        query:
          bool:
            should: [
              exists: { field: 'length' }
              exists: { field: 'width' }
              exists: { field: 'height' }
            ]
    ]

只是为了返回任何有用的东西,但任何组合我都会抛出错误 . 在上面的情况下,它抛出 No query registered for [exists]

我看了documentation for null values in elasticsearch,但仍然没有运气 .

我该怎么写这个查询?

1 回答

  • 0

    我能够让它有点像这样工作:

    query:
      bool:
        must: [
          nested:
            path: 'skus'
            query:
              constant_score:
                filter:
                  bool:
                    must: [
                      missing: { field: 'skus.width' }
                    ]
        ]
    

    这将找到缺少 widthSkus . 但是,不幸的是,添加额外的 lengthheight 字段表现为 OR 而不是 AND ,我不确定这是否是elasticsearch的错误 .

    这足以满足我们现在的需求,但仍然不确定是否有方法可以过滤多个缺少的字段 .

相关问题