Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
15 changes: 14 additions & 1 deletion source/includes/model-data/indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +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
from django_mongodb_backend.indexes import SearchIndex, VectorSearchIndex

class Nutrition(EmbeddedModel):
calories = models.IntegerField(default=0)
Expand All @@ -15,6 +15,7 @@ class Recipe(models.Model):
cuisine = models.CharField(max_length=200)
cook_time = models.IntegerField(default=0)
allergens = ArrayField(models.CharField(max_length=100), null=True, blank=True)
ratings = ArrayField(models.IntegerField(default=0), size=10)
nutrition = EmbeddedModelField(Nutrition, null=True, blank=True)

class Meta:
Expand Down Expand Up @@ -72,6 +73,18 @@ class Meta:
]
# end-atlas-search

# start-vector-search
class Meta:
db_table = "recipes"
indexes = [
VectorSearchIndex(
name=["vector_search_idx"],
fields=["ratings", "cook_time"],
similarities=["cosine", "euclidean"],
)
]
# end-vector-search

# start-partial
class Meta:
db_table = "recipes"
Expand Down
43 changes: 43 additions & 0 deletions source/model-data/indexes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ This section shows how to create the following advanced
index types:

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

Expand Down Expand Up @@ -240,6 +241,48 @@ an Atlas Search index named ``"title_search_idx"`` on the ``title`` field:
- `SearchIndex <{+api+}ref/models/indexes/#searchindex>`__ class in the
{+django-odm+} API documentation.

.. _django-indexes-vector-search:

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

Atlas Vector Search indexes allow you to query data based on its semantic meaning
rather than by keyword matches. You can integrate vector search with full-text search
queries and AI frameworks to support a range of use cases.

To create a vector search index, assign the ``indexes`` option in your model's
``Meta`` class to a ``VectorSearchIndex`` object. Pass the following arguments to the
``VectorSearchIndex()`` constructor:

- ``name``: *(Optional)* The name of your Atlas Vector Search index. If you do not
specify this argument, {+framework+} automatically generates an index name.
- ``fields``: The fields you want to index. At least one must be a vector
field, represented by an array of ``FloatField`` or ``IntegerField`` values and an
Copy link
Collaborator

Choose a reason for hiding this comment

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

"and an array size" -> "with a size"
(It would be really helpful to get intersphinx working to https://django-mongodb-backend.readthedocs.io/ so you can have size link to ArrayField.size) If you need help, let me know.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Unfortunately additional changes to the intersphinx links are stalled, I can't configure it myself - the docs platform team does that - and they have some higher priority work at the moment. I added a link to the API docs using the source constant instead

array size.
- ``similarities``: The :atlas:`similarity function </atlas-vector-search/vector-search-type/#std-label-avs-similarity-functions>`
to use. You can use the ``"cosine"``, ``"dotProduct"``, or ``"euclidean"``
similarity function. Specify the function as a single string value or a list
of values that assigns functions to individual vector fields.

The following example updates the ``Recipe`` model's ``Meta`` class to create
a vector search index named ``"vector_search_idx"`` on the ``ratings`` vector field
and the ``cook_time`` numeric field:

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

.. tip::

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

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

.. _django-indexes-partial:

Partial Indexes
Expand Down
Loading