首页 > 解决方案 > 单个属性上的 NeedIndexError (Google AppEngine + Firestore)

问题描述

我有一个已上传到 Google AppEngine 的 Python(标准环境)应用程序,它使用 Firestore 进行存储。

这是我的模型类的简化版本:

from google.appengine.ext import ndb

class User(ndb.Model):
    displayName = ndb.StringProperty()

    @classmethod
    def query_users(cls):
        query = cls.query().order(cls.displayName)
        db_objects = query.fetch()
        # do something with db_objects

这正如我在本地开发服务器(dev_appserver.py)中所期望的那样工作。

当我不使用 .order(cls.displayName) 调用时,我没有收到错误,但显然结果没有排序。

当我使用 .order(cls.displayName) 调用时,我收到以下错误:

NeedIndexError: no matching index found.
The suggested index for this query is:
- kind: User
  properties:
  - name: displayName

所有(10-15 次测试)用户文档在被查询的 Firestore 实例中都有一个 displayName 属性。我认为 Firestore 为每个属性生成了一个索引?

我发现了一个关于复合索引的稍微相似的问题,但我的问题更多是关于单个属性的索引。

此外,该项目的 Google Cloud Platform (GCP) 控制台中的索引页面不显示任何索引,而是显示以下文本:

To use Firestore during Beta, open the Firebase console.

Firebase 控制台索引页面 ( https://console.firebase.google.com/project/ $PROJECT_ID/database/firestore/indexes ) 没有列出复合索引或其他索引。

我有一个自动生成的 index.yaml 文件,其中包含复杂的索引(并且没有简单的索引)。

我还有一个 firestore.indexes.json 文件,目前没有指定任何内容:

{
  "indexes": []
}

(我在 firestore.rules 文件中创建了一些数据库规则,并同时创建了这个文件。)

通过阅读有关索引的 Firestore 文档,firestore.indexes.json 文件应该用于复杂(多属性)索引,而不是用于单属性索引。

那么,Google AppEngine (GAE) / Google Cloud Platform(GCP,Python 标准环境)是否与 Firestore 索引配合得很好?

更重要的是,有人对如何用单一属性索引解决这个问题有任何想法吗?

对于后续问题,将 Firestore 用于数据存储时是否会忽略 index.yaml 文件?我应该在 firestore.indexes.json 文件中的 index.yaml 文件中重新创建索引并部署它吗?

标签: firebasegoogle-app-enginegoogle-cloud-platformgoogle-cloud-firestore

解决方案


推荐阅读