Objectify query filter by List of keys that have a

2019-03-03 13:28发布

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!

0条回答
登录 后发表回答