Katedral Pillon
Katedral Pillon

Reputation: 14864

objectify composite filter implementation or hack

It would appear that Objectify does not support the Datastore's CompositeFilter. But objectify does take Filters. So for now how do I implement a composite AND filter in objectify? I am moving from Datanucleus and I need the query

"SELECT f.healthy FROM Food f WHERE  f.fan = :userid AND flavor = : flavor";

So the type is Food.class and the fields are fan and flavor

Upvotes: 0

Views: 753

Answers (2)

tj-recess
tj-recess

Reputation: 1809

You should create a composite filter and pass that to ofy query variant which accepts filter.

CompositeFilterOperator.and(
     FilterOperator.EQUAL.of("f.fan", userId),
     CompositeFilterOperator.or(
         FilterOperator.EQUAL.of("flavor", flavor),
         FilterOperator.EQUAL.of("color", color)));

However, implementing OR filters have its own side effects; for e.g. you can't use cursors.

Ref of above code: https://cloud.google.com/appengine/docs/java/javadoc/com/google/appengine/api/datastore/Query.CompositeFilter

Upvotes: 5

jirungaray
jirungaray

Reputation: 1674

Queries are quite simple:

List<Food> foods = ofy().load().type(Food.class).filter("fan", xxx).filter("flavor", xxx).list();

make sure all fields you plan to filter/sort on are marked with @Index before persisting the entities.

Upvotes: 0

Related Questions