如何在 FaunaDB 的文档中索引作为 @relation 的值?

问题描述

是否可以从 FaunaDB 中的 @relation 类型创建值的索引?这是架构,但我无法弄清楚如何为 data.testing.status 值创建索引。

type TestType {
  testing: Testing!
}

type Testing {
  status: PaymentStatus!
  testType: [TestType!] @relation
}

enum PaymentStatus {
  PAID
  UNPAID
}

我不知道枚举是否导致问题?我找不到任何关于此的文档。

这是查询

Map(
  Paginate(Match(Index("certificate_remittance_by_remittance"),"UNPAID")),Lambda("ref",Get(Var("ref")))
)

以及相关文档数据:

 "ref": Ref(Collection("Certificate"),"302119834927235593"),"ts": 1624382777140000,"data": {
    "remittance": Ref(Collection("Remittance"),"302119834830766601"),}

及汇款凭证:

{
  "ref": Ref(Collection("Remittance"),"data": {
    "status": "UNPAID","chequeNumber": "","remittanceOwed": 245,"remittanceAmount": 245
  }
}

解决方法

当然可以。

架构的 enum 部分不会导致问题。如果是这样,您将无法成功导入架构。

使用您提供的架构,请注意 GraphQL API 已经为您创建了关系索引:

Get(Index("testType_testing_by_testing"))

{
  ref: Index("testType_testing_by_testing"),ts: 1624315929200000,active: true,serialized: true,name: "testType_testing_by_testing",source: Collection("TestType"),data: {
    gql: {
      ts: Time("2021-06-21T22:52:08.969203Z")
    }
  },terms: [
    {
      field: ["data","testing"]
    }
  ],unique: false,partitions: 1
}

要为 TestType 集合添加索引,请在 status 字段上运行:

CreateIndex({
  name: "TestType_by_status",terms: [
    { field: ["data","status"] },]
})