【发布时间】:2019-11-10 01:22:06
【问题描述】:
我有一个使用 python 2.7 和 Cloud Datastore 的 Google App Engine 项目(它可能是 Cloud Firestore,但稍后会详细介绍)。我的问题是,当我做一个简单的查询时,例如Data.query(ndb.AND(Data.timeStamp >= day, Data.timeStamp < day + datetime.timedelta(days=1))).order(Data.timeStamp) 我收到 500(服务器错误),并且日志中出现此错误:
NeedIndexError: 找不到匹配的索引。
此查询的建议索引是:
- 种类:数据
属性:
- 名称:时间戳
建议的索引是简单查询的索引。如果我将该查询添加到 index.yaml 文件并运行 gcloud datastore indexes create index.yaml,我会收到以下消息:
错误:(gcloud.datastore.indexes.create) 服务器响应代码 [400] :
错误请求 意外 HTTP 状态 400。
为 entity_type 创建复合索引失败:“数据”
物业{
名称:“时间戳”
方向:上升
}
祖先:假
: 这个指数:
IndexDef{semantics=DATASTORE, kind=Data, indexAncestor=NONE, propertyDefs=[PropertyDef{path=timeStamp, mode=ORDERED, direction=ASCENDING}]}
没有必要,因为单属性索引是内置的。
所以我不能运行这个查询,因为我需要一个索引,但我不能创建索引。所以我的问题是,我该如何运行这个查询?
更多信息:
不确定,但这可能是相关的:当我进入 Google Cloud Console (console.cloud.google.com) 并单击 Datastore 选项卡时,它声称我使用“Cloud Firestore in Native mode”并给我一个指向Firestore 选项卡。当我去那里时,我可以在 Firestore 中看到我的所有数据。即使我使用 python2 和 ndb api 访问 Cloud Datastore/Firestore,这一切也会发生。
此外,当我单击 Firestore 选项卡中的索引页面时,GCP 声称我的所有字段都已为简单查询编制索引。
最后,dev_appserver.py 没有在 index.yaml 文件中为我生成索引(如预期的那样)。
这是我的代码:
main.py
import datetime
from google.appengine.ext import ndb
class Data(ndb.Model):
timeStamp = ndb.DateTimeProperty(indexed=True)
#WSGI compatible function
def app(env, startResponse):
headers = [('Content-type', 'text/plain')]
status = "200 OK"
path = env["PATH_INFO"]
if path == "/doData":
Data(timeStamp = datetime.datetime.now()).put()
startResponse(status, headers)
return ["done"]
elif path == "/getData":
day = datetime.datetime.strptime("2019-06-28", "%Y-%m-%d")
records = Data.query(ndb.AND(Data.timeStamp >= day, Data.timeStamp < day + datetime.timedelta(days=1))).order(Data.timeStamp).fetch(10)
print(records)
startResponse(status, headers)
return [str(record.timeStamp) + "\n" for record in records]
startResponse("404 NOT FOUND", headers)
return ["404 Page Not Found"]
app.yaml:
runtime: python27
api_version: 1
threadsafe: yes
handlers:
- url: /.*
script: main.app
index.yaml:
indexes:
- kind: Data
properties:
- name: timeStamp
# AUTOGENERATED
# This index.yaml is automatically updated whenever the dev_appserver
# detects that a new type of query is run. If you want to manage the
# index.yaml file manually, remove the above marker line (the line
# saying "# AUTOGENERATED"). If you want to manage some indexes
# manually, move them above the marker line. The index.yaml file is
# automatically uploaded to the admin console when you next deploy
# your application using appcfg.py.
提前致谢!
【问题讨论】:
标签: indexing google-cloud-firestore google-cloud-datastore app-engine-ndb google-app-engine-python