Skip to content

Commit d83672a

Browse files
committed
DOCSP-49758: Atlas search indexes
1 parent 7b2d67b commit d83672a

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

source/includes/model-data/indexes.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from django.db.models import Q, F
44
from django_mongodb_backend.models import EmbeddedModel
55
from django_mongodb_backend.fields import EmbeddedModelField, ArrayField
6+
from django_mongodb_backend.indexes import SearchIndex
67

78
class Nutrition(EmbeddedModel):
89
calories = models.IntegerField(default=0)
@@ -60,6 +61,15 @@ class Meta:
6061
]
6162
# end-embedded
6263

64+
# start-atlas-search
65+
class Meta:
66+
db_table = "recipes"
67+
indexes = [
68+
SearchIndex(fields=["title"],
69+
name="title_search_idx"),
70+
]
71+
# end-atlas-search
72+
6373
# start-partial
6474
class Meta:
6575
db_table = "recipes"

source/model-data/indexes.txt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,9 +202,41 @@ Advanced Index Configuration
202202
This section shows how to create the following advanced
203203
index types:
204204

205+
- :ref:`django-indexes-atlas-search`
205206
- :ref:`django-indexes-partial`
206207
- :ref:`django-indexes-unique`
207208

209+
.. _django-indexes-atlas-search:
210+
211+
Atlas Search Indexes
212+
~~~~~~~~~~~~~~~~~~~~
213+
214+
Atlas Search indexes specify the behavior of an Atlas Search, or a full-text
215+
search on collections hosted on MongoDB Atlas.
216+
217+
To create an Atlas Search index, assign the ``indexes`` option in your model's
218+
``Meta`` class to a ``SearchIndex`` object. Pass the following arguments to the
219+
``SearchIndex()`` constructor:
220+
221+
- ``fields``: The fields you want to index.
222+
- ``name``: *(Optional)* The name of your Atlas Search index. If you do not
223+
specify this argument, {+django-odm+} automatically generates an index name.
224+
225+
The following example updates the ``Recipe`` model's ``Meta`` class to create
226+
an Atlas Search index named ``"title_search_idx"`` on the ``title`` field:
227+
228+
.. literalinclude:: /includes/model-data/indexes.py
229+
:start-after: start-atlas-search
230+
:end-before: end-atlas-search
231+
:language: python
232+
:copyable:
233+
:emphasize-lines: 3-6
234+
235+
.. tip::
236+
237+
To learn more about Atlas Search queries and indexes, see :atlas:`Atlas Search </atlas-search>`
238+
in the Atlas documentation.
239+
208240
.. _django-indexes-partial:
209241

210242
Partial Indexes

0 commit comments

Comments
 (0)