Skip to content

Commit 794ab48

Browse files
authored
DOCSP-49759: Vector search index (#33)
* DOCSP-49759: Vector search index * edits * LM feedback * fix * link * fix link
1 parent 82d1787 commit 794ab48

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

source/includes/model-data/indexes.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from django.db.models import Q, F
44
from django_mongodb_backend.models import EmbeddedModel
55
from django_mongodb_backend.fields import EmbeddedModelField, ArrayField
6-
from django_mongodb_backend.indexes import SearchIndex
6+
from django_mongodb_backend.indexes import SearchIndex, VectorSearchIndex
77

88
class Nutrition(EmbeddedModel):
99
calories = models.IntegerField(default=0)
@@ -15,6 +15,7 @@ class Recipe(models.Model):
1515
cuisine = models.CharField(max_length=200)
1616
cook_time = models.IntegerField(default=0)
1717
allergens = ArrayField(models.CharField(max_length=100), null=True, blank=True)
18+
ratings = ArrayField(models.IntegerField(default=0), size=10)
1819
nutrition = EmbeddedModelField(Nutrition, null=True, blank=True)
1920

2021
class Meta:
@@ -72,6 +73,18 @@ class Meta:
7273
]
7374
# end-atlas-search
7475

76+
# start-vector-search
77+
class Meta:
78+
db_table = "recipes"
79+
indexes = [
80+
VectorSearchIndex(
81+
name=["vector_search_idx"],
82+
fields=["ratings", "cook_time"],
83+
similarities=["cosine", "euclidean"],
84+
)
85+
]
86+
# end-vector-search
87+
7588
# start-partial
7689
class Meta:
7790
db_table = "recipes"

source/model-data/indexes.txt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ This section shows how to create the following advanced
203203
index types:
204204

205205
- :ref:`django-indexes-atlas-search`
206+
- :ref:`django-indexes-vector-search`
206207
- :ref:`django-indexes-partial`
207208
- :ref:`django-indexes-unique`
208209

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

244+
.. _django-indexes-vector-search:
245+
246+
Atlas Vector Search Indexes
247+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
248+
249+
Atlas Vector Search indexes allow you to query data based on its semantic meaning
250+
rather than by keyword matches. You can integrate vector search with full-text search
251+
queries and AI frameworks to support a range of use cases.
252+
253+
To create a vector search index, assign the ``indexes`` option in your model's
254+
``Meta`` class to a ``VectorSearchIndex`` object. Pass the following arguments to the
255+
``VectorSearchIndex()`` constructor:
256+
257+
- ``name``: *(Optional)* The name of your Atlas Vector Search index. If you do not
258+
specify this argument, {+framework+} automatically generates an index name.
259+
- ``fields``: The fields you want to index. At least one must be a vector
260+
field, represented by an array of ``FloatField`` or ``IntegerField`` values with
261+
a `size <{+api+}ref/models/fields/#django_mongodb_backend.fields.ArrayField.size>`__.
262+
- ``similarities``: The :atlas:`similarity function </atlas-vector-search/vector-search-type/#std-label-avs-similarity-functions>`
263+
to use. You can use the ``"cosine"``, ``"dotProduct"``, or ``"euclidean"``
264+
similarity function. Specify the function as a single string value or a list
265+
of values that assigns functions to individual vector fields.
266+
267+
The following example updates the ``Recipe`` model's ``Meta`` class to create
268+
a vector search index named ``"vector_search_idx"`` on the ``ratings`` vector field
269+
and the ``cook_time`` numeric field:
270+
271+
.. literalinclude:: /includes/model-data/indexes.py
272+
:start-after: start-vector-search
273+
:end-before: end-vector-search
274+
:language: python
275+
:copyable:
276+
:emphasize-lines: 3-9
277+
278+
.. tip::
279+
280+
To learn more about Atlas Vector Search queries and indexes, see the following resources:
281+
282+
- :atlas:`Atlas Vector Search </atlas-vector-search>` in the Atlas documentation.
283+
- `VectorSearchIndex <{+api+}ref/models/indexes//#vectorsearchindex>`__ class in the
284+
{+django-odm+} API documentation.
285+
243286
.. _django-indexes-partial:
244287

245288
Partial Indexes

0 commit comments

Comments
 (0)