GAE datastore query with filter and sort using objectify

后端 未结 3 2013
再見小時候
再見小時候 2021-01-15 04:40

I am trying to query the datastore for the top 100 users in terms of points scored, who have logged on in the past week (the date field).

List use         


        
3条回答
  •  太阳男子
    2021-01-15 05:23

    What you have observed is that standard expected behaviour for index based queries as used by app engine. While filtering, if you apply inequality filter(which can be used only on one property in a query) , then when you have multiple sort orders, the first ordering will be for that property and then further ordering can be based on other properties. For doing a query based on an inequality filter for date and sorting by points, datastore will be using and index like below, where the date property is in ascending or descending order:

    day 1 -  100
    day 2 -  30
    day 2 - 90
    day 2- 10
    day 3 - 50
    day 4 - 40
    day 5 - 60 
    

    Now if you do a query with inequality filter of day > day1 , then the query will search the above index and return below results, which will already be sorted in terms of the date, even if you dont mention explicitly:

    day 2 -  30
    day 2 - 90
    day 2- 10
    day 3 - 50
    day 4 - 40
    day 5 - 60
    

    Now if you are doing a query with inequality filter on date and add a sort order on points, then it will be like applying an additional sort on the above result which is already sorted by date. That is why you are forced to explicitly mention date as first sort order( because its already present by default) and then mention points as second sort order. The result will like below. See the sorting done for day 2:

    day 2 -  10
    day 2 - 30
    day 2- 90
    day 3 - 50
    day 4 - 40
    day 5 - 60
    

    So if you want to achieve your logic, you need to retrieve data from app engine and do some additional sorting like below:

    1, Fetch with date inequality filter and then in your clientside do a proper sorting based on points to get the top 100.

    2, Fetch the top results(around 300) based on a descending order index for points and then filter them based on the date in your clientside to get the desired 100.

提交回复
热议问题