Alex Hamelin
Alex Hamelin

Reputation: 27

Google Datastore Query and Sort on different properties

I'm trying to sort data on a different field from the one used as a filter using Google's Datastore API (Java11). I get the following error

inequality filter property and first sort order must be the same

Example: Data:

Name: Toronto, Temperature: 30
Name: New York, Temperature: 70
Name: Montreal, Temperature: 10

Datastore Query:

Query<Entity> query = Query.newEntityQueryBuilder()
        .setKind("Cities")
                .addOrderBy(OrderBy.asc("Name"))
        .setFilter(CompositeFilter.and(
            PropertyFilter.ge("Temperature", 30)))
        .build();

Which should be the same as:

SELECT name,temperature FROM cities WHERE temperature >= '30' ORDER BY name;

Is there a way to execute such a simple query in Datastore?

Upvotes: 0

Views: 2273

Answers (1)

Jim Morrison
Jim Morrison

Reputation: 2887

No, you'll have to resort yourself. As documented in the note box at https://cloud.google.com/datastore/docs/concepts/queries#sort_orders:

"Note: Because of the way Datastore mode executes queries, if a query specifies inequality filters on a property and sort orders on other properties, the property used in the inequality filters must be ordered before the other properties."

Upvotes: 0

Related Questions