Skip to content

Commit 85fb761

Browse files
committed
Refactores usage of connection singeton to exposed functions
1 parent 85dafe6 commit 85fb761

File tree

7 files changed

+34
-34
lines changed

7 files changed

+34
-34
lines changed

elasticsearch_dsl/analysis.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import six
22

3-
from .connections import connections
4-
from .utils import DslBase, AttrDict
3+
from .connections import get_connection
4+
from .utils import AttrDict, DslBase
55

66
__all__ = [
77
'tokenizer', 'analyzer', 'char_filter', 'token_filter', 'normalizer'
@@ -94,7 +94,7 @@ def simulate(self, text, using='default', explain=False, attributes=None):
9494
:arg attributes: if ``explain`` is specified, filter the token
9595
attributes to return.
9696
"""
97-
es = connections.get_connection(using)
97+
es = get_connection(using)
9898

9999
body = {'text': text, 'explain': explain}
100100
if attributes:

elasticsearch_dsl/document.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66
from fnmatch import fnmatch
77

88
from elasticsearch.exceptions import NotFoundError, RequestError
9-
from six import iteritems, add_metaclass
9+
from six import add_metaclass, iteritems
1010

11+
from .connections import get_connection
12+
from .exceptions import IllegalOperation, ValidationException
1113
from .field import Field
14+
from .index import Index
1215
from .mapping import Mapping
13-
from .utils import ObjectBase, merge, DOC_META_FIELDS, META_FIELDS
1416
from .search import Search
15-
from .connections import connections
16-
from .exceptions import ValidationException, IllegalOperation
17-
from .index import Index
17+
from .utils import DOC_META_FIELDS, META_FIELDS, ObjectBase, merge
1818

1919

2020
class MetaField(object):
@@ -121,7 +121,7 @@ def _get_using(cls, using=None):
121121

122122
@classmethod
123123
def _get_connection(cls, using=None):
124-
return connections.get_connection(cls._get_using(using))
124+
return get_connection(cls._get_using(using))
125125

126126
@classmethod
127127
def _default_index(cls, index=None):

elasticsearch_dsl/index.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
from .connections import connections
2-
from .search import Search
3-
from .update_by_query import UpdateByQuery
1+
from . import analysis
2+
from .connections import get_connection
43
from .exceptions import IllegalOperation
54
from .mapping import Mapping
5+
from .search import Search
6+
from .update_by_query import UpdateByQuery
67
from .utils import merge
7-
from . import analysis
88

99
DEFAULT_DOC_TYPE = 'doc'
1010

@@ -32,7 +32,7 @@ def to_dict(self):
3232
return d
3333

3434
def save(self, using=None):
35-
es = connections.get_connection(using or self._index._using)
35+
es = get_connection(using or self._index._using)
3636
es.indices.put_template(name=self._template_name, body=self.to_dict())
3737

3838
class Index(object):
@@ -110,7 +110,7 @@ def _get_connection(self, using=None):
110110
if self._name is None:
111111
raise ValueError(
112112
"You cannot perform API calls on the default index.")
113-
return connections.get_connection(using or self._using)
113+
return get_connection(using or self._using)
114114
connection = property(_get_connection)
115115

116116
def mapping(self, mapping):

elasticsearch_dsl/mapping.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
except ImportError:
44
import collections as collections_abc
55

6-
from six import iteritems, itervalues
76
from itertools import chain
87

8+
from six import iteritems, itervalues
9+
10+
from .connections import get_connection
11+
from .field import Nested, Text, construct_field
912
from .utils import DslBase
10-
from .field import Text, construct_field, Nested
11-
from .connections import connections
1213

1314
META_FIELDS = frozenset((
1415
'dynamic', 'transform', 'dynamic_date_formats', 'date_detection',
@@ -135,7 +136,7 @@ def save(self, index, using='default'):
135136
return index.save()
136137

137138
def update_from_es(self, index, using='default'):
138-
es = connections.get_connection(using)
139+
es = get_connection(using)
139140
raw = es.indices.get_mapping(index=index)
140141
_, raw = raw.popitem()
141142
self._update_from_dict(raw['mappings'])

elasticsearch_dsl/search.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from .aggs import A, AggBase
1515
from .utils import DslBase, AttrDict
1616
from .response import Response, Hit
17-
from .connections import connections
17+
from .connections import get_connection
1818
from .exceptions import IllegalOperation
1919

2020

@@ -669,7 +669,7 @@ def count(self):
669669
if hasattr(self, '_response'):
670670
return self._response.hits.total
671671

672-
es = connections.get_connection(self._using)
672+
es = get_connection(self._using)
673673

674674
d = self.to_dict(count=True)
675675
# TODO: failed shards detection
@@ -688,7 +688,7 @@ def execute(self, ignore_cache=False):
688688
ES, while cached result will be ignored. Defaults to `False`
689689
"""
690690
if ignore_cache or not hasattr(self, '_response'):
691-
es = connections.get_connection(self._using)
691+
es = get_connection(self._using)
692692

693693
self._response = self._response_class(
694694
self,
@@ -710,7 +710,7 @@ def scan(self):
710710
https://elasticsearch-py.readthedocs.io/en/master/helpers.html#elasticsearch.helpers.scan
711711
712712
"""
713-
es = connections.get_connection(self._using)
713+
es = get_connection(self._using)
714714

715715
for hit in scan(
716716
es,
@@ -725,7 +725,7 @@ def delete(self):
725725
delete() executes the query by delegating to delete_by_query()
726726
"""
727727

728-
es = connections.get_connection(self._using)
728+
es = get_connection(self._using)
729729

730730
return AttrDict(
731731
es.delete_by_query(
@@ -786,7 +786,7 @@ def execute(self, ignore_cache=False, raise_on_error=True):
786786
Execute the multi search request and return a list of search results.
787787
"""
788788
if ignore_cache or not hasattr(self, '_response'):
789-
es = connections.get_connection(self._using)
789+
es = get_connection(self._using)
790790

791791
responses = es.msearch(
792792
index=self._index,

elasticsearch_dsl/update_by_query.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import copy
2-
3-
from .search import Request, QueryProxy, ProxyDescriptor
4-
from .query import Q, Bool
1+
from .connections import get_connection
2+
from .query import Bool, Q
53
from .response import UpdateByQueryResponse
6-
from .connections import connections
4+
from .search import ProxyDescriptor, QueryProxy, Request
5+
76

87
class UpdateByQuery(Request):
98

@@ -134,7 +133,7 @@ def execute(self):
134133
Execute the search and return an instance of ``Response`` wrapping all
135134
the data.
136135
"""
137-
es = connections.get_connection(self._using)
136+
es = get_connection(self._using)
138137

139138
self._response = self._response_class(
140139
self,

test_elasticsearch_dsl/conftest.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from mock import Mock
99
from pytest import fixture, skip
1010

11-
from elasticsearch_dsl.connections import connections
11+
from elasticsearch_dsl.connections import connections, add_connection
1212
from .test_integration.test_data import DATA, FLAT_DATA, TEST_GIT_DATA, \
1313
create_git_index, create_flat_git_index
1414
from .test_integration.test_document import PullRequest, Comment, User, History
@@ -18,7 +18,7 @@
1818
def client():
1919
try:
2020
connection = get_test_client(nowait='WAIT_FOR_ES' not in os.environ)
21-
connections.add_connection('default', connection)
21+
add_connection('default', connection)
2222
return connection
2323
except SkipTest:
2424
skip()
@@ -33,7 +33,7 @@ def write_client(client):
3333
def mock_client(dummy_response):
3434
client = Mock()
3535
client.search.return_value = dummy_response
36-
connections.add_connection('mock', client)
36+
add_connection('mock', client)
3737
yield client
3838
connections._conn = {}
3939
connections._kwargs = {}

0 commit comments

Comments
 (0)