2016-12-18 121 views
3

我有应用背景过滤器嵌套显著方面聚集困难,bg_count始终为0Elasticsearch嵌套显著方面的聚集背景过滤

我有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。

感谢

+1

您似乎正在触碰[后续问题](https://github.com/elastic/elasticsearch/issues/20101),在后台过滤器中,您需要按顺序“返回”父级上下文根据父文档的字段定义背景过滤器。在那一点你需要一个'reverse_nested'查询,但是这不存在。避免这种情况的一种方法是将'app_id'字段添加到嵌套文档中,以便您可以在后台过滤器上下文中简单地使用它。 – Val

+0

@Val谢谢,这似乎是正确的答案。如果你想要它作为答案,我会接受它。 – Orr

回答

1

你似乎击中following issue凡在你的后台滤波器,你需要“返回”到父上下文,以便基于父文档的字段定义你的背景过滤器。

您需要一个reverse_nested查询在那一点,但不存在。

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