【问题标题】:CreateIndex with searchable nested object arrays具有可搜索嵌套对象数组的 CreateIndex
【发布时间】:2020-01-29 18:57:38
【问题描述】:

任何人都知道如何创建一个索引,其中术语是一个 ref 并且可以通过数组中的嵌套对象进行搜索?

在“对话”集合中,我保存了以下示例数据:

{
  "created": Time("2019-09-29T22:11:01.493034Z"),
  "updated": Time("2019-09-29T22:11:01.493034Z"),
  "participants": [
    {
      "ref": Ref(Collection("users"), "244754936642929163"),
      "firstname": "John",
      "creator": true
    },
    {
      "ref": Ref(Collection("users"), "244517629884105216"),
      "firstname": "Max"
    }
  ]
}

有一个索引会很棒,我可以在其中搜索参与者数组中是否包含 ref。

【问题讨论】:

    标签: faunadb


    【解决方案1】:

    这可以使用索引来解决,其中数组被选为术语。当以术语为目标的字段是数组时,会在数组中的per 项中生成一个单独的索引条目。所以假设现存的集合 parentchild 然后

    db> CreateIndex({name: "cs", source: Collection("parent"), terms: [{field: ["data", "child"]}]})
    {
      ref: Index("cs"),
      ts: 1569831659780000,
      active: true,
      serialized: true,
      name: 'p',
      source: Collection("parent"),
      terms: [ { field: [ 'data', 'child' ] } ],
      partitions: 1
    }
    

    会做生意。示例用法:

    db> Create(Collection("child"), {})
    {
      ref: Ref(Collection("child"), "244918886014648845"),
      ts: 1569831701200000
    }
    db> Create(Collection("child"), {})
    {
      ref: Ref(Collection("child"), "244918887478460941"),
      ts: 1569831702590000
    }
    db> Create(Collection("parent"), {data:{child:[Ref(Collection("child"), "244918886014648845"), Ref(Collection("child"), "244918887478460941")]}})
    {
      ref: Ref(Collection("parent"), "244918956982272520"),
      ts: 1569831768880000,
      data: {
        child: [
          Ref(Collection("child"), "244918886014648845"),
          Ref(Collection("child"), "244918887478460941")
        ]
      }
    }
    db> Paginate(Match(Index("cs"), Ref(Collection("child"), "244918886014648845")))
    {
      data: [
        Ref(Collection("parent"), "244918956982272520")
      ]
    }
    

    两个子引用都会匹配。

    【讨论】:

      猜你喜欢
      • 2023-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-20
      • 1970-01-01
      • 2020-06-11
      • 1970-01-01
      • 2018-05-27
      相关资源
      最近更新 更多