google-code-export / objectify-appengine

Automatically exported from code.google.com/p/objectify-appengine
MIT License
1 stars 0 forks source link

Filtering per @Id field #121

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I am using Objectify 3.1.

When filtering a query per a class attribute with an @Id anotation, I get the 
following error message "The provided keys-only multi-query needs to perform 
some sorting in memory. As a result, this query can only be sorted by the key 
property as this is the only property that is available in memory." Below is an 
example of my code

public class Document implements Serializable
{
    private static final long serialVersionUID = 3266444335668869756L;

    @Id
    private String id;

    ...
}

Query<Document> query = ObjectifyService.begin().query(Document.class);
query.filter("id in ", keys); // keys is an ArrayList

Original issue reported on code.google.com by jmo...@gpartner.eu on 30 Apr 2012 at 7:34

GoogleCodeExporter commented 9 years ago
While writing this new post, I just realized I forgot a line of code in my last 
post :
query = query.order("-lastUpdateDate");

While searching into the documentation, I found something interesting : 
https://developers.google.com/appengine/docs/java/datastore/queries#Properties_i
n_Inequality_Filters_Must_Be_Sorted_before_Other_Sort_Orders

If I add a sort by id before the sort by  last update date, the error message 
disappears :

Query<Document> query = ObjectifyService.begin().query(Document.class);
query = query.filter("id in ", keys); // keys is an ArrayList
query = query.order("id");
query = query.order("-lastUpdateDate");

But, now, documents are not sorted by lastUddateDate, but by id first, and then 
by lastUpdateDate. Not exactly what I wanted...

Do you have any idea how to sort the result by lastUpdateDate within a query 
using a IN filter ?
Thanks for help,

Original comment by jmo...@gpartner.eu on 30 Apr 2012 at 1:10

GoogleCodeExporter commented 9 years ago
Are you trying to do a fetchKeys()?  The original error message is what you get 
when you try to do that kind of query without fetching the entity data.  The 
GAE SDK needs the entity data so that it can do in-memory sorting.

Do a regular fetch() and you will probably be ok.

Original comment by lhori...@gmail.com on 30 Apr 2012 at 2:19

GoogleCodeExporter commented 9 years ago
Thanks for your answer.

You are right, with a regular fetch(), it's ok, but I am doing a fetchKeys() 
because, of what I understood, the fetchKeys() only works with Indexes, and do 
not execute "real queries" to datastore.

I want to optimize the number of queries I am doing to the datastore, that's 
why I fetch keys only. Can you confirm that this mechanisme will not count as 
read operations on the datastore with fetchKeys() ?

Is there a way to make this query working with fetchKeys() ?

Thanks,

Original comment by jmo...@gpartner.eu on 30 Apr 2012 at 2:41

GoogleCodeExporter commented 9 years ago
Keys-only queries still cost datastore operations.  They cost 1 read op, plus 1 
small op per key returned.

However, if you want a sorted IN query, the sorting must be done client-side.  
You cannot make this query work with fetchKeys().  If you have questions about 
application design, please open up a discussion on the objectify mailing list 
rather than the bug tracker.  Thanks!

Original comment by lhori...@gmail.com on 30 Apr 2012 at 3:02