List of terms using a UDF
Solution
This is a more advanced version of the List of terms solution. This solution uses a user-defined function (UDF) to compose the query for every provided term.
You can now call the UDF with an array of terms to search:
ObjectV(data: Arr(ObjectV(ref: RefV(id = "181388642088911360", collection = RefV(id = "spells", collection = RefV(id = "collections"))),ts: LongV(1629836365330000),data: ObjectV(name: StringV(Hippo's Wallow),element: Arr(StringV(water), StringV(earth))))))
map[data:[map[data:map[element:[water earth] name:Hippo's Wallow] ref:{181388642088911360 0xc0000925a0 0xc0000925a0 <nil>} ts:1629835747470000]]]
{data: [{ref: ref(id = "181388642088911360", collection = ref(id = "spells", collection = ref(id = "collections"))), ts: 1629836886190000, data: {name: "Hippo's Wallow", element: ["water", "earth"]}}]}
{
data: [
{
ref: Ref(Collection("spells"), "181388642088911360"),
ts: 1629831125560000,
data: { name: "Hippo's Wallow", element: [ 'water', 'earth' ] }
}
]
}
{'data': [{'ref': Ref(id=181388642088911360, collection=Ref(id=spells, collection=Ref(id=collections))), 'ts': 1629831361330000, 'data': {'name': "Hippo's Wallow", 'element': ['water', 'earth']}}]}
{
data: [
{
ref: Ref(Collection("spells"), "181388642088911360"),
ts: 1629503268260000,
data: { name: "Hippo's Wallow", element: [ 'water', 'earth' ] }
}
]
}
or with a different array:
ObjectV(data: Arr(ObjectV(ref: RefV(id = "181388642046968320", collection = RefV(id = "spells", collection = RefV(id = "collections"))),ts: LongV(1629836365330000),data: ObjectV(name: StringV(Fire Beak),element: Arr(StringV(air), StringV(fire)),spellbook: RefV(id = "181388642139243008", collection = RefV(id = "spellbooks", collection = RefV(id = "collections"))))), ObjectV(ref: RefV(id = "181388642071085568", collection = RefV(id = "spells", collection = RefV(id = "collections"))),ts: LongV(1629836365330000),data: ObjectV(name: StringV(Water Dragon's Claw),element: Arr(StringV(water), StringV(fire)),spellbook: RefV(id = "181388642139243008", collection = RefV(id = "spellbooks", collection = RefV(id = "collections"))))), ObjectV(ref: RefV(id = "181388642088911360", collection = RefV(id = "spells", collection = RefV(id = "collections"))),ts: LongV(1629836365330000),data: ObjectV(name: StringV(Hippo's Wallow),element: Arr(StringV(water), StringV(earth))))))
map[data:[map[data:map[element:[air fire] name:Fire Beak spellbook:{181388642139243008 0xc000109b00 0xc000109b00 <nil>}] ref:{181388642046968320 0xc000109920 0xc000109920 <nil>} ts:1629835747470000] map[data:map[element:[water fire] name:Water Dragon's Claw spellbook:{181388642139243008 0xc000109ec0 0xc000109ec0 <nil>}] ref:{181388642071085568 0xc000109ce0 0xc000109ce0 <nil>} ts:1629835747470000] map[data:map[element:[water earth] name:Hippo's Wallow] ref:{181388642088911360 0xc0001700c0 0xc0001700c0 <nil>} ts:1629835747470000]]]
{data: [{ref: ref(id = "181388642046968320", collection = ref(id = "spells", collection = ref(id = "collections"))), ts: 1629836886190000, data: {name: "Fire Beak", element: ["air", "fire"], spellbook: ref(id = "181388642139243008", collection = ref(id = "spellbooks", collection = ref(id = "collections")))}}, {ref: ref(id = "181388642071085568", collection = ref(id = "spells", collection = ref(id = "collections"))), ts: 1629836886190000, data: {name: "Water Dragon's Claw", element: ["water", "fire"], spellbook: ref(id = "181388642139243008", collection = ref(id = "spellbooks", collection = ref(id = "collections")))}}, {ref: ref(id = "181388642088911360", collection = ref(id = "spells", collection = ref(id = "collections"))), ts: 1629836886190000, data: {name: "Hippo's Wallow", element: ["water", "earth"]}}]}
{
data: [
{
ref: Ref(Collection("spells"), "181388642046968320"),
ts: 1629831125560000,
data: {
name: 'Fire Beak',
element: [ 'air', 'fire' ],
spellbook: Ref(Collection("spellbooks"), "181388642139243008")
}
},
{
ref: Ref(Collection("spells"), "181388642071085568"),
ts: 1629831125560000,
data: {
name: "Water Dragon's Claw",
element: [ 'water', 'fire' ],
spellbook: Ref(Collection("spellbooks"), "181388642139243008")
}
},
{
ref: Ref(Collection("spells"), "181388642088911360"),
ts: 1629831125560000,
data: { name: "Hippo's Wallow", element: [ 'water', 'earth' ] }
}
]
}
{'data': [{'ref': Ref(id=181388642046968320, collection=Ref(id=spells, collection=Ref(id=collections))), 'ts': 1629831361330000, 'data': {'name': 'Fire Beak', 'element': ['air', 'fire'], 'spellbook': Ref(id=181388642139243008, collection=Ref(id=spellbooks, collection=Ref(id=collections)))}}, {'ref': Ref(id=181388642071085568, collection=Ref(id=spells, collection=Ref(id=collections))), 'ts': 1629831361330000, 'data': {'name': "Water Dragon's Claw", 'element': ['water', 'fire'], 'spellbook': Ref(id=181388642139243008, collection=Ref(id=spellbooks, collection=Ref(id=collections)))}}, {'ref': Ref(id=181388642088911360, collection=Ref(id=spells, collection=Ref(id=collections))), 'ts': 1629831361330000, 'data': {'name': "Hippo's Wallow", 'element': ['water', 'earth']}}]}
{
data: [
{
ref: Ref(Collection("spells"), "181388642046968320"),
ts: 1629830703650000,
data: {
name: 'Fire Beak',
element: [ 'air', 'fire' ],
spellbook: Ref(Collection("spellbooks"), "181388642139243008")
}
},
{
ref: Ref(Collection("spells"), "181388642071085568"),
ts: 1629830703650000,
data: {
name: "Water Dragon's Claw",
element: [ 'water', 'fire' ],
spellbook: Ref(Collection("spellbooks"), "181388642139243008")
}
},
{
ref: Ref(Collection("spells"), "181388642088911360"),
ts: 1629830703650000,
data: { name: "Hippo's Wallow", element: [ 'water', 'earth' ] }
}
]
}
Discussion
You pass an array of terms in the Call
function and the UDF
uses Reduce
to compose a Match
expression for each term.
When there are terms, a Union
of the results is returned,
otherwise the list of documents in the spells
collection is returned.
Union
combines all of the results for every term, which is
equivalent to an "or" query. If you replace Union
with
Intersection
, only the results common to every term are returned,
which is equivalent to an "and" query.
Is this article helpful?
Tell Fauna how the article can be improved:
Visit Fauna's forums
or email docs@fauna.com
Thank you for your feedback!