【问题标题】:Elasticsearch nested significant terms aggregation with background filterElasticsearch 使用背景过滤器嵌套重要术语聚合
【发布时间】:2023-03-28 12:54:01
【问题描述】:

我很难将背景过滤器应用于嵌套的重要术语聚合,bg_count 始终为 0。

我正在索引具有 ID 和时间戳的文章视图,并且在单个索引上具有多个应用程序。我希望前景和背景设置与同一应用程序相关,因此我尝试在 boo 查询和背景过滤器中的 app_id 字段上应用术语过滤器。 article_views 是一个嵌套对象,因为我还希望能够在 timestamp 上使用范围过滤器查询视图,但我还没有做到这一点。

映射:

    {
    "article_views": {
        "type": "nested",
        "properties": {
            "id": {
                "type": "string",
                "index": "not_analyzed"
            },
            "timestamp": {
                "type": "date",
                "format": "strict_date_optional_time||epoch_millis"
            }
        }
    },
    "app_id": {
        "type": "string",
        "index": "not_analyzed"
    }
}

查询:

  {
   "aggregations": {
      "articles": {
         "nested": {
            "path": "article_views"
         },
         "aggs": {
            "articles": {
               "significant_terms": {
                  "field": "article_views.id",
                  "size": 5,
                  "background_filter": {
                     "term": {
                        "app_id": "17"
                     }
                  }
               }
            }
         }
      }
   },
   "query": {
      "bool": {
         "must": [
            {
               "term": {
                  "app_id": "17"
               }
            },
            {
               "nested": {
                  "path": "article_views",
                  "query": {
                     "terms": {
                        "article_views.id": [
                           "1",
                           "2"
                        ]
                     }
                  }
               }
            }
         ]
      }
   }
}

正如我所说,在我的结果中,bg_count 始终为 0,这让我很担心。如果重要术语在其他未嵌套的字段上,background_filter 可以正常工作。

Elasticsearch 版本为 2.2。

谢谢

【问题讨论】:

  • 您似乎正在点击following issue,在您的背景过滤器中,您需要“返回”到父上下文,以便根据父文档的字段定义您的背景过滤器.那时您需要一个reverse_nested 查询,但这并不存在。避免这种情况的一种方法是将app_id 字段添加到嵌套文档中,以便您可以在后台过滤器上下文中简单地使用它。
  • @Val 谢谢,这似乎是正确的答案。如果您想要正确的答案,我会接受。

标签: elasticsearch


【解决方案1】:

您似乎正在点击following issue,在您的背景过滤器中,您需要“返回”到父上下文,以便根据父文档的字段定义您的背景过滤器。

此时您需要一个reverse_nested 查询,但这并不存在。

避免这种情况的一种方法是将app_id 字段添加到您的嵌套文档中,以便您可以在后台过滤器上下文中简单地使用它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-21
    • 1970-01-01
    • 2018-01-30
    • 2014-12-31
    • 1970-01-01
    • 1970-01-01
    • 2021-04-19
    • 2015-11-14
    相关资源
    最近更新 更多