开发者

JDO Query ordering issue

开发者 https://www.devze.com 2023-03-08 01:48 出处:网络
I have a JDO query which filters on two properties of a \"Person\" entity, lets call them \"age\" and开发者_开发知识库 \"height\". I wish to order the results by the Persons \"score\". However, due to

I have a JDO query which filters on two properties of a "Person" entity, lets call them "age" and开发者_开发知识库 "height". I wish to order the results by the Persons "score". However, due to the restrictions App Engine imposes I firstly have to order by age or height. So I currently have

query.setOrdering("age desc, score asc");

I am really not interested in the results being ordered by age, I just have to include that for the query to work (as I have filtered on age), I really just need the results ordered by score.

Is there a way to get the results ordered by score and effectively ignore the age ordering?

Thanks


You can't order by score first since no index could support that query.

The solution/workaround I've seen recommended is to pick a few pre-defined age ranges, pre-compute each Persons membership to those ranges, and then use an equality filter on that membership so you can sort by score.

For example, if you picked "<13", "13-18", and "18+" as your age ranges, you'd add three boolean properties to each Person. Then you could do a query like "select * from Person where isUnder13 = true order by score". I guess in this case since membership is mutually exclusive you could use a single enum property and search for something like ageRange = 'UNDER_13', which would cut down on the number of indexes built.

Of course, this means you can't have an arbitrary search function... but having fewer choices isn't always bad for user experience.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号