API version: 0.3.0

---

# Indices

Indices cost compute and storage, so Neo4j and MongoDB only index a property if its definition asks for it with
`index`. Elasticsearch is the exception on purpose: it is the search engine of the API and is optimized for common
search queries, so it indexes every property by its type, unless the property sets `"search": false`. The value is
an intent, which describes what the index is for. The database which stores the value creates the best index it has for
it, so the same definition keeps working if the property moves to another database or the database is replaced.

| Intent     | Use                              | Properties                                                                        | Database                                                                                                                       |
| ---------- | -------------------------------- | --------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------ |
| `exact`    | exact values, ranges and sorting | `boolean`, `integer`, `number`, `string`, `datetime`, `elementId`                 | Neo4j (range index) or MongoDB (ascending index), Elasticsearch (keyword for a `string`, if named)                             |
| `fulltext` | the words of a string            | `string`                                                                          | Neo4j (full-text index) or MongoDB (text index), Elasticsearch (text, if named)                                                |
| `vector`   | nearest neighbors                | `vector`                                                                          | Elasticsearch (`dense_vector` index)                                                                                           |
| `geo`      | geometries by their position     | the [geo types](/reference/formats-and-languages/geojson.md#supported-geometries) | MongoDB (`2dsphere` index) or Neo4j (point index), Elasticsearch (`geo_point` or `geo_shape` mapping, no further index needed) |

Every intent has its own page: [exact](/reference/indices/exact.md), [fulltext](/reference/indices/fulltext.md),
[vector](/reference/indices/vector.md) and [geo](/reference/indices/geo.md). Indices which the intents do not cover, e.g.
composite indices of Neo4j or exact control over the mapping of Elasticsearch, are described in
[raw indices](/reference/indices/raw-indices.md).

`exact`, `fulltext` and `geo` are created by the database which stores the value: Neo4j for the properties which it
stores (short strings, numbers, booleans, dates and points with `"storage": "graph"`), MongoDB for everything else, see
[placement](/reference/schemas/placement.md). `vector` is always an index of Elasticsearch.

## Nested properties

Properties inside objects and lists are indexed like the ones at the top level, `index` is written where the property
is defined:

```json
{
  "address": {
    "type": "object",
    "properties": {
      "city": {
        "type": "string",
        "index": [
          "exact",
          "fulltext"
        ]
      },
      "location": {
        "type": "point",
        "index": "geo"
      }
    }
  },
  "authors": {
    "type": "list",
    "items": {
      "type": "object",
      "properties": {
        "name": {
          "type": "string",
          "index": "exact"
        }
      }
    }
  },
  "tags": {
    "type": "list",
    "items": {
      "type": "string",
      "index": "exact"
    }
  }
}
```

Nested values are stored in MongoDB, which indexes them by their path, e.g. `address.city` or `authors.name`: lists need
no segment of their own, MongoDB indexes every element of a list. The indices are named
`ens_<type>_<property>_<intent>_<hash>`, e.g. `ens_place_address_city_exact_1a2b3c4d`, see [index names](#index-names).
MongoDB allows one text index per collection, so all properties of a type with `fulltext` in MongoDB share the text
index `ens_<type>_fulltext_<hash>`; adding or removing one recreates it. Elasticsearch indexes nested properties if it
is named, e.g. `"index": {"default": "exact", "elasticsearch": "exact"}`.

Neo4j does not store nested properties, naming `neo4j` for one is rejected with `400 Bad Request`, like naming a
database which does not store the property at all, e.g. `mongodb` for a property with `"storage": "graph"`. The further
properties of `additionalProperties` have no names and can not be indexed.

A property can have several indices, give `index` a list of intents. A `string` which is looked up by exact value and
searched as text has `"index": ["exact", "fulltext"]`, Neo4j then keeps a range index and a full-text index for it.

To control the databases individually, `index` can also be an object, which maps `default` (all databases without an
entry) and the databases (`neo4j`, `mongodb`, `elasticsearch`) to an intent, a list of intents or `false`. The databases
must provide the intent and, except for Elasticsearch, store the value; requesting an index which a database does not
provide is rejected:

```json
{
  "type": "string",
  "index": {
    "default": "exact",
    "neo4j": false
  }
}
```

Elasticsearch needs no intent for most properties. This is intended: as the search engine of the API it is optimized for
common search queries, which should work without a schema. It maps every `string` so that full text and exact lookups
both work (a text field with a `keyword` sub-field, like Elasticsearch does for dynamic mappings, see
[its limits](/reference/databases/elasticsearch.md#limits)), and the other types by their own. It only maps a `string` more
narrowly if the definition names `elasticsearch`: `fulltext` maps text only, `exact` maps a keyword only and both
together map the text field with its `keyword` sub-field. An existing field can not be changed in place, see
[migrations](/reference/schemas/migrations.md).

```json
{
  "type": "string",
  "index": {
    "neo4j": [
      "exact",
      "fulltext"
    ],
    "elasticsearch": "fulltext"
  }
}
```

Changing the `index` of a property changes what the next migration plans, the existing index is replaced if its kind
differs.

## Index names

Every index which a schema creates is named `ens_<type>_<property>_<intent>_<hash>` in Neo4j and MongoDB, e.g.
`ens_place_address_city_exact_1a2b3c4d`. Type and property are lowercased, every character outside of `a-z`, `0-9`
and `_` becomes `_`, and each is cut to 32 characters, so the names stay readable, short and valid in both databases.
Raw indices use `raw` (a raw index of a property) or `composite` (a raw index of the schema, with its `name` as the
property) instead of the intent, the text index of MongoDB has no property: `ens_<type>_fulltext_<hash>`.

The hash, 8 hexadecimal characters of xxh3, covers the exact kind of the index, type, property path, intent and options.
That keeps names unique where the readable part is not, e.g. for `a-b` and `a_b`, or for very long names, and a changed
option renames the index. Names are never looked up in a table: the planner of a migration calculates them from the
schema. Only indices with the prefix `ens_` are managed by schemas, all other indices are never touched by migrations.
The indices on the `id` of every type have the prefix `eni_` and are managed by [`index:ensure`](/command/index-ensure.md),
with the same rules for their names. [`database:drop`](/command/database-drop.md) removes the indices of both prefixes.

---

Home: [Ember Nexus API docs](/index.md)

Full site map: [llms.txt](/llms.txt)

Previous: [HAS_MIGRATION](/reference/schemas/built-in-schemas/has-migration.md)

Next: [Exact indices](/reference/indices/exact.md)
