Google App Engine error: NeedIndexError: no matching index found

Google App Engine error: NeedIndexError: no matching index found

我在使用 Google 的 App 引擎索引时遇到问题。当 运行 我的应用程序通过 GoogleAppEngineLauncher 时,应用程序运行正常。部署应用程序时,出现以下错误:

NeedIndexError: no matching index found.
The suggested index for this query is:
- kind: Bar
  ancestor: yes
  properties:
  - name: rating
    direction: desc

错误发生在这行代码之后:

 bars = bar_query.fetch(10)

在上面这行代码之前,写着:

bar_query = Bar.query(ancestor=guestbook_key(guestbook_name)).order(-Bar.rating)

我的 index.yaml 文件包含下面的确切 "suggested" 索引 # AUTOGENERATED:

- kind: Bar
  ancestor: yes
  properties:
  - name: rating
    direction: desc

我是不是漏掉了什么?我删除了 index.yaml 文件并再次部署了应用程序(通过命令行),上传的文件少了一个 - 所以 index.yaml 文件就在那里。

本地一切正常。我正在研究最新的 Mac OSx。用于部署的命令是:

appcfg.py -A app-name --oauth2 update app

我实现的数据存储大致基于留言簿教程应用程序。

如有任何帮助,我们将不胜感激。

编辑:

我的ndb.Model定义如下:

class Bar(ndb.Model):
    content = ndb.StringProperty(indexed=False)
    lat = ndb.FloatProperty(indexed=False)
    lon = ndb.FloatProperty(indexed=False)
    rating = ndb.IntegerProperty(indexed=True)
    url = ndb.TextProperty(indexed=False)

检查 https://appengine.google.com/datastore/indexes 以查看此索引是否存在且状态设置为 "serving"。有可能索引还在建中

开发环境模拟生产环境。它实际上并没有数据存储意义上的索引。

我通过将错误提示缺少的索引移动到 "index.yaml" 文件中自动生成行的上方来解决此问题。

在您的情况下,yaml 文件将如下所示:

indexes:
- kind: Bar
 ancestor: yes
 properties:
 - name: rating
   direction: desc

# AUTOGENERATED

然后您所要做的就是更新您的应用程序,然后更新索引,您可以通过 运行 以下命令更新索引。

appcfg.py [options] update_indexes <directory>

目录是相对于您的 index.yaml 文件的目录。然后,您应该会在 https://appengine.google.com/datastore/indexes

的仪表板上看到该索引

更新最初是 "pending",但在索引显示 "serving" 之后,您就可以进行查询了。

现在可能有点晚了,但是 运行 "gcloud app deploy index.yaml" 有所帮助,因为 运行 部署本身忽略了 index.yaml 文件。

正如其他人所说,https://appengine.google.com/datastore/indexes 的仪表板将显示 "pending" 一段时间。

我偶然发现了同样的问题,你的评论帮助我找到了正确的方向。以下是 Google 说明的处理方法:

根据 Google 文档,故事是使用

gcloud app deploy 

index.yaml 文件未上传(问题是为什么不上传?)。无论如何,必须手动上传这个索引文件。

为此,文档给出了以下命令:

gcloud datastore create-indexes index.yaml

(假设您从 index.yaml 文件的同一目录执行此操作) 完成此操作后,您可以转到 Datastore 控制台,您将看到索引已创建。然后它将开始被索引(在我的例子中花了大约 5 分钟),一旦索引被提供,你就可以开始你的应用程序了。

就我而言,我已手动上传索引文件,如下所示:

gcloud datastore indexes create "C:\Path\of\your\project\index.yaml"

那么你应该确认更新:

Configurations to update:

descriptor:      [C:\Path\of\your\project\index.yaml]
type:            [datastore indexes]
target project:  [project_name]


Do you want to continue (Y/n)?  y

然后你可以去数据存储控制台检查索引是否已经通过这个link创建: https://console.cloud.google.com/datastore/indexes

这个 NeedIndexError 可以由不同的原因触发,因为我到达这里时遇到了一个稍微不同的问题,所以我会尝试解释我做错的所有事情以展示可以做的事情:

  • 我原以为每种实体只能有一个索引。这不是真的,只要我found 你需要有和你需要做的不同查询一样多的索引

  • development web server 时自动生成索引并放置在 index.yaml 文件中的 #AUTOGENERATED 行下方。

  • 修改索引后,我首先使用 gcloud datastore indexes create index.yaml 然后我 等待 直到索引在 https://console.cloud.google.com/datastore/indexes?project=your-projectServing

  • 我通过执行 gcloud datastore indexes cleanup index.yaml 清理未使用的索引请注意,您不会删除生产中正在使用的索引。参考here

  • 请注意,如果您未在索引属性中指定 direction,默认情况下将为 ASC。因此,如果您尝试进行 - 排序查询,它将再次出现错误。

我认为的事情,但我没有 100% 的证据比我的特定问题更长,但我认为可以作为一种头脑风暴:

  • 索引在查询数据时很重要,而不是在上传时。

  • 如果您手动生成索引,则似乎没有必要手动创建 #AUTOGENERATED 行。参考here

  • 由于开发服务器在进行查询时会更新 #AUTOGENERATED 行以下的索引,您可以通过添加此通道 "accidentally" 来解决您的问题。而真正的问题是缺少使用 gcloud datastore indexes create index.yaml 命令手动更新索引。参考 here 这里