Skip to content

DOCSP-49758: Atlas search indexes #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions source/includes/model-data/indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from django.db.models import Q, F
from django_mongodb_backend.models import EmbeddedModel
from django_mongodb_backend.fields import EmbeddedModelField, ArrayField
from django_mongodb_backend.indexes import SearchIndex

class Nutrition(EmbeddedModel):
calories = models.IntegerField(default=0)
Expand Down Expand Up @@ -60,6 +61,17 @@ class Meta:
]
# end-embedded

# start-atlas-search
class Meta:
db_table = "recipes"
indexes = [
SearchIndex(
fields=["title"],
name="title_search_idx",
)
]
# end-atlas-search

# start-partial
class Meta:
db_table = "recipes"
Expand Down
2 changes: 0 additions & 2 deletions source/limitations-upcoming.txt
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ Indexes

{+django-odm+} does not support the following index functionalities:

- Creating ``$vectorSearch`` and ``$search`` indexes through the Django
Indexes API
- Creating geospatial indexes through the Django Indexes API
- Updating indexes in ``EmbeddedModelFields`` after model creation

Expand Down
35 changes: 35 additions & 0 deletions source/model-data/indexes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,44 @@ Advanced Index Configuration
This section shows how to create the following advanced
index types:

- :ref:`django-indexes-atlas-search`
- :ref:`django-indexes-partial`
- :ref:`django-indexes-unique`

.. _django-indexes-atlas-search:

Atlas Search Indexes
~~~~~~~~~~~~~~~~~~~~

Atlas Search indexes specify the behavior of an Atlas Search, which is a
full-text search on collections hosted on MongoDB Atlas.

To create an Atlas Search index, assign the ``indexes`` option in your model's
``Meta`` class to a ``SearchIndex`` object. Pass the following arguments to the
``SearchIndex()`` constructor:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be great to add interphinx support for django_mongodb_backend, e.g.

:class:`~django_mongodb_backend.indexes.SearchIndex`

(could be done separately)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a link to the SearchIndex class to the tip at the end of this section


- ``fields``: The fields you want to index.
- ``name``: *(Optional)* The name of your Atlas Search index. If you do not
specify this argument, {+framework+} automatically generates an index name.

The following example updates the ``Recipe`` model's ``Meta`` class to create
an Atlas Search index named ``"title_search_idx"`` on the ``title`` field:

.. literalinclude:: /includes/model-data/indexes.py
:start-after: start-atlas-search
:end-before: end-atlas-search
:language: python
:copyable:
:emphasize-lines: 3-8

.. tip::

To learn more about Atlas Search queries and indexes, see the following resources:

- :atlas:`Atlas Search </atlas-search>` in the Atlas documentation.
- `SearchIndex <{+api+}ref/models/indexes/#searchindex>`__ class in the
{+django-odm+} API documentation.

.. _django-indexes-partial:

Partial Indexes
Expand Down
Loading