Skip to content

Commit 6b84168

Browse files
committed
edits
1 parent c5a45fb commit 6b84168

File tree

7 files changed

+37
-21
lines changed

7 files changed

+37
-21
lines changed

.github/workflows/test-python-atlas.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ on:
55
paths:
66
- '**.py'
77
- '!setup.py'
8-
- '.github/workflows/test-python.yml'
8+
- '.github/workflows/test-python-atlas.yml'
99
workflow_dispatch:
1010

1111
concurrency:

django_mongodb_backend/checks.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@
66

77

88
def check_indexes(app_configs, databases=None, **kwargs): # noqa: ARG001
9-
# Validate vector search indexes for models.
9+
"""
10+
Call Index.check() on all model indexes.
11+
12+
This function will be obsolete when Django calls Index.check() after
13+
https://code.djangoproject.com/ticket/36273.
14+
"""
1015
errors = []
1116
if app_configs is None:
1217
models = apps.get_models()
@@ -24,4 +29,4 @@ def check_indexes(app_configs, databases=None, **kwargs): # noqa: ARG001
2429

2530

2631
def register_checks():
27-
register(Tags.models)(check_indexes)
32+
register(check_indexes, Tags.models)

django_mongodb_backend/features.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -629,10 +629,13 @@ def is_mongodb_6_3(self):
629629

630630
@cached_property
631631
def supports_atlas_search(self):
632+
"""Does the server support Atlas search queries and search indexes?"""
632633
try:
633-
# Check search indexes support (raises if unsupported).
634+
# An existing collection must be used on MongoDB 6, otherwise
635+
# the operation will not error.
634636
self.connection.get_collection("django_migrations").list_search_indexes()
635637
except OperationFailure:
638+
# Error: $listSearchIndexes stage is only allowed on MongoDB Atlas
636639
return False
637640
else:
638641
return True

django_mongodb_backend/indexes.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,11 @@ def check(self, model, connection):
125125
)
126126
return errors
127127

128-
# Maps Django internal type to atlas search index type.
129-
# Reference: https://www.mongodb.com/docs/atlas/atlas-search/define-field-mappings/#data-types
130128
def search_index_data_types(self, field, db_type):
129+
"""
130+
Map a model field's internal type to search index type.
131+
Reference: https://www.mongodb.com/docs/atlas/atlas-search/define-field-mappings/#data-types
132+
"""
131133
if field.get_internal_type() == "UUIDField":
132134
return "uuid"
133135
if field.get_internal_type() in ("ObjectIdAutoField", "ObjectIdField"):

docs/source/conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"mongodb": ("https://www.mongodb.com/docs/languages/python/django-mongodb/v5.1/", None),
4545
"pymongo": ("https://pymongo.readthedocs.io/en/stable/", None),
4646
"python": ("https://docs.python.org/3/", None),
47+
"atlas": ("https://www.mongodb.com/docs/atlas/", None),
4748
}
4849

4950
root_doc = "contents"

docs/source/ref/models/indexes.rst

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,27 @@ Some MongoDB-specific indexes are available in
1111
``SearchIndex``
1212
===============
1313

14-
.. class:: SearchIndex(*expressions, **kwargs)
14+
.. class:: SearchIndex(fields=(), name=None)
1515

16-
...
16+
Creates a basic :doc:`search index <atlas:atlas-search/index-definitions>` on
17+
the given field(s).
1718

19+
If ``name`` isn't provided, one will be generated automatically. If you need
20+
to reference the name in your search query and don't provide your own name,
21+
you can lookup the generated one using: ``Model._meta.indexes[0].name``
22+
(substiting a different index as needed if your model has multiple indexes).
1823

1924
``VectorSearchIndex``
2025
=====================
2126

22-
.. class:: VectorSearchIndex(*expressions, similarities="cosine", **kwargs)
27+
.. class:: VectorSearchIndex(fields=(), similarities="cosine", name=None)
28+
29+
A subclass of :class:`SearchIndex` that creates a :doc:`vector search index
30+
<atlas:atlas-vector-search/vector-search-type>` on the given field(s).
2331

2432
Available values for ``similarities`` are ``"euclidean"``, ``"cosine"``, and
25-
``"dotProduct"``.
33+
``"dotProduct"``. You can provide either a string value, in which case that
34+
value will be applied to all fields, or a list or tuple of values of the same
35+
length as list/tuple of fields with a similarity value for each field.
2636

27-
...
37+
<document restrictions on arrayfield, etc.>

tests/indexes_/test_checks.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
@skipIfDBFeature("supports_atlas_search")
1515
@isolate_apps("indexes_", attr_name="apps")
1616
@override_system_checks([check_indexes])
17-
class InvalidSearchIndexesTests(TestCase):
18-
def test_requires_atlas_search_support(self):
17+
class UnsupportedSearchIndexesTests(TestCase):
18+
def test_search_requires_atlas_search_support(self):
1919
class Article(models.Model):
2020
title = models.CharField(max_length=10)
2121

@@ -38,12 +38,7 @@ class Meta:
3838
],
3939
)
4040

41-
42-
@skipIfDBFeature("supports_atlas_search")
43-
@isolate_apps("indexes_", attr_name="apps")
44-
@override_system_checks([check_indexes])
45-
class UnsupportedSearchIndexesTests(TestCase):
46-
def test_requires_atlas_search_support(self):
41+
def test_vector_search_requires_atlas_search_support(self):
4742
class Article(models.Model):
4843
title = models.CharField(max_length=10)
4944

@@ -151,8 +146,8 @@ class Meta:
151146
"Field 'vector_data' is defined more than once. Vector and filter "
152147
"fields must use distinct field names.",
153148
id="django_mongodb_backend.indexes.VectorSearchIndex.E004",
154-
hint="If you need different configurations for the same field,"
155-
" create separate indexes.",
149+
hint="If you need different configurations for the same field, "
150+
"create separate indexes.",
156151
obj=Article._meta.indexes[0],
157152
),
158153
],

0 commit comments

Comments
 (0)