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 1 commit
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
10 changes: 10 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,15 @@ class Meta:
]
# end-embedded

# start-atlas-search
class Meta:
db_table = "recipes"
indexes = [
SearchIndex(fields=["title"],
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is the indent style that Django uses:

SearchIndex(
    fields=["title"],
    name="title_search_idx",
)

name="title_search_idx"),
]
# end-atlas-search

# start-partial
class Meta:
db_table = "recipes"
Expand Down
32 changes: 32 additions & 0 deletions source/model-data/indexes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,41 @@ 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, or a full-text
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm a bit naive on this functionaltiy, but I'm not sure what "or" means here. Does it mean "in other words"?

https://www.mongodb.com/docs/atlas/atlas-search/ says "Atlas Search is an embedded full-text search"

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, "in other words." I changed to "which is" for clarity

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, {+django-odm+} automatically generates an index name.
Copy link
Collaborator

Choose a reason for hiding this comment

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

It's actually the behavior of Django to automatically generate an index name rather than Django Mongo Backend.


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-6

.. tip::

To learn more about Atlas Search queries and indexes, see :atlas:`Atlas Search </atlas-search>`
in the Atlas documentation.

.. _django-indexes-partial:

Partial Indexes
Expand Down
Loading