2017-02-11 39 views
0

我有一些很多字段的文档,我想找到在某个小时后插入的文档的数量,这可以通过使用范围查询来完成,但在所有文档字段中,我希望只显示时间戳字段。我可以使用聚合内的_source字段吗?

我用下面的查询

{ 
     "_source": [ 
     "fields.Timestamp" 
     ], 
     "aggs": { 
     "last_5_mins": { 
      "filter": { 
      "range": { 
       "fields.Timestamp": { 
       "gt": "2017-02-10T10:07:04.4367673Z" 
       } 
      } 
      } 
     } 
     } 
    } 

当我看到输出我没有得到的时间戳匹配的文档,而我得到了不与我的查询相匹配的文件。

我的输出:

"hits": { 
    "total": 6, 
    "max_score": 1, 
    "hits": [ 
     { 
     "_index": "matrix", 
     "_type": "neo", 
     "_id": "5", 
     "_score": 1, 
     "_source": { 

     } 
     }, 
     { 
     "_index": "matrix", 
     "_type": "neo", 
     "_id": "2", 
     "_score": 1, 
     "_source": { 
      "fields": { 
      "Timestamp": "2017-02-10T10:07:04.4367673Z" 
      } 
     } 
     }, 
     { 
     "_index": "matrix", 
     "_type": "neo", 
     "_id": "4", 
     "_score": 1, 
     "_source": { 
      "fields": { 
      "Timestamp": "2017-02-10T10:07:04.4836472Z" 
      } 
     } 
     }, 
     { 
     "_index": "matrix", 
     "_type": "neo", 
     "_id": "6", 
     "_score": 1, 
     "_source": { 

     } 
     }, 
     { 
     "_index": "matrix", 
     "_type": "neo", 
     "_id": "1", 
     "_score": 1, 
     "_source": { 
      "fields": { 
      "Timestamp": "2017-02-10T10:07:04.4367673Z" 
      } 
     } 
     }, 
     { 
     "_index": "matrix", 
     "_type": "neo", 
     "_id": "3", 
     "_score": 1, 
     "_source": { 
      "fields": { 
      "Timestamp": "2017-02-10T10:07:12.1147415Z" 
      } 
     } 
     } 
    ] 
    }, 
    "aggregations": { 
    "last_5_mins": { 
     "doc_count": 2 
    } 
    } 
} 

我怎样才能得到匹配的文件的时间戳? 谢谢..

回答

0

你并不需要使用该聚合,只需将您的range中的查询部分,你是好:

{ 
    "_source": [ 
     "fields.Timestamp" 
    ], 
    "query": { 
     "bool": { 
     "filter": { 
      "range": { 
       "fields.Timestamp": { 
        "gt": "2017-02-10T10:07:04.4367673Z" 
       } 
      } 
     } 
     } 
    } 
} 
+0

Thanks..May我知道为什么你没有用过滤器吗?因为没有过滤器,搜索'fields.Timestamp'将会是一个代价高昂的搜索。而且我也想要文件的数量。 – Seeker

+0

那里,现在使用过滤器。只要知道不使用过滤器并不等于“昂贵”,还有许多其他因素发挥作用。文档的数量是响应中的'hits.total'值。 – Val

+0

谢谢..瓦尔它帮助..可能我知道你正在采取什么因素? – Seeker

相关问题