java.lang.IllegalArgumentException: The first sort property must be the same as the property to which the inequality filter is applied. In your query the first sort property is vote but the inequality filter is on createdDate
I get this error when I try to run the code below. Is there a work around this problem?
Query query = new Query("Post");
long timeAgo = now - timeLimit;
Filter categoryFilter = new FilterPredicate("postCategory", FilterOperator.EQUAL, category);
Filter createdDateFilter = new FilterPredicate("createdDate", FilterOperator.GREATER_THAN, timeAgo);
Filter compositeFilter = CompositeFilterOperator.and(categoryFilter, createdDateFilter);
query.setFilter(compositeFilter);
query.addSort("vote", SortDirection.DESCENDING);
List<Entity> postsEntityList = datastore.prepare(query).asList(FetchOptions.Builder.withOffset(offset).limit(limit));
The code above is trying to reteive a list of Post entities based on their category and who were created within the last 24 hours. then sort them by how many votes they have. it also has limit and offset for pagination.
To avoid the error, the only option for you is to add multiple sort orders like below with createdDate as the first sort order:
.addSort("createdDate", SortDirection.ASCENDING)
.addSort("vote", SortDirection.DESCENDING);
But this will have the limitation that the results will not be properly sorted with respect to your second property and so you will have do additional sorting at your client side. Please read this earlier post to understand this behaviour
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With