google-cloud-datastoregcloud-pythongoogle-cloud-python

412 no matching index found while executing a query in cloud datastore


I am using gcloud-python library for querying data from the cloud datastore. Consider my snippet to be like this

from google.appengine.ext import ndb
from datetime import datetime
class Order(ndb.Model):
     order_name = ndb.StringProperty(required=True)
     date_created = ndb.DateTimeProperty(default= datetime.now())

#code for querying the cloud datastore
from gcloud.datastore.query import Query

date_start = datetime.combine(date(year=2015, month=08, day=01), time())
date_end = datetime.combine(date(year=2015, month=08, day=03), time())

query = Query(kind='Order')

query.add_filter('order_name', '=', 'grand-line-order')
query.add_filter('date_created', '<', date_end)
query.add_filter('date_created', '>', date_start)

iterator = query.fetch(limit=10)
records, more, cursor = iterator.next_page()
print records

For the above snippet i am getting

    File "/Users/sathyanarrayanan/Desktop/app/services/cdr_helper.py", line 528, in fetch_cdr
    records, more, cursor = iterator.next_page()
  File "/Users/sathyanarrayanan/Desktop/app/gcloud/datastore/query.py", line 388, in next_page
    transaction_id=transaction and transaction.id,
  File "/Users/sathyanarrayanan/Desktop/app/gcloud/datastore/connection.py", line 257, in run_query
    datastore_pb.RunQueryResponse)
  File "/Users/sathyanarrayanan/Desktop/app/gcloud/datastore/connection.py", line 108, in _rpc
    data=request_pb.SerializeToString())
  File "/Users/sathyanarrayanan/Desktop/app/gcloud/datastore/connection.py", line 85, in _request
    raise make_exception(headers, content, use_json=False)
PreconditionFailed: 412 no matching index found.

My Index.yaml file is like this.

indexes:
- kind: Order
  ancestor: yes
  properties:
  - name: date_created

- kind: Order
  ancestor: yes
  properties:
  - name: date_created
    direction: desc

- kind: Order
  ancestor: yes
  properties:
  - name: order_name
    direction: asc
  - name: date_created
    direction: desc

- kind: Order
  ancestor: yes
  properties:
  - name: order_name
    direction: asc
  - name: date_created
    direction: asc

Am I doing something wrong? Please help me out.


Solution

  • All of your indexes using ancestor:yes so ancestor key should be added in your query. without ancestor your index configuration require another index with 'ancestor:no'

    - kind: Order
      ancestor: no
      properties:
        - name: order_name
          direction: asc
        - name: date_created
          direction: desc
    

    Note: specific indexes for each query