412 在云数据存储中执行查询时找不到匹配的索引

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

我正在使用 gcloud-python 库从云数据存储中查询数据。考虑我的片段是这样的

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

对于上面的片段,我得到

    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.

我的Index.yaml文件是这样的

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

我是不是做错了什么?请帮帮我。

index configuration docs 表示索引配置应位于名为 datastore-indexes.xml.

的 XML 文件中

您的所有索引都使用 ancestor:yes,因此应在您的查询中添加祖先键。没有祖先,您的索引配置需要另一个带有 'ancestor:no'

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

注意:每个查询的特定索引