Skip to main content
 首页 » 编程设计

python之Google App Engine之NeedIndexError : no matching index found

2025年05月04日39lexus

我正在尝试做简单的查询:

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 相关。

目前,无法运行任何复杂的查询,这使我无法取得任何进展。

请您参考如下方法:

实际上这不是一个“简单查询”,因为您是按 deviceID 过滤并按 timestamp 排序。

GAE 指导您创建一个复合索引,这是您必须附加到您的 index.yaml 文件的内容:

- kind: DeviceLocation 
  properties: 
  - name: deviceID 
  - name: timestamp 

确保在此特定顺序中包含这两个属性(deviceID 和 timestamp)。

最后要改进的是按属性过滤的方式:

DeviceLocation.deviceID == data['deviceID']