Skip to content

Commit 092aeef

Browse files
authored
DOCSP-49758: Atlas search indexes (#29)
* DOCSP-49758: Atlas search indexes * edits * code highlight * edit * feature support page
1 parent 877e899 commit 092aeef

File tree

3 files changed

+48
-3
lines changed

3 files changed

+48
-3
lines changed

source/includes/model-data/indexes.py

Lines changed: 12 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,17 @@ class Meta:
6061
]
6162
# end-embedded
6263

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

source/limitations-upcoming.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,7 @@ Index Support
5454
- Planned GA Support
5555

5656
* - Atlas Search and Atlas Vector Search indexes
57-
- *Unsupported*. You cannot use the Django
58-
Indexes API to create these indexes, but you can use
59-
the PyMongo Driver by :ref:`exposing your MongoClient <django-client-operations>`.
57+
- ✓
6058
- ✓
6159

6260
* - Compound indexes

source/model-data/indexes.txt

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,9 +202,44 @@ 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, which is a
215+
full-text 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, {+framework+} 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-8
234+
235+
.. tip::
236+
237+
To learn more about Atlas Search queries and indexes, see the following resources:
238+
239+
- :atlas:`Atlas Search </atlas-search>` in the Atlas documentation.
240+
- `SearchIndex <{+api+}ref/models/indexes/#searchindex>`__ class in the
241+
{+django-odm+} API documentation.
242+
208243
.. _django-indexes-partial:
209244

210245
Partial Indexes

0 commit comments

Comments
 (0)