【问题标题】:Elasticsearch-dsl sort, find last X entriesElasticsearch-dsl排序,找到最后X个条目
【发布时间】:2017-03-13 07:22:25
【问题描述】:

我正在尝试在我的索引/文档类型中查找最后 30 个条目

我什么都没试过,我完全没有想法!

我目前的方法是找到过去 5 分钟内的所有结果,然后过滤结果并抓取最后 30 个条目,但这比正确的方法要慢。

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

我试过了

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

【问题讨论】:

    标签: python elasticsearch-dsl elasticsearch-py


    【解决方案1】:

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

    您错误地指定了您的sort

    【讨论】:

    • 第二个选项有错字吗? s = s.sort({"timestamp" : {"order" : "desc"}})?
    【解决方案2】:

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

    #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()
    

    【讨论】:

      【解决方案3】:

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

      时间戳将按升序排序。

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

      时间戳将按降序排序。

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

      【讨论】:

        猜你喜欢
        • 2011-01-04
        • 2012-08-12
        • 2021-09-23
        • 1970-01-01
        • 2017-12-26
        • 1970-01-01
        • 1970-01-01
        • 2021-12-13
        • 1970-01-01
        相关资源
        最近更新 更多