2016-12-09 36 views
0

我對ElasticSearch非常新穎。任何人都可以幫我找到查詢。 我有以下記錄在我的elasticsearchElasticsearch查詢以最近的日期形式獲得不同的結果一個月

Name Work   Time Stamp 
-------------------------------------------- 
Steve, eating,  2016-11-12 05:36:40 
Steve, sleeping, 2016-11-12 06:14:50 
Steve, going,  2016-11-12 07:21:22 
Steve, driving, 2016-11-12 08:20:10 
Steve, reading, 2016-11-12 09:24:30 
James, eating,  2016-11-12 11:36:40 
James, sleeping, 2016-11-12 05:14:50 
James, going,  2016-11-12 08:21:22 
James, driving, 2016-11-12 10:20:10 
James, reading, 2016-11-12 09:24:30 
Crag, sleeping, 2016-05-12 09:24:30 

我需要以下數據

Name Work  Time Stamp 
------------------------------------------- 
Steve, reading, 2016-11-12 09:24:30 
James, eating,  2016-11-12 11:36:40 

回答

0

這是對指數非常重要以正確的方式中的數據:因爲你要排序

1)日期,你必須「說」到彈性的那個特定的領域是日期字段,所以你需要映射數據:

PUT stack 
{ 
    "mappings": { 
    "stack": { 
     "properties": { 
     "time": { 
       "type": "date" 
      } 
     } 
    } 
    } 
} 

2)然後插入數據:

POST /_bulk 
{"index":{"_index":"stack","_type":"stack"}} 
{"name":"Steve","work":"eating","Time":"2016-11-12"} 
{"index":{"_index":"stack","_type":"stack"}} 
{"name":"Steve","work":"sleeping","Time":"2016-11-13"} 
{"index":{"_index":"stack","_type":"stack"}} 
{"name":"James","work":"eating","Time":"2016-11-12"} 
{"index":{"_index":"stack","_type":"stack"}} 
{"name":"James","work":"sleeping","Time":"2016-05-15"} 

3)然後搜索:

GET stack/_search 
{ 
    "size": 0, 
    "aggs": { 
     "top-tags": { 
      "terms": { 
       "field": "name", 
       "size": 3 
      }, 
      "aggs": { 
       "top_tag_hits": { 
        "top_hits": { 
         "sort": [ 
          { "Time": { "order": "desc" }} 
         ], 
         "size" : 2 
        } 
       } 
      } 
     } 
    } 
} 
+0

我編輯我的回答,請看看 – Lax

相關問題