Hey @aprilmintacpineda , thanks for the answer and my apologies for correcting you. Thatâs indeed a possibility but probably not what @maisnamraju is looking for since you can do that without storing the references.
@maisnamraju, your problem is the structure of the query. So weâll go step by step in a Let. Each time weâll return all values so we can see exactly how our query progresses when we add elements. To just get the Leads is simple, you got that right.
Let({
lead: Get(Ref(Collection('Leads'), '269038063157510661'))
},
{
lead: Var('lead')
}
)
We can get the âsource keyâ out of the lead (you had that part as well iirc)
Let({
lead: Get(Ref(Collection('Leads'), '269038063157510661')),
sourceKey: Select(['data', 'source'], Var('lead'))
},
{
lead: Var('lead'),
sourceKey: Var('sourceKey')
}
)
Approach 1 (more flexible, a bit more expensive) Index that Returns references + Map/Get
Then we can get all sourceReferences related to this sourceKey by using an index. Your index is slightly different from mine. Iâll write another example with your index later. Mine looks like:
{
name: "LeadSourceByKey",
unique: false,
serialized: true,
source: "Sources",
terms: [
{
field: ["data", "key"]
}
]
}
I do not specify values, in which case the reference of the source will be returned.
We can then get all references as follows:
Let(
{
lead: Get(Ref(Collection("Leads"), "269038063157510661")),
sourceKey: Select(["data", "source"], Var("lead")),
sourceReferences: Match(Index("LeadSourceByKey"), Var("sourceKey"))
)
},
{
lead: Var("lead"),
sourceKey: Var("sourceKey"),
sourceReferences: Var("sourceReferences")
}
)
And from these references, you can get the actual source documents by using a Map + Get again.
Let(
{
lead: Get(Ref(Collection("Leads"), "269038063157510661")),
sourceKey: Select(["data", "source"], Var("lead")),
sourceReferences: Match(Index("LeadSourceByKey"), Var("sourceKey")),
sources: Map(
Paginate(Var("sourceReferences")),
Lambda(["ref"], Get(Var("ref")))
)
},
{
lead: Var("lead"),
sourceKey: Var("sourceKey"),
sourceReferences: Var("sourceReferences"),
sources: Var("sources")
}
Approach 2 (yours) Index that Returns values
You made an index that returns some values which could be an approach (put all values in the index). In that case, since you did not return the reference we canât Get the document. However, itâs cheaper since it doesnât do a get for each source document (you can optimise like this, the tradeoff is that itâs less flexible, if your documents change, your index has to be updated)
Imagine we use your index:
{
name: "LeadSourceValuesByKey",
unique: false,
serialized: true,
source: "Sources",
terms: [
{
field: ["data", "key"]
}
],
values: [
{
field: ["data", "key"]
},
{
field: ["data", "value"]
}
]
}
Then we specified that this index returns [key, value] instead of only the reference. So we can write it differently:
Let(
{
lead: Get(Ref(Collection("Leads"), "269038063157510661")),
sourceKey: Select(["data", "source"], Var("lead")),
sourceValues: Paginate(Match(Index("LeadSourceValuesByKey"), Var("sourceKey")))
},
{
lead: Var("lead"),
sourceKey: Var("sourceKey"),
sourceValues: Var("sourceValues")
}
)
Would return:
{
lead: {
ref: Ref(Collection("Leads"), "269038063157510661"),
ts: 1592833540970000,
data: {
notes: "voicemail ",
source: "key-name",
name: "Glenn"
}
},
sourceKey: "key-name",
sourceValues: {
data: [["key-name", "Google Ads"]]
}
}
Note that the sourceValues is an array since the values of an index are always an array. Since Paginate returns an array as well, you now have an array of arrays. (you might have multiple source values that were returned from the index). the result is: