Batch Query App engine datastore with AND expression

84 views
Skip to first unread message

Marcus Masekowsky

unread,
Jul 12, 2015, 1:50:27 PM7/12/15
to google-a...@googlegroups.com
Hi,

I am trying to add a AND-Expression to a GAE batch query to filter Entities:

Currently I am doing this, and it works perfect:
1. 
DBQuery.selectFromKeysOnly -> Query to select Entitie by key

2. 
String queryBatchString = "select from " + C1.class.getName() 
+ " where " 
                                 + Model.KEY_FIELD + " == :keys";
3. 
Query query = pm.newQuery(queryBatchString);
modelList = (List<Model>)query.execute(encodedKeyList);

The disadvantage with that approach is, that the query in step 2 always returns all Entities. But I only want Entites which have a date > current Date. Currently I am filtering the Entities in the server code (Step 4). 
To avoid this, I changed the query in 2. and added an "AND" expression to filter the entities directly:

String queryBatchString = "select from " + C1.class.getName() 
 + " where " 
                                 + Model.KEY_FIELD + " == :keys"
+  " AND " + C1.C1_DATE_FIELD + " > "+ currDate.getTimeInMillis();


But with this change I get an QueryCompilerSyntaxException:
"
Portion of expression could not be parsed: AND encodedKey == :keys
org.datanucleus.store.query.QueryCompilerSyntaxException: Portion of expression could not be parsed: AND encodedKey == :keys
"

What is wrong or what can be done better. Are AND-Expressions not allowed in Bbatch Queries?


Thanks 
Marcus

Patrice (Cloud Platform Support)

unread,
Jul 13, 2015, 11:11:11 AM7/13/15
to google-a...@googlegroups.com, marcus.m...@gmail.com
Hi,

As far as I know, datastore can take AND queries with no trouble. I'm thinking this might be the library you're using to interact with the datastore that might do this.

I assume if you try a simple "where" clause just checking for your date component, it works?

Cheers

Patrice (Cloud Platform Support)

unread,
Jul 13, 2015, 11:14:13 AM7/13/15
to google-a...@googlegroups.com, pvout...@google.com, marcus.m...@gmail.com
hmmm, having rechecked, are you sure that you can do an AND if your query is a "selectFromKeysOnly". Isn't there a more generic query?

Cheers

Marcus Masekowsky

unread,
Jul 13, 2015, 4:54:30 PM7/13/15
to google-a...@googlegroups.com
Hi, thanks for your reply.

to clarify, I have one query which retrieves the keys for Entity C1, This query is not my focus, because it onlys fetches the keys which I will later use to get the Entities from class C1.
So I have a list with keys from Entity C1. C1 has an attribute date of type long (Calendar Timemillis). Now I want to select all Entities with their Key in my keyList AND date > currDate.

Patrice (Cloud Platform Support)

unread,
Jul 14, 2015, 4:27:32 PM7/14/15
to google-a...@googlegroups.com, marcus.m...@gmail.com
Hi Marcus, 

I know this won't give you anything more than what you already have, but can you try running your second query "queryBatchString", but without the filter for  the date field?
And could you copy the string for "queryBatchString"? Because looking at how you build it, your keys should be after your "where", but it seems to have trouble parsing "AND encodedKey", which should never be in the string in the first place.

Cheers!

Marcus Masekowsky

unread,
Jul 16, 2015, 2:45:45 PM7/16/15
to google-a...@googlegroups.com, marcus.m...@gmail.com
I found the solution. I used the wrong syntax. Thanks for your help:

Correct is:

String queryBatch = "select from " + C1.class.getName() 
+ " where " + Model.KEY_FIELD + " == :keys"
+" && " + C1.C1_DATE_FIELD + " > :date"

Marcus Masekowsky

unread,
Jul 16, 2015, 3:41:56 PM7/16/15
to google-a...@googlegroups.com
now I am asking me how to pass the second parameter value to the query:
select from C1 where key == :keys && date > :date

":keys" is a list of keys and ":date" is a single long value.

I execute the query with 
query.execute(keyList);

The method "execute" only takes one parameter. 

Marcus Masekowsky

unread,
Jul 16, 2015, 4:21:17 PM7/16/15
to google-a...@googlegroups.com
Ok, I solved it. 
Solution is to hard code it in query like this:

select from C1 where key == :keys && date > 1436905284198

Patrice (Cloud Platform Support)

unread,
Jul 17, 2015, 12:50:59 PM7/17/15
to google-a...@googlegroups.com, marcus.m...@gmail.com
Hi Marcus,

Thank you for letting me know you got it to work.

Always happy to help, if there's anything else feel free to start a new thread

Cheers, and have a good weekend!
Reply all
Reply to author
Forward
0 new messages