Skip to content

Commit d06d804

Browse files
more review feedback
1 parent 2468e8a commit d06d804

File tree

15 files changed

+179
-175
lines changed

15 files changed

+179
-175
lines changed

elasticsearch_dsl/_async/index.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,20 @@ async def save(self, using=None):
5656

5757

5858
class AsyncIndex(IndexBase):
59+
def __init__(self, name, using="default"):
60+
"""
61+
:arg name: name of the index
62+
:arg using: connection alias to use, defaults to ``'default'``
63+
"""
64+
super().__init__(name, AsyncMapping, using=using)
65+
5966
def _get_connection(self, using=None):
6067
if self._name is None:
6168
raise ValueError("You cannot perform API calls on the default index.")
6269
return get_connection(using or self._using)
6370

6471
connection = property(_get_connection)
6572

66-
def get_or_create_mapping(self):
67-
if self._mapping is None:
68-
self._mapping = AsyncMapping()
69-
return self._mapping
70-
7173
def as_template(self, template_name, pattern=None, order=None):
7274
# TODO: should we allow pattern to be a top-level arg?
7375
# or maybe have an IndexPattern that allows for it and have

elasticsearch_dsl/_sync/index.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,20 @@ def save(self, using=None):
5454

5555

5656
class Index(IndexBase):
57+
def __init__(self, name, using="default"):
58+
"""
59+
:arg name: name of the index
60+
:arg using: connection alias to use, defaults to ``'default'``
61+
"""
62+
super().__init__(name, Mapping, using=using)
63+
5764
def _get_connection(self, using=None):
5865
if self._name is None:
5966
raise ValueError("You cannot perform API calls on the default index.")
6067
return get_connection(using or self._using)
6168

6269
connection = property(_get_connection)
6370

64-
def get_or_create_mapping(self):
65-
if self._mapping is None:
66-
self._mapping = Mapping()
67-
return self._mapping
68-
6971
def as_template(self, template_name, pattern=None, order=None):
7072
# TODO: should we allow pattern to be a top-level arg?
7173
# or maybe have an IndexPattern that allows for it and have

elasticsearch_dsl/index_base.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121

2222
class IndexBase:
23-
def __init__(self, name, using="default"):
23+
def __init__(self, name, mapping_class, using="default"):
2424
"""
2525
:arg name: name of the index
2626
:arg using: connection alias to use, defaults to ``'default'``
@@ -31,6 +31,7 @@ def __init__(self, name, using="default"):
3131
self._settings = {}
3232
self._aliases = {}
3333
self._analysis = {}
34+
self._mapping_class = mapping_class
3435
self._mapping = None
3536

3637
def resolve_nested(self, field_path):
@@ -51,6 +52,11 @@ def resolve_field(self, field_path):
5152
return self._mapping.resolve_field(field_path)
5253
return None
5354

55+
def get_or_create_mapping(self):
56+
if self._mapping is None:
57+
self._mapping = self._mapping_class()
58+
return self._mapping
59+
5460
def mapping(self, mapping):
5561
"""
5662
Associate a mapping (an instance of

tests/_async/test_document.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,7 @@ async def test_update_no_fields():
588588
await md.update()
589589

590590

591-
async def test_search_with_custom_alias_and_index(async_mock_client):
591+
def test_search_with_custom_alias_and_index():
592592
search_object = MyDoc.search(
593593
using="staging", index=["custom_index1", "custom_index2"]
594594
)

tests/_async/test_mapping.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,11 @@
1717

1818
import json
1919

20-
from elasticsearch_dsl import Keyword, Nested, Text, analysis
21-
from elasticsearch_dsl._async import mapping
20+
from elasticsearch_dsl import AsyncMapping, Keyword, Nested, Text, analysis
2221

2322

2423
def test_mapping_can_has_fields():
25-
m = mapping.AsyncMapping()
24+
m = AsyncMapping()
2625
m.field("name", "text").field("tags", "keyword")
2726

2827
assert {
@@ -31,14 +30,14 @@ def test_mapping_can_has_fields():
3130

3231

3332
def test_mapping_update_is_recursive():
34-
m1 = mapping.AsyncMapping()
33+
m1 = AsyncMapping()
3534
m1.field("title", "text")
3635
m1.field("author", "object")
3736
m1.field("author", "object", properties={"name": {"type": "text"}})
3837
m1.meta("_all", enabled=False)
3938
m1.meta("dynamic", False)
4039

41-
m2 = mapping.AsyncMapping()
40+
m2 = AsyncMapping()
4241
m2.field("published_from", "date")
4342
m2.field("author", "object", properties={"email": {"type": "text"}})
4443
m2.field("title", "text")
@@ -64,7 +63,7 @@ def test_mapping_update_is_recursive():
6463

6564

6665
def test_properties_can_iterate_over_all_the_fields():
67-
m = mapping.AsyncMapping()
66+
m = AsyncMapping()
6867
m.field("f1", "text", test_attr="f1", fields={"f2": Keyword(test_attr="f2")})
6968
m.field("f3", Nested(test_attr="f3", properties={"f4": Text(test_attr="f4")}))
7069

@@ -101,7 +100,7 @@ def test_mapping_can_collect_all_analyzers_and_normalizers():
101100
)
102101
n3 = analysis.normalizer("unknown_custom")
103102

104-
m = mapping.AsyncMapping()
103+
m = AsyncMapping()
105104
m.field(
106105
"title",
107106
"text",
@@ -160,7 +159,7 @@ def test_mapping_can_collect_multiple_analyzers():
160159
tokenizer=analysis.tokenizer("trigram", "nGram", min_gram=3, max_gram=3),
161160
filter=[analysis.token_filter("my_filter2", "stop", stopwords=["c", "d"])],
162161
)
163-
m = mapping.AsyncMapping()
162+
m = AsyncMapping()
164163
m.field("title", "text", analyzer=a1, search_analyzer=a2)
165164
m.field(
166165
"text",
@@ -194,7 +193,7 @@ def test_mapping_can_collect_multiple_analyzers():
194193

195194
def test_even_non_custom_analyzers_can_have_params():
196195
a1 = analysis.analyzer("whitespace", type="pattern", pattern=r"\\s+")
197-
m = mapping.AsyncMapping()
196+
m = AsyncMapping()
198197
m.field("title", "text", analyzer=a1)
199198

200199
assert {
@@ -203,14 +202,14 @@ def test_even_non_custom_analyzers_can_have_params():
203202

204203

205204
def test_resolve_field_can_resolve_multifields():
206-
m = mapping.AsyncMapping()
205+
m = AsyncMapping()
207206
m.field("title", "text", fields={"keyword": Keyword()})
208207

209208
assert isinstance(m.resolve_field("title.keyword"), Keyword)
210209

211210

212211
def test_resolve_nested():
213-
m = mapping.AsyncMapping()
212+
m = AsyncMapping()
214213
m.field("n1", "nested", properties={"n2": Nested(properties={"k1": Keyword()})})
215214
m.field("k2", "keyword")
216215

0 commit comments

Comments
 (0)