Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The first sort property must be the same as the property to which the inequality filter is applied

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.

like image 492
code511788465541441 Avatar asked Oct 04 '22 09:10

code511788465541441


1 Answers

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

like image 97
tony m Avatar answered Oct 10 '22 10:10

tony m