1
votes

I am trying to use google datastore for my non GAE application.

For that i have created kinds and ancestor related entities in datastore using gcloud python library.

Also updated datastore index configuration for all the kinds using gcd tool via WEB-INF/datastore-indexes.xml file and its status' are serving.

However i can not successfully query the index based columns either in console or using gcloud lib.

Here is the query & traceback

from gcloud import datastore

ds = datastore.Client(dataset_id='XXXXXX')
query = datastore.Query(ds, kind='event')
query.add_filter('EvtName', '=', 'buy')
query.add_filter('EventDateTime', '<=', datetime.datetime(2015, 10, 22, 8, 45))
for itm in query.fetch():
    print(dict(itm))


gcloud.exceptions.PreconditionFailed: 412 no matching index found.

here is my datastore-indexes.xml config

<?xml version="1.0" encoding="utf-8"?>

<datastore-indexes
  autoGenerate="false">
    <datastore-index kind="event" ancestor="true">
        <property name="EvtName" direction="desc" />
        <property name="EventDateTime" direction="desc" />
    </datastore-index>
  <datastore-index kind="att" ancestor="true">
      <property name="EvtAttName" direction="desc" />
        <property name="EventDateTime" direction="desc" />
    </datastore-index>
  <datastore-index kind="att_val" ancestor="true">
      <property name="AttValue" direction="desc" />
        <property name="EventDateTime" direction="desc" />
    </datastore-index>
  <datastore-index kind="user" ancestor="true">
        <property name="EventDateTime" direction="desc" />
    </datastore-index>
</datastore-indexes>

am i missing something?

1
Can you share the queries you are trying to perform? Datastore requires specific indexes for each query. - Patrick Costello
@PatrickCostello, I have updated my question with query. Please check - shivg

1 Answers

2
votes

All of your indexes are designed to be used with ancestor queries (note the ancestor=true). However, your actual query does not query within a specific ancestor.

In order to answer your specific query, you need the index:

<datastore-index kind="event" ancestor="false">
    <property name="EvtName" direction="desc" />
    <property name="EventDateTime" direction="desc" />
</datastore-index>

Or, if you actually do want to query for entities with a specific parent, make sure to add an ancestor filter with Query#hasAncestor(Key parentKey).