Objectify query filter by List of keys that have a parent
Asked Answered
B

0

0

I would like to create an Api method via Google App Engine (Objectify) that returns a CollectionResponse of the posts of the people that I am following, sorted by date descending.

I have an Entity Post and Entity Profile both of which have Long id as their key.

ThePost Entity has the following property specifying it has a Parent:

@Parent
private Key<Profile> profileKey;

The Profile Entity has the following property storing a List of id's of the people the profile is following:

// Ids of the profiles this person follows
private List<Long> following = new ArrayList<>(0);

I was thinking then, I could do something like this:

    List<Long> idsProfile = profile.getFollowing();

    Query<Goal> query = ofy().load().type(Post.class)
                    .order("-createdDate") 
                    .filterKey("in", idsProfile) 
                    .limit(Constants.DEFAULT_LIST_LIMIT);

            if (cursor != null) {
                query = query.startAt(Cursor.fromWebSafeString(cursor));
            }

            QueryResultIterator<Goal> queryIterator = query.iterator();
            List<Post> postList = new ArrayList<Post>(Constants.DEFAULT_LIST_LIMIT);
            while (queryIterator.hasNext()) {
                postList.add(queryIterator.next());
            }

            return CollectionResponse.<Post>builder().setItems(postList).setNextPageToken(queryIterator.getCursor().toWebSafeString()).build();

What I am doing here is getting a List of all the id's of Profile's that someone is following and trying to query and filter on that List to only return the Post's by those users.

But I am getting this error:

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "badRequest",
    "message": "java.lang.IllegalArgumentException: __key__ filter value must be a Key"
   }
  ],
  "code": 400,
  "message": "java.lang.IllegalArgumentException: __key__ filter value must be a Key"
 }
}

I've been trying different things within the .filterKey("in", idsProfile) bit but can't seem to get it to work.

Could someone help me with that part to make this work? Thanks!

Brammer answered 12/1, 2016 at 15:44 Comment(3)
I don't think you can use an IN filter for filterKeys. Also in regard to the usage of IN in a cursor query the documentation says Because the NOT_EQUAL and IN operators are implemented with multiple queries, queries that use them do not support cursors, nor do composite queries constructed with the CompositeFilterOperator.or method. (Source: cloud.google.com/appengine/docs/java/datastore/…)Bureaucratic
@Bureaucratic Damn that sucks. You might be right - I won't be able to use a cursor potentially.Brammer
Try what suggested in this answer, it helped me link to answerShepherd

© 2022 - 2024 — McMap. All rights reserved.