2016-08-23 75 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"

相关问题