Skip to content

Commit 0859cf7

Browse files
committed
edits
1 parent 74bb495 commit 0859cf7

File tree

3 files changed

+74
-39
lines changed

3 files changed

+74
-39
lines changed

django_mongodb_backend/indexes.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,9 @@ def __init__(
118118
"Atlas Search field mappings."
119119
)
120120
if analyzer is not None and not isinstance(analyzer, str):
121-
raise ValueError(f"analyzer must be a string. got type: {type(analyzer)}")
121+
raise ValueError(f"analyzer must be a string; got: {type(analyzer)}.")
122122
if search_analyzer is not None and not isinstance(search_analyzer, str):
123-
raise ValueError(f"search_analyzer must be a string. got type: {type(search_analyzer)}")
123+
raise ValueError(f"search_analyzer must be a string; got: {type(search_analyzer)}.")
124124
self.field_mappings = field_mappings
125125
self.analyzer = analyzer
126126
self.search_analyzer = search_analyzer
@@ -134,9 +134,10 @@ def deconstruct(self):
134134
path, args, kwargs = super().deconstruct()
135135
if self.field_mappings is not None:
136136
kwargs["field_mappings"] = self.field_mappings
137-
if self.analyzer is not None:
137+
del kwargs["fields"]
138+
if self.analyzer:
138139
kwargs["analyzer"] = self.analyzer
139-
if self.search_analyzer is not None:
140+
if self.search_analyzer:
140141
kwargs["search_analyzer"] = self.search_analyzer
141142
return path, args, kwargs
142143

@@ -187,13 +188,13 @@ def get_pymongo_index_model(
187188
field = model._meta.get_field(field_name)
188189
type_ = self.search_index_data_types(field.db_type(schema_editor.connection))
189190
fields[field_path] = {"type": type_}
190-
analyzers = {}
191-
if self.analyzer is not None:
192-
analyzers["analyzer"] = self.analyzer
193-
if self.search_analyzer is not None:
194-
analyzers["searchAnalyzer"] = self.search_analyzer
191+
extra = {}
192+
if self.analyzer:
193+
extra["analyzer"] = self.analyzer
194+
if self.search_analyzer:
195+
extra["searchAnalyzer"] = self.search_analyzer
195196
return SearchIndexModel(
196-
definition={"mappings": {"dynamic": False, "fields": fields}, **analyzers},
197+
definition={"mappings": {"dynamic": False, "fields": fields}, **extra},
197198
name=self.name,
198199
)
199200

docs/ref/models/indexes.rst

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,26 +37,26 @@ minutes, depending on the size of the collection.
3737

3838
Use ``field_mappings`` (instead of ``fields``) to create an advanced search
3939
index. ``field_mappings`` is a dictionary that maps field names to index
40-
options (see ``definition["mappings"]["fields"]`` in the
41-
:ref:`atlas:fts-static-mapping-example`).
40+
options. See ``definition["mappings"]["fields"]`` in the
41+
:ref:`atlas:fts-static-mapping-example`.
4242

4343
If ``name`` isn't provided, one will be generated automatically. If you
4444
need to reference the name in your search query and don't provide your own
4545
name, you can lookup the generated one using ``Model._meta.indexes[0].name``
4646
(substituting the name of your model as well as a different list index if
4747
your model has multiple indexes).
4848

49-
Use ``analyzer`` or ``search_analyzer`` to configure the
50-
indexing and searching analyzer, respectively, for
51-
the search index definition. If these fields are not provided,
52-
they will default to ``lucene.standard`` at the server level.
53-
(See ``definition["mappings"]["analyzer"]``
54-
and ``definition["mappings"]["searchAnalyzer"]``
55-
in the :ref:`atlas:fts-static-mapping-example`).
49+
Use ``analyzer`` and ``search_analyzer`` to configure the indexing and
50+
searching analyzer, respectively, for the search index definition. If
51+
these options aren't provided, the server defaults to ``lucene.standard``.
52+
See ``definition["mappings"]["analyzer"]`` and
53+
``definition["mappings"]["searchAnalyzer"]`` in the
54+
:ref:`atlas:fts-static-mapping-example`).
5655

5756
.. versionchanged:: 5.2.2
5857

59-
The ``fields_mappings``, ``analyzer``, and ``search_analyzer`` arguments were added.
58+
The ``fields_mappings``, ``analyzer``, and ``search_analyzer``
59+
arguments were added.
6060

6161
``VectorSearchIndex``
6262
---------------------

tests/indexes_/test_search_indexes.py

Lines changed: 53 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,45 @@ def test_field_mappings_type(self):
6767
SearchIndex(field_mappings={"foo"})
6868

6969
def test_analyzer_type(self):
70-
msg = "analyzer must be a string. got type: <class 'int'>"
70+
msg = "analyzer must be a string; got: <class 'int'>."
7171
with self.assertRaisesMessage(ValueError, msg):
7272
SearchIndex(analyzer=42)
7373

7474
def test_search_analyzer_type(self):
75-
msg = "search_analyzer must be a string. got type: <class 'list'>"
75+
msg = "search_analyzer must be a string; got: <class 'list'>."
7676
with self.assertRaisesMessage(ValueError, msg):
7777
SearchIndex(search_analyzer=["foo"])
7878

79+
def test_deconstruct(self):
80+
index = SearchIndex(name="recent_test_idx", fields=["number"])
81+
name, args, kwargs = index.deconstruct()
82+
self.assertEqual(name, "django_mongodb_backend.indexes.SearchIndex")
83+
self.assertEqual(args, ())
84+
self.assertEqual(kwargs, {"name": "recent_test_idx", "fields": ["number"]})
85+
86+
def test_deconstruct_field_mappings(self):
87+
field_mappings = {"headline": {"type": "token"}}
88+
index = SearchIndex(field_mappings=field_mappings)
89+
_, args, kwargs = index.deconstruct()
90+
self.assertEqual(args, ())
91+
self.assertEqual(kwargs, {"name": "", "field_mappings": field_mappings})
92+
93+
def test_deconstruct_analyzer(self):
94+
index = SearchIndex(
95+
fields=["a"], analyzer="lucene.simple", search_analyzer="lucene.english"
96+
)
97+
_, args, kwargs = index.deconstruct()
98+
self.assertEqual(args, ())
99+
self.assertEqual(
100+
kwargs,
101+
{
102+
"name": "",
103+
"fields": ["a"],
104+
"analyzer": "lucene.simple",
105+
"search_analyzer": "lucene.english",
106+
},
107+
)
108+
79109

80110
class VectorSearchIndexTests(SimpleTestCase):
81111
def test_no_init_args(self):
@@ -209,7 +239,7 @@ def test_valid_fields(self):
209239

210240
def test_field_mappings(self):
211241
index = SearchIndex(
212-
name="recent_test_idx",
242+
name="field_mappings_test_idx",
213243
field_mappings={
214244
"char": {
215245
"indexOptions": "offsets",
@@ -225,27 +255,31 @@ def test_field_mappings(self):
225255
index_info = connection.introspection.get_constraints(
226256
cursor=None,
227257
table_name=SearchIndexTestModel._meta.db_table,
228-
)
258+
)[index.name]
229259
expected_options = {
230-
"dynamic": False,
231-
"fields": {
232-
"char": {
233-
"indexOptions": "offsets",
234-
"norms": "include",
235-
"store": True,
236-
"type": "string",
237-
}
260+
"analyzer": None,
261+
"searchAnalyzer": None,
262+
"mappings": {
263+
"dynamic": False,
264+
"fields": {
265+
"char": {
266+
"indexOptions": "offsets",
267+
"norms": "include",
268+
"store": True,
269+
"type": "string",
270+
}
271+
},
238272
},
239273
}
240-
self.assertCountEqual(index_info[index.name]["columns"], index.fields)
241-
self.assertEqual(index_info[index.name]["options"]["mappings"], expected_options)
274+
self.assertCountEqual(index_info["columns"], index.fields)
275+
self.assertEqual(index_info["options"], expected_options)
242276
finally:
243277
with connection.schema_editor() as editor:
244278
editor.remove_index(index=index, model=SearchIndexTestModel)
245279

246-
def test_analyzer_inclusion(self):
280+
def test_analyzer(self):
247281
index = SearchIndex(
248-
name="recent_test_idx",
282+
name="analyzer_test_idx",
249283
fields=["char"],
250284
analyzer="lucene.simple",
251285
search_analyzer="lucene.simple",
@@ -256,7 +290,7 @@ def test_analyzer_inclusion(self):
256290
index_info = connection.introspection.get_constraints(
257291
cursor=None,
258292
table_name=SearchIndexTestModel._meta.db_table,
259-
)
293+
)[index.name]
260294
expected_options = {
261295
"analyzer": "lucene.simple",
262296
"searchAnalyzer": "lucene.simple",
@@ -272,8 +306,8 @@ def test_analyzer_inclusion(self):
272306
},
273307
},
274308
}
275-
self.assertCountEqual(index_info[index.name]["columns"], index.fields)
276-
self.assertEqual(index_info[index.name]["options"], expected_options)
309+
self.assertCountEqual(index_info["columns"], index.fields)
310+
self.assertEqual(index_info["options"], expected_options)
277311
finally:
278312
with connection.schema_editor() as editor:
279313
editor.remove_index(index=index, model=SearchIndexTestModel)

0 commit comments

Comments
 (0)