Indexes
An understanding of index creation and usage is crucial for effective Fauna development. Indexes allow for the organization and retrieval of documents by attributes other than their Reference. They act as a lookup table that improves the performance of finding documents: instead of reading every single document to find the one(s) that you are interested in, you query an index to find those documents. To learn more about how Fauna indexes work, see the Index tutorials. For examples of index creation and usage, see Index recipes.
See limits for details on concurrent index builds and
transaction limits. See the CreateIndex reference page for
limitations on what an index may be named.
|
Source
When you create an index, you specify its source
, which is one or more
collections of documents. Once the index
is active, any query that creates, updates, or deletes a document in the
source
collection(s) causes the index to be updated.
Terms
An index can define terms
: these are zero or more term objects that
define which document fields to use for searching.
terms
are comparable to column=value
predicates in an SQL WHERE
clause. For example, if your documents have a name
field, you can
define terms
to include that field, and then you can find all of the
documents that match a name
.
Only scalar Values are indexed. When a term targets a document field or index binding result that has an array, one index entry per array item is created, which makes it easy to search for an array item. Objects are not indexed. As a result, it is not possible to search for arrays or objects.
Be aware that when the terms
definition includes multiple array
fields, the number of index entries created is the Cartesian product of
the number of array items. For example, when an index terms
definition
specifies two fields that are arrays, and a document is created
including one array with 5 items and the second array with 11 items, 55
index entries are created. Index
write operations are
grouped together, so the billing impact depends on the overall size of
the index entries.
When an index has one or more terms
, the index is partitioned by the
terms
, allowing Fauna to efficiently scale indexes.
When a document is indexed, and all of the index’s defined terms
evaluate to null
, no index entry is stored for the document.
Values
An index can define values
: these are zero or more scalar
Values returned for each index entry that matches the terms
when you query the index. values
are comparable to the SQL SELECT
clause.
values
are also how indexes are sorted: each field value in values
is sorted lexically according to the field type, and the order can be
inverted by setting reverse: true
.
Each index entry records the Reference of each document involved in the
index. When there is no values
definition, the index returns the
Reference for each matching index entry. When values
is defined, only
the defined field values are returned.
When a document is indexed, and all of the index’s defined values
evaluate to null
, no index entry is stored for the document.
Values must refer to fields with scalar Values. Objects are
not indexed, so when a values
definition points to a document field or
index binding result that has an Object, the index entry stores
null
because Objects cannot be sorted. When a values
definition points to a document field or index binding result that
has an Array, one index entry per array item is created.
Collection index
An index with no terms
and values
specified is known as a
collection index: searching for specific documents is not possible,
and all documents within the collection are included in the result set,
and are sorted by their reference in ascending order.
Unique
You can specify that an index is unique
. This means that, for the
defined terms
and values
, the index contains only one entry for a
document having those specific terms
and values
. As a result,
creating or updating a document to have the same terms
and values
as
an existing document would cause an error.
Avoid creating a unique index that does not define If you do create a "term-less" index, the index could cause performance issues. Every time a covered document is created or updated, the index (and its history) needs to be evaluated to decide whether the document is unique or not. As the index grows larger, the evaluation for uniqueness can cause your queries involving writes to exceed the query timeout. |
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!