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)
Basil Wallace
  • 51
  • 1
  • 5

3 Answers3

2

the proper way to sort on timestamp in descending order is either s = s.sort('-timestamp') or s = s.sort({"price" : {"order" : "desc"}}).

You were specifying your sort incorrectly.

Honza Král
  • 2,672
  • 10
  • 10
1

Check if timestamp is enabled in doctype.If it's enable, then only we can use timestamp in 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()
Ajay Singh
  • 1,056
  • 12
  • 17
1

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

timestamp will sort in ascending order.

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

timestamp will sort in descending order.

s = Search().sort('-timestamp')
Hasan Khan
  • 207
  • 3
  • 10