2016-08-23 123 views
0

我是Elastic Search的新手。我想查找最近訪問的前10個唯一doc_id。如何根據ElasticSearch中的viewing_timestamp對桶結果進行排序?

我已經對doc_id進行了第一次聚合,並添加了子聚合來對每個組進行排序並獲得單個結果。現在我想分類這個桶。 我無法根據view_timestamp對存儲桶的結果進行排序。我如何在首次彙總時添加訂單?

我已經嘗試了其他解決堆棧溢出,但它不適合我。任何人都可以幫我解決這個問題嗎?


查詢

{ 
    "query": { 
     "constant_score": { 
      "filter": { 
       "term": { "username": "[email protected]" } 
      } 
     } 
    }, 

    "size":0, 
    "aggs":{ 
     "title": { 
      "terms": { 
       "field": "doc_id", 
       "size":0 
      } 
      , 
      "aggs": { 
       "top": { 
        "top_hits": { 
         "sort": [ 
         { 
          "viewed_timestamp": { 
           "order": "desc" 
          } 
         } 
        ], 
         "size": 1 

        } 
       } 
      }   

     } 

    }  

} 

桶結果:

{ 
    "aggregations": { 
     "title": { 
      "doc_count_error_upper_bound": 0, 
      "sum_other_doc_count": 0, 
      "buckets": [{ 
       "key": "b003", 
       "doc_count": 3, 
       "top_tag_hits": { 
        "hits": { 
         "total": 3, 
         "max_score": null, 
         "hits": [{ 
          "_index": "visitedData", 
          "_type": "userdoc", 
          "_id": "AVak51Sp", 
          "_score": null, 
          "_source": { 
           "viewed_timestamp": "20160819T152359", 
           "content_type": "bp", 
           "title": "Data print", 
           "doc_id": "BP003" 
          }, 
          "sort": [ 
           1471620239000 
          ] 
         }] 
        } 
       } 
      }, { 
       "key": "bp004", 
       "doc_count": 3, 
       "top_tag_hits": { 
        "hits": { 
         "total": 3, 
         "max_score": null, 
         "hits": [{ 
          "_index": "visitedData", 
          "_type": "userdoc", 
          "_id": "AVak513Y8G", 
          "_score": null, 
          "_source": { 
           "viewed_timestamp": "20160819T152401", 
           "content_type": "bp", 
           "title": "Application Print", 
           "doc_id": "BP004" 
          }, 
          "sort": [ 
           1471620241000 
          ] 
         }] 
        } 
       } 
      }] 
     } 
    } 
} 

回答

0

這是怎麼一回事,因爲你的view_timestap類型不是日期,它是timesatmp。您應該將該字段更改爲日期格式,如: "updateTime": "2017-01-12T21:28:49.562065"

相關問題