View as Markdown

Storage Search

The storage engine supports configurable indices that automatically index the content of storage objects written through the storageWrite API.

Only a subset of the storage object value is indexed as configured. Search these indices using the query syntax.

Nakama populates the configured indices at startup and holds the maximum number of configured entries plus a threshold. Once that threshold is surpassed, it automatically evicts the oldest entries.

These indices aren’t designed to hold indexed representations of all objects in a given collection. Instead, they enable complex queries to look up cohorts of users or entities for different use cases, such as offline matchmaking.

Note
The search system built on the Storage Engine is an eventually consistent system. Indexes are populated asynchronously, which may take a few milliseconds to reflect updates written to storage objects. By default, the search indexes fetch their results from the Storage Engine directly, so values always reflect the most up-to-date storage object state. When using index-only mode, results are returned directly from the index without an additional database read.

Creating a new index #

Create and configure indices through the runtimes.

Index names must be unique. Each index is tied to a collection, and you must specify the top-level keys of the object fields to index. If an object has none of those keys, it isn’t indexed.

Set key to index only storage objects with that specific key. Omit it (or pass an empty string) to index all object keys that match the collection and fields.

Set a maximum entry count for the index.

The optional sortableFields parameter specifies which indexed fields support custom sort ordering when listing results. These must be a subset of fields.

Multiple indices can share a collection as long as their names are unique. Nakama automatically indexes any objects that match an index’s configuration on storage write.

Index-only mode #

By default, when a query runs against an index, Nakama first resolves the matching index entries and then fetches the corresponding storage objects from the database. This ensures returned values are always complete and up-to-date.

When indexOnly is set to true at registration time, the list operation returns values directly from the index without reading from the database. This reduces database reads and can improve performance, but comes with an important caveat: the index only stores the fields that were specified at registration, so the returned objects may contain partial data — fields not included in fields will be absent from the response.

Listing from an index #

Query the index using the powerful query syntax that also powers the Nakama matchmaker.

To filter by indexed field values, prefix the field key with value..

Each index entry also includes these queryable values automatically, in addition to the configured fields:

  • update_time
  • key
  • user_id
  • version
  • create_time
  • read
  • write
  • collection

These fields don’t need the value. prefix in queries.

Sorting results #

Use the optional order parameter to sort results by storage object fields. The prefix - before a field name indicates descending order. Declare the fields you want to sort by in sortableFields when registering the index.

To sort results by the value of any of the sortable fields, their respective keys need to be prefixed with value..

Register a storage index filter #

Register a custom filtering function per index. This function is called for each storage object eligible for the index and must return a boolean value.

Return true to index the object. Return false to exclude it from the index and delete any previously indexed entry for it.

The registered function is also applied to all eligible entries when the index is populated.

Related Pages