Index definitions

Defines an index.

An index stores, or covers, specific document field values for quick retrieval. Using indexes can significantly improve query performance and reduce costs, especially for large datasets.

You include index definitions in a collection schema.

Syntax

index <indexName> {
  [terms [<term> . . .]]
  [values [<value> . . .]]
}

Name

indexName String Required

Unique index name.

Properties

Property Type Required Description

terms

Array of field accessors

Yes, if values is not defined.

Fields used for exact match searches.

Supports dot notation and bracket notation. You can only index persistable field values.

Use mva() to index an Array field’s values.

values

Array of field accessors

Yes, if terms is not defined.

Fields used for sorting and range searches.

Supports dot notation and bracket notation. You can only index persistable field values.

Use mva() to index an Array field’s values.

Use asc() or desc() to sort results in respective ascending or descending order. Defaults to asc (ascending).

Examples

collection Product {
  ...
  index byName {
    terms [.name]
    values [desc(.quantity), desc(mva(.categories))]
  }
  ...
}

Don’t index document IDs

When indexing a field that contains document, index the entire field, not the document ID:

collection Product {
  ...
  // The `store` field accepts `Store` collection documents.
  store: Ref<Store>
  ...

  // Correct:
  index byStore {
    terms [.store]
  }

  // Incorrect:
  // Indexing `store.id` fails.
  // Instead, index by the entire `store`field.
  index byStoreId {
    terms [.store.id]
  }
}

IDs aren’t persistable and can’t be indexed.

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!