【问题标题】:"no matching index found" when using Google Cloud Firestore via Datastore library通过 Datastore 库使用 Google Cloud Firestore 时“找不到匹配的索引”
【发布时间】:2021-10-28 05:07:33
【问题描述】:

我有一个在 Firestore 模式下配置了 Datastore 的 Google Cloud Platform 项目,其中以下 (Python) 代码会产生 google.api_core.exceptions.FailedPrecondition: 400 no matching index found. 错误。

from google.cloud import datastore

store = datastore.Client(project='my-project')
query = store.query(kind='my-kind')
query.add_filter('start_date', '<', 1500000000)
results = query.fetch()
for r in results:  # error thrown here
    print(r)

由于 Firestore 自动索引单个字段(并且我没有从该自动索引中排除 start_timestamp 字段),我希望这可以工作。我做错了什么?

(请注意,这在 Datastore 处于 Datastore 而不是 Firestore 模式的另一个项目上非常有效;不幸的是,我碰巧需要使用的项目已经有 Firestore。由于 Firestore 应该与 Datastore 向后兼容,我曾希望这会正常工作。)

【问题讨论】:

    标签: google-cloud-platform google-cloud-firestore google-cloud-datastore


    【解决方案1】:

    正如 Jim Morrison 所说,您需要使用 Firestore 库。

    from google.cloud import firestore
    db = firestore.Client()
    # [START get_simple_query]
    docs = db.collection(u'my-collection').where(u'start_date', u'<=', 1500000000).stream()
    
    for doc in docs:
        print(u'{} => {}'.format(doc.id, doc.to_dict()))
    

    【讨论】:

      【解决方案2】:

      很遗憾,Firestore 本机和 Datastore 模式下的 Firestore 之间的索引不同。您需要使用匹配的客户端来使您的查询正常工作。我建议您使用 google-cloud-firestore python 库而不是 google-cloud-datastore 库。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-11-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-01-26
        • 2021-01-13
        • 1970-01-01
        相关资源
        最近更新 更多