4

I'm trying to find the last 30 entries into my index/doc type

I've tried nothing and I'm all out of ideas!

My current approach I find all the results over the last 5 minutes, then filter through the results and grab out the last 30 entries, but this is slower than the correct approach.

s = Search(using=es, index="history", doc_type=p)
   .filter('range', timestamp={'gte': mins})
   .extra(size=1000)

And I've tried

s = Search(using=es, index="history", doc_type=p)
   .sort("timestamp", {'order': "desc"})
   .extra(size=30)
4

3 回答 3

3

按降序排序的正确方法timestamps = s.sort('-timestamp')or s = s.sort({"price" : {"order" : "desc"}})

您指定的sort不正确。

于 2017-03-16T06:52:29.753 回答
1

检查doctype中是否启用了时间戳。如果启用,则只有我们可以在 elasticsearch dsl中使用时间戳。

#Add Query
s = Search(using=es, index="history", doc_type=p).query("query_string", query="*").sort("timestamp", {'order': "desc"})

#To specify the from/size parameters i.e for first 30 entries
s=s[0:30]

#Call elasticsearch
s.execute()
于 2017-03-13T09:03:39.630 回答
1

https://elasticsearch-dsl.readthedocs.io/en/latest/api.html?highlight=sort#elasticsearch_dsl.Search.sort

时间戳将按升序排序。

s = Search().sort('timestamp')

时间戳将按降序排序。

s = Search().sort('-timestamp')
于 2021-01-08T04:18:16.520 回答