Hey,
I have the problem that querying for entities sometimes returns an empty list, even though there exists data that should be returned. When I go into the browser
https://console.cloud.google.com/datastore/entities/query?... and do the query there it works and I can see the data. Then, after a while, performing the query in my code works again and returns the appropriate results.
In more detail:
I have an entity like this:
@Entity
@Cache
public class Entity {
@Parent private Key<ParentClass> parentKey;
@Id private String id;
@Index private String myIndexedString;
@Index private Date myIndexedDate;
...
}
Then I do this query (also sometimes with order("myIndexedDate")):
public List<Entity> queryEntites(Key<ParentClass> parentKey, String theIndexedValue, int offset, int limit) {
return ofy().consistency(Consistency.STRONG).load()
.type(Entity.class)
.ancestor(parentKey)
.filter("myIndexedString", theIndexedValue)
.order("-myIndexedDate")
.offset(offset)
.limit(limit)
.list();
}
I have these entries in my datastore-index.xml:
<datastore-index kind="Entity" ancestor="true" source="manual">
<property name="myIndexedString" direction="asc"/>
<property name="myIndexedDate" direction="desc"/>
</datastore-index>
<datastore-index kind="Entity" ancestor="true" source="manual">
<property name="myIndexedString" direction="asc"/>
<property name="myIndexedDate" direction="asc"/>
</datastore-index>
Then I call
queryEntites(specificParentKey, specificIndexedValue, 0, 100);
and the returned list is empty.
Immediately after this I use the following function
public List<Entity> queryEntityOnlyByIndexedValueWithoutParentAndOrder(String theIndexedValue, int offset, int limit) {
return ofy().consistency(Consistency.STRONG).load().type(Entity.class)
.filter("myIndexedString", theIndexedValue)
.offset(offset)
.limit(limit)
.list();
}
via calling
queryEntityOnlyByIndexedValue(specificIndexedValue, 0, 100);
and also the returned result is also an empty list.
My hope was that this might work, as there is no multi-index involved.
So what is happening? As it seems, the index is not available. Why is there not an exception is this case, but a returned empty list?
In both cases there should have been returned something. I could check in the browser by querying for "myIndexedString" that there are indeed objects (and I know for other reasons as well). Why can I look at the objects in the browser, even though queryEntityOnlyByIndexedValue() was not working?
I actually also tried the first query queryEntites() 50 times in a row with waiting half a second between each try. In all cases the returned list was empty. Then I tried it again 30min later and it worked.
Some remarks:
I have a lot of Entities in my database (millions), and even though a single entity is normally not changed that often (maybe 5 times in 24h and then possibly never changed again), it can be that for a short period of time a single entity is changed multiple times in a row (like 10 times in 5min), or that fixing a parentKey, that there are many changes to entities with that parentKey (like 10.000 new entries or changes in 1 hour).
Questions:
Why do I get an empty list via the query, even though I can watch the data in the browser?
Why is there no exception thrown?
Is this a problem with the index, or are there other reasons?
Thanks a lot
Stefan