FQL v4 will be decommissioned on June 30, 2025. Ensure that you complete your migration from FQL v4 to FQL v10 by that date. Fauna accounts created after August 21, 2024 must use FQL v10. These accounts will not be able to run FQL v4 queries or access the v4 Dashboard. For more details, see the v4 EOL announcement and migration guide. Contact support@fauna.com with any questions. |
Index terms
When you create an index you have the option of including one or more
entries in the terms
field. Each entry represents a field name to make
searchable in the index’s source collection(s).
Let’s add some documents to a collection named cars
:
[
{
ref: Ref(Collection("cars"), "1"),
ts: 1645226205590000,
data: { make: 'Honda', model: 'Civic', doors: 4 }
},
{
ref: Ref(Collection("cars"), "2"),
ts: 1645226205590000,
data: { make: 'Honda', model: 'Accord', doors: 2 }
},
{
ref: Ref(Collection("cars"), "3"),
ts: 1645226205590000,
data: { make: 'Toyota', model: 'Camry', doors: 4 }
}
]
In order to search values in the make
field, we can include make
in
the terms
definition in an index called cars_by_make
:
{
ref: Index("cars_by_make"),
ts: 1641940850400000,
active: true,
serialized: true,
name: "cars_by_make",
source: Collection("cars"),
terms: [
{
field: ["data", "make"]
}
],
partitions: 1
}
Note that if any new documents are added to the cars
collection which
do not include a make
field, those documents are not represented in
the cars_by_make
index.
Now we can use the cars_by_make
index to search for documents which
match on search criteria for the make
field:
{
data: [
{
ref: Ref(Collection("cars"), "1"),
ts: 1645226205590000,
data: { make: 'Honda', model: 'Civic', doors: 4 }
},
{
ref: Ref(Collection("cars"), "2"),
ts: 1645226205590000,
data: { make: 'Honda', model: 'Accord', doors: 2 }
}
]
}
If you need your application to support searching by multiple fields,
you can create an index with multiple fields in the terms
definition.
The following example creates an index on the make
and model
fields
in the cars
collection:
{
ref: Index("cars_by_make_and_model"),
ts: 1641943522140000,
active: true,
serialized: true,
name: "cars_by_make_and_model",
source: Collection("cars"),
terms: [
{ field: ["data", "make"] },
{ field: ["data", "model"] }
],
partitions: 1
}
Queries against an index with multiple terms must include an array of
search criteria. The criteria are compared against the fields in the
terms
field in the order in which they are listed. The following
example searches for documents in the cars
collection which match
Toyota
in the make
field and Camry
in the model
field:
{
data: [
{
ref: Ref(Collection("cars"), "3"),
ts: 1645226205590000,
data: { make: 'Toyota', model: 'Camry', doors: 4 }
}
]
}
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!