Hi inam! Thanks for using Fauna.
The Equals
function simply compares two values and returns true
if they match. Fauna doesn’t have a wildcard version of Equals
, so that strategy is not going to work. It might be possible to use ContainsStrRegex
to good effect, though.
I’m guessing that the by_room_type
index has no terms. It would be more efficient if you were using a index that specified the room type as a term. Something like this:
CreateIndex({
name: "rooms_by_room_type",
source: Collection("rooms"),
terms: [
{ field: ["data", "room_type"] }
],
})
With that index, you can then get the references for all matching room types:
Paginate(Match(Index("rooms_by_room_type"), "double"))
If you need all rooms, regardless of room type, then you’d use:
Paginate(Documents(Collection("rooms")))
If you need to fetch some/all of the room details with those queries, you could revise the index to report them, or simply run a query like this:
Map(
Paginate(Match(Index("rooms_by_room_type"), "double")),
Lambda(
"ref",
Get(Var("ref"))
)
)
Notice that inside the Map
call, I’ve place the first Paginate
example, which fetches a page of room with the “double” room type. Map
then calls the Lambda
on each entry in the page of results, and fetches the full document for the room.
This is an example of FQL’s composition capability, when the result from one function can be used as a parameter to another function.
If you need to search by two criteria, then you would create another index that could look like this:
CreateIndex({
name: "rooms_by_room_type_and_price",
source: Collection("rooms"),
terms: [
{ field: ["data", "room_type"] },
{ field: ["data", "price"] },
],
})
Then you could fetch matching entries with:
Paginate(Match(Index("rooms_by_room_type_and_price"), "double", 80.00))
Fauna forces you to be more deliberate about your queries than SQL does, so that i can avoid resource problems that arise from open-ended queries and temporary results.