我正在尝试进行简单的查询:
entity = DeviceLocation.query(ndb.StringProperty('deviceID') == data['deviceID']).order(-DeviceLocation.timestamp).get()
每次引发下一个错误:
NeedIndexError: no matching index found. recommended index is:
- kind: DeviceLocation
properties:
- name: deviceID
- name: timestamp
问题是,即使我正在尝试创建索引(由此引导 - https://cloud.google.com/appengine/docs/python/datastore/indexes),部署命令(gcloud app deloy index.yaml
)也会返回此信息:
ERROR: (gcloud.app.deploy) Server responded with code [400]:
Bad Request Unexpected HTTP status 400.
Creating a composite index failed for entity_type: "DeviceLocation"
ancestor: false
Property {
name: "timestamp"
direction: 2
}
: This index:
IndexDef{form=SCANNER_BUILTIN_SINGLE_PROPERTY, kind=DeviceLocation, isAncestor=false, propertyDefs=[PropertyDef{path=timestamp, direction=DESCENDING, mode=null}]}
is not necessary, since single-property indices are built in. Please remove it from your index file and upgrade to the latest version of the SDK, if you haven't already.
据我了解,错误之间存在矛盾。这与group_by
和projection
相关。
目前,无法运行任何复杂的查询,这会阻止我取得任何进展。
答案 0 :(得分:5)
实际上这不是一个简单的查询'因为您按deviceID
过滤并按timestamp
排序。
GAE指示您创建复合索引,这是您必须附加到index.yaml
文件的内容:
- kind: DeviceLocation
properties:
- name: deviceID
- name: timestamp
确保按此特定顺序包含这两个属性(deviceID
和timestamp
)。
要改进的最后一件事是您按财产过滤的方式:
DeviceLocation.deviceID == data['deviceID']