Taher Talikoti
Taher Talikoti

Reputation: 21

GAE datastore composite filter not working

I am constructing a composite filter, It has OR and then AND filter operator when I set the sort order to this query it is not giving the correct result.

the query that I am trying to execute is { status == Initiated OR (Status == Failed AND failedCount <= 5 ) }. it actually executing the query without any error but the result is not exactly the correct. it retrieving only "status == failed AND failedCount <= 5 " not the status == initiated. and the order I need is "createdAt" field.

Filter initiatedFilter = new FilterPredicate("status", FilterOperator.EQUAL, "Initiated");

Filter failedFilter = new FilterPredicate("status", FilterOperator.EQUAL, "Failed");

Filter failedCountFilter = new FilterPredicate("failedCount", FilterOperator.LESS_THAN_OR_EQUAL, 5);

CompositeFilter failedCompositeFilter = CompositeFilterOperator.and(failedFilter, failedCountFilter);

CompositeFilter initaiedFailedFilter = CompositeFilterOperator.or(initiatedFilter, failedCompositeFilter);

Query query2 = new Query("JobInstance").setFilter(initaiedFailedFilter).addSort("failedCount", SortDirection.ASCENDING).addSort("createdAt", SortDirection.ASCENDING);

jobEntitesList = datastoreService.prepare(query2).asList(FetchOptions.Builder.withLimit(limit));

for (Entity jobEntity2 : jobEntitesList) {
    /* doing some task */
}

What I have observed is If a remove the addSort() its working fine. but for me sort is important. I want the result should be in "createdAt" order. If I remove failedCount then it is throwing some error.

Upvotes: 0

Views: 195

Answers (1)

Jim Morrison
Jim Morrison

Reputation: 2887

Or filters are implemented entirely in the client (like IN query filters: https://cloud.google.com/appengine/docs/standard/java/datastore/queries#filters). So your sort is happening in the client. It's likely that you are getting all of the results from the status == Failed AND FailedCount <= 5 query before getting any results from the status == Initiated.

From your comment, it's unclear what sort order you are looking for. As documented in the Restrictions on queries, the primary sort order must be on failedCount since that has an inequality filter.

Upvotes: 1

Related Questions