Indexes
reference: |
Indexes are the preferred way to query your data. They’re effectively
a lookup table for organizing and retrieving documents by attributes instead
of by reference. When you use an index, Fauna uses index terms
and
values
to search instead of scanning every document to find a match for
your query. When you cover a value, Fauna doesn’t have to look up the
source document.
An index exists as a method of the collection for which it is defined.
Defining an index defines a method on the collection. Querying an
index returns a set of documents that match your index definition.
For example, if you create an index named priceRange
in the Product
collection, an indexed query on priceRange()
has the following form:
Product.priceRange()
Only documents that match the priceRange
index definition
are returned.
Index properties
Two fields are used to define an index:
-
terms
-
values
Terms
Including the terms
field in an index allows you to select one or
more document fields to be searchable in the index. For example,
consider a collection called People
in which documents have fields
called first_name
and last_name
. If you include those fields in the
terms
of an index on the People
collection, you can run queries
that search for matches in those fields.
When an index has one or more terms, the index is partitioned by the
terms
, which allows Fauna to scale indexes efficiently.
The resulting index method on the collection takes as many arguments as terms the index is defined with. Calling an index method with arguments returns the set of documents contained in the index whose corresponding field values match the arguments.
The following document field types can be indexed as terms:
Values
Index values
define which document values should be
used to select matching documents to return in the result set. It defines
values you want to cover. For example, if you want to sort query results
from a Customer
collection by last_name
, define last_name
in the
values
field:
Customer.definition!.update({
indexes: {
sortedOnLastname: {
values: [
{
field: "lastname",
order: "asc"
}
]
}
}
})
The example also defines an ascending, asc
, sort order.
When you query a field covered by an index, Fauna doesn’t
have to fetch the source document to get the covered value. When an index
has all the values
a query requires, that query is considered
fully covered. A fully covered query is the most efficient query.
The default document order in a set returned by an index is the
ascending order of the values for the field. You can use the order
field
to define ascending, asc
, or descending, desc
, order.
The values
field also allows you to query on range to get only those
documents in the requested range. Range notation has the following format:
{ from: <startValue>, to: <endValue> }
<startValue>
and <endValue>
are optional.
For example, if you create a priceRange
index with a price
field for
the Product
collection,
Product.definition.update({
indexes: {
"priceRange": {
"values": [{ "field": "price" }]
}
})
For example, to get all Product
collection documents with a price
value between 100
and 150
, inclusive, submit the following query
against the priceRange
index:
Product.priceRange({ from: 100, to: 150 })
Constraints
You can define unique constraints are defined on a document using the constraints field, which takes an array of unique constraints.
When one or more fields are part of a unique constraint, multiple documents can’t have the same values for the constrained fields. Constraints aren’t applied retroactively to existing documents but are applied only when a new document is created or an existing document is updated.
You can declare any number of constraints that define which Document
field values must be unique in a Collection. A unique constraint can
define one or more fields that must be unique in combination.
For example, if firstName
and lastName
fields are included in a unique
definition, the first and last name combination must be unique in the
collection.
If you create a unique constraint over more than one field, the absence of
one or more fields with the presence of other fields in the unique constraint
is a combination that the constraint is enforced over. In the firstName
/
lastName
example, if you create a user with only a lastName
, you can’t
create another user with only that lastName
, but you could if you add a
firstName
.
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!