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 specify terms: these are zero or more scalar Values from indexed documents that help you to find specific documents. terms are comparable to column=value predicates in an SQL WHERE clause. For example, if your documents contain a name field, you can define terms to include that field, and then you can find all of the documents that match a specific name.

Only scalar Values are indexed. When a term points to a document field or index binding result that contains an array, one index entry per array item is created, which makes it easy to search for any 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’s terms field specifies two fields that are arrays, and a document is created with one array having 5 items and the second array having 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 specify 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’s type, and the order can be inverted by specifying reverse.

Each index entry records the Reference of each document involved in the index. When no values are specified, the index returns the Reference for each matching index entry. When one or more values are specified, only those 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 containing scalar Values. Objects are not indexed, so when a values definition points to a document field or index binding result that contains 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 contains 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 any terms.

If you do create a "term-less" index, the index can perform very poorly: every time a covered document is created or updated, the entire index (and its history) needs to be evaluated to determine 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!