Working around inequality and sort order limitations

1,814 views
Skip to first unread message

Dennis Lo

unread,
Mar 7, 2011, 6:18:21 PM3/7/11
to objectify-appengine
Hi,

I'm trying to do a query using inequalities and sort ordering.
Essentially I want to obtain entities within a certain date range
sorted by descending order.

Assume I have a Game kind with properties releaseDate (date format
yyyyMMdd) and rating. I would like to do the following:

Query<Game> q = ofy().query(Game.class);

q = q.filter("releaseDate >=", 201010101).filter("releaseDate <=",
201110101).order("-rating");

List<Game> gameList = q.list();

Now I understand that inequality filters must be specified first in
any sort ordering and so this does not work. Hence, I'm getting an
error like: "BadArgumentError: First ordering property must be the
same as inequality filter property, if specified for this query;
received rating, expected releaseDate"

Also it's explained here:
http://code.google.com/appengine/docs/python/datastore/queries.html#Restrictions_on_Queries

Any other ideas on how to work around this limitation?

Jeff Schnitzer

unread,
Mar 7, 2011, 6:43:51 PM3/7/11
to objectify...@googlegroups.com
Fetch all the results and sort them in memory? That's typically the
way an RDBMS would do it...

Jeff

Dennis Lo

unread,
Mar 7, 2011, 7:41:22 PM3/7/11
to objectify-appengine
Also I forgot to mention that the Game kind has a lot of entries.
So if I fetch all the results first and sort in memory, then that is a
waste when all I wanted was just a few results?

However, if the filter is performed first, then the number of results
returned wouldn't be too large.
Then the sorting should take too long.

For example:

Query<Game> q = ofy().query(Game.class);

q = q.filter("releaseDate >=", 201010101).filter("releaseDate <=",
201110101);

List<Game> unsortedGameList = q.list();

//Perform sort on unsortedGameList by rating... and return sorted list
of games.

Hmm, so the work around would essentially be filter first and then
sort the List<Game> in memory.
I think I'll have use the Collections.sort() method of doing the
sorting.
Something like http://www.albeesonline.com/blog/2008/10/16/sorting-an-arraylist-of-objects/
will allow me to sort by the rating property.

On Mar 7, 11:43 pm, Jeff Schnitzer <j...@infohazard.org> wrote:
> Fetch all the results and sort them in memory?  That's typically the
> way an RDBMS would do it...
>
> Jeff
>
> On Mon, Mar 7, 2011 at 3:18 PM, Dennis Lo <lo.den...@gmail.com> wrote:
> > Hi,
>
> > I'm trying to do a query using inequalities and sort ordering.
> > Essentially I want to obtain entities within a certain date range
> > sorted by descending order.
>
> > Assume I have a Game kind with properties releaseDate (date format
> > yyyyMMdd) and rating. I would like to do the following:
>
> > Query<Game> q = ofy().query(Game.class);
>
> > q = q.filter("releaseDate >=", 201010101).filter("releaseDate <=",
> > 201110101).order("-rating");
>
> > List<Game> gameList = q.list();
>
> > Now I understand that inequality filters must be specified first in
> > any sort ordering and so this does not work. Hence, I'm getting an
> > error like: "BadArgumentError: First ordering property must be the
> > same as inequality filter property, if specified for this query;
> > received rating, expected releaseDate"
>
> > Also it's explained here:
> >http://code.google.com/appengine/docs/python/datastore/queries.html#R...
Reply all
Reply to author
Forward
0 new messages