From 0f45c4ad1511bd3142a3c72b2ee17b9faec7426d Mon Sep 17 00:00:00 2001 From: norareidy Date: Wed, 21 May 2025 11:21:42 -0400 Subject: [PATCH 1/6] DOCSP-49759: Vector search index --- source/includes/model-data/indexes.py | 15 +++++++++- source/model-data/indexes.txt | 42 +++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) 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..69271d5 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,47 @@ 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: + +Vector Search Indexes +~~~~~~~~~~~~~~~~~~~~~ + +Vector Search indexes specify the behavior of an Atlas Vector Search, which allows +you to query data based on its semantic meaning rather than only 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 Vector Search index. If you do not + specify this argument, {+framework+} automatically generates an index name. +- ``fields``: The fields you want to index. +- ``similarities``: The :atlas:`similarity function ` + to use. You can specify the ``"cosine"``, ``"dotProduct"``, or ``"euclidean"`` + similarity function, as a single string value or a list of values that assigns + different 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`` and ``cook_time`` +fields: + +.. 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 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 From b0ee58741b55b995fe84e3e16c5f049192e3ae75 Mon Sep 17 00:00:00 2001 From: norareidy Date: Wed, 21 May 2025 13:10:50 -0400 Subject: [PATCH 2/6] edits --- source/model-data/indexes.txt | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/source/model-data/indexes.txt b/source/model-data/indexes.txt index 69271d5..f5ed832 100644 --- a/source/model-data/indexes.txt +++ b/source/model-data/indexes.txt @@ -247,7 +247,7 @@ Vector Search Indexes ~~~~~~~~~~~~~~~~~~~~~ Vector Search indexes specify the behavior of an Atlas Vector Search, which allows -you to query data based on its semantic meaning rather than only keyword matches. +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. @@ -257,15 +257,17 @@ To create a Vector Search index, assign the ``indexes`` option in your model's - ``name``: *(Optional)* The name of your Vector Search index. If you do not specify this argument, {+framework+} automatically generates an index name. -- ``fields``: The fields you want to index. +- ``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 + array size. - ``similarities``: The :atlas:`similarity function ` - to use. You can specify the ``"cosine"``, ``"dotProduct"``, or ``"euclidean"`` - similarity function, as a single string value or a list of values that assigns - different functions to individual vector fields. + 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`` and ``cook_time`` -fields: +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 From 4a5b20b5bbc95fa0f6ee9e7db983a681be318a88 Mon Sep 17 00:00:00 2001 From: norareidy Date: Wed, 21 May 2025 15:09:54 -0400 Subject: [PATCH 3/6] LM feedback --- source/model-data/indexes.txt | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/source/model-data/indexes.txt b/source/model-data/indexes.txt index f5ed832..17bc14e 100644 --- a/source/model-data/indexes.txt +++ b/source/model-data/indexes.txt @@ -243,19 +243,18 @@ an Atlas Search index named ``"title_search_idx"`` on the ``title`` field: .. _django-indexes-vector-search: -Vector Search Indexes -~~~~~~~~~~~~~~~~~~~~~ +Atlas Vector Search Indexes +~~~~~~~~~~~~~~~~~~~~~~~~~~~ -Vector Search indexes specify the behavior of an Atlas Vector Search, which allows -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. +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 +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 Vector Search index. If you do not +- ``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 @@ -266,7 +265,7 @@ To create a Vector Search index, assign the ``indexes`` option in your model's 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 +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 @@ -278,7 +277,7 @@ and the ``cook_time`` numeric field: .. tip:: - To learn more about Vector Search queries and indexes, see the following resources: + 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 From 392abc366f543c3de657c71e67eb56168ce0761a Mon Sep 17 00:00:00 2001 From: norareidy Date: Thu, 22 May 2025 16:42:44 -0400 Subject: [PATCH 4/6] fix --- source/model-data/indexes.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/model-data/indexes.txt b/source/model-data/indexes.txt index 17bc14e..4975d3a 100644 --- a/source/model-data/indexes.txt +++ b/source/model-data/indexes.txt @@ -257,8 +257,8 @@ To create a vector search index, assign the ``indexes`` option in your model's - ``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 - array size. + field, represented by an array of ``FloatField`` or ``IntegerField`` values with + a 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 From 00d0c05292c3c29b75eaa010f7dced4f12de740d Mon Sep 17 00:00:00 2001 From: norareidy Date: Thu, 22 May 2025 16:47:12 -0400 Subject: [PATCH 5/6] link --- source/model-data/indexes.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/model-data/indexes.txt b/source/model-data/indexes.txt index 4975d3a..a7800c7 100644 --- a/source/model-data/indexes.txt +++ b/source/model-data/indexes.txt @@ -258,7 +258,7 @@ To create a vector search index, assign the ``indexes`` option in your model's 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. + 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 From 3b9bb4c3a9ef67fd20f4cac97a240622b3808bac Mon Sep 17 00:00:00 2001 From: norareidy Date: Thu, 22 May 2025 16:51:00 -0400 Subject: [PATCH 6/6] fix link --- source/model-data/indexes.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/model-data/indexes.txt b/source/model-data/indexes.txt index a7800c7..4354162 100644 --- a/source/model-data/indexes.txt +++ b/source/model-data/indexes.txt @@ -258,7 +258,7 @@ To create a vector search index, assign the ``indexes`` option in your model's 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>__`. + 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