diff --git a/source/includes/model-data/indexes.py b/source/includes/model-data/indexes.py index 33649ad..b3ed774 100644 --- a/source/includes/model-data/indexes.py +++ b/source/includes/model-data/indexes.py @@ -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) @@ -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: @@ -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" diff --git a/source/model-data/indexes.txt b/source/model-data/indexes.txt index 84120e5..4354162 100644 --- a/source/model-data/indexes.txt +++ b/source/model-data/indexes.txt @@ -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` @@ -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 with + a `size <{+api+}ref/models/fields/#django_mongodb_backend.fields.ArrayField.size>`__. +- ``similarities``: The :atlas:`similarity function ` + 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 ` in the Atlas documentation. + - `VectorSearchIndex <{+api+}ref/models/indexes//#vectorsearchindex>`__ class in the + {+django-odm+} API documentation. + .. _django-indexes-partial: Partial Indexes