使用Google App Engine排序问题

B.E*_*.E. 1 python google-app-engine google-cloud-datastore

我使用以下类来存储一些数据:

class NewsArticle(db.Model):
    score = db.FloatProperty(default=0.0)
    date_scored = db.DateTimeProperty()
    ...
Run Code Online (Sandbox Code Playgroud)

我需要做的是让那些在某个时间段内获得最高分的NewsArticle实体(例如获得今天或上周的最高得分数据实体).

我尝试了以下方法:

query = db.GqlQuery('SELECT * FROM NewsArticle WHERE date_created > DATETIME(:year, :month, :day, 0, 0, 0) ORDER BY score DESC', year=date.selected_year, month=date.selected_month, day=date.selected_day)
Run Code Online (Sandbox Code Playgroud)

但这不起作用,因为数据存储区需要

第一个排序属性必须与不等过滤器属性相同

我考虑将所有NewsArticle实体放在特定的时间范围内,然后在我的应用程序中进行分数排序,但我期待大量的结果,因此内存排序不会有效.

我的问题可能还有哪些其他解决方案?

jbo*_*chi 5

你可以:

  1. 仅按时间帧过滤并按内存中的分数排序,或者

  2. 如果您可以将时间范围限制为整天和几周,请在模型中包含其他属性,以将整周保存为整数,将日期保存为a DateProperty并对其进行简单的相等检查.

编辑:要了解更多信息,请查看查询限制