Model index reference

Some MongoDB-specific indexes, for use on a model’s Meta.indexes option, are available in django_mongodb_backend.indexes.

Search indexes

MongoDB creates these indexes asynchronously, however, Django’s AddIndex and RemoveIndex operations will wait until the index is created or deleted so that the database state is consistent in the operations that follow. Adding indexes may take seconds or minutes, depending on the size of the collection.

Changed in version 5.2.1: The aforementioned waiting was added.

SearchIndex

class SearchIndex(fields=(), field_mappings=None, name=None, analyzer=None, search_analyzer=None)

Creates a basic search index on the given field(s).

Some fields such as DecimalField aren’t supported. See the Atlas documentation for a complete list of unsupported data types.

Use field_mappings (instead of fields) to create a complex search index. field_mappings is a dictionary that maps field names to index options. It corresponds to definition["mappings"]["fields"] in the Static Mapping Example.

If name isn’t provided, one will be generated automatically. If you need to reference the name in your search query and don’t provide your own name, you can lookup the generated one using Model._meta.indexes[0].name (substituting the name of your model as well as a different list index if your model has multiple indexes).

Use analyzer and search_analyzer to configure the indexing and searching analyzer. If these options aren’t provided, the server defaults to lucene.standard. It corresponds to definition["analyzer"] and definition["searchAnalyzer"] in the Static Mapping Example.

Changed in version 5.2.2: The field_mappings, analyzer, and search_analyzer arguments were added.

VectorSearchIndex

class VectorSearchIndex(*, fields=(), name=None, similarities)

A subclass of SearchIndex that creates a vector search index on the given field(s).

The index must reference at least one vector field: an ArrayField with a base_field of FloatField or IntegerField and a size. It cannot reference an ArrayField of any other type.

It may also have other fields to filter on, provided the field stores boolean, date, objectId, numeric, string, or uuid.

Available values for the required similarities keyword argument are "cosine", "dotProduct", and "euclidean" (see About the Similarity Functions for how to choose). You can provide this value either a string, in which case that value will be applied to all vector fields, or a list or tuple of values with a similarity corresponding to each vector field.