2017-02-11 47 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

相關問題