Skip to content

Commit b68c4bd

Browse files
minor refactoring, fixed linter and docs
1 parent 7e77c3b commit b68c4bd

File tree

6 files changed

+44
-98
lines changed

6 files changed

+44
-98
lines changed

elasticsearch_dsl/_async/__init__.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Licensed to Elasticsearch B.V. under one or more contributor
2+
# license agreements. See the NOTICE file distributed with
3+
# this work for additional information regarding copyright
4+
# ownership. Elasticsearch B.V. licenses this file to you under
5+
# the Apache License, Version 2.0 (the "License"); you may
6+
# not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.

elasticsearch_dsl/_async/search.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@
1919
from elasticsearch.helpers import async_scan
2020

2121
from ..async_connections import get_connection
22-
from ..base_search import BaseMultiSearch, BaseSearch
2322
from ..response import Response
23+
from ..search_base import MultiSearchBase, SearchBase
2424
from ..utils import AttrDict
2525

2626

27-
class Search(BaseSearch):
27+
class Search(SearchBase):
2828
def __aiter__(self):
2929
"""
3030
Iterate over the hits.
@@ -100,7 +100,7 @@ async def delete(self):
100100
)
101101

102102

103-
class MultiSearch(BaseMultiSearch):
103+
class MultiSearch(MultiSearchBase):
104104
"""
105105
Combine multiple :class:`~elasticsearch_dsl.Search` objects into a single
106106
request.

elasticsearch_dsl/_sync/__init__.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Licensed to Elasticsearch B.V. under one or more contributor
2+
# license agreements. See the NOTICE file distributed with
3+
# this work for additional information regarding copyright
4+
# ownership. Elasticsearch B.V. licenses this file to you under
5+
# the Apache License, Version 2.0 (the "License"); you may
6+
# not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.

elasticsearch_dsl/_sync/search.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@
1818
from elasticsearch.exceptions import ApiError
1919
from elasticsearch.helpers import scan
2020

21-
from ..base_search import BaseMultiSearch, BaseSearch
2221
from ..connections import get_connection
2322
from ..response import Response
23+
from ..search_base import MultiSearchBase, SearchBase
2424
from ..utils import AttrDict
2525

2626

27-
class Search(BaseSearch):
27+
class Search(SearchBase):
2828
def __iter__(self):
2929
"""
3030
Iterate over the hits.
@@ -92,7 +92,7 @@ def delete(self):
9292
)
9393

9494

95-
class MultiSearch(BaseMultiSearch):
95+
class MultiSearch(MultiSearchBase):
9696
"""
9797
Combine multiple :class:`~elasticsearch_dsl.Search` objects into a single
9898
request.

elasticsearch_dsl/search.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@
1515
# specific language governing permissions and limitations
1616
# under the License.
1717

18-
from .base_search import *
19-
from elasticsearch_dsl._sync.search import *
18+
from elasticsearch_dsl._sync.search import * # noqa: F401, F403
19+
20+
from .search_base import * # noqa: F401, F403

elasticsearch_dsl/base_search.py renamed to elasticsearch_dsl/search_base.py

Lines changed: 3 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,11 @@
1818
import collections.abc
1919
import copy
2020

21-
from elasticsearch.exceptions import ApiError
22-
from elasticsearch.helpers import scan
23-
2421
from .aggs import A, AggBase
25-
from .connections import get_connection
2622
from .exceptions import IllegalOperation
2723
from .query import Bool, Q, Query
2824
from .response import Hit, Response
29-
from .utils import AttrDict, DslBase, recursive_to_dict
25+
from .utils import DslBase, recursive_to_dict
3026

3127

3228
class QueryProxy:
@@ -299,7 +295,7 @@ def _clone(self):
299295
return s
300296

301297

302-
class BaseSearch(Request):
298+
class SearchBase(Request):
303299
query = ProxyDescriptor("query")
304300
post_filter = ProxyDescriptor("post_filter")
305301

@@ -807,66 +803,8 @@ def to_dict(self, count=False, **kwargs):
807803
d.update(recursive_to_dict(kwargs))
808804
return d
809805

810-
def count(self):
811-
"""
812-
Return the number of hits matching the query and filters. Note that
813-
only the actual number is returned.
814-
"""
815-
if hasattr(self, "_response") and self._response.hits.total.relation == "eq":
816-
return self._response.hits.total.value
817-
818-
es = get_connection(self._using)
819-
820-
d = self.to_dict(count=True)
821-
# TODO: failed shards detection
822-
resp = es.count(index=self._index, query=d.get("query", None), **self._params)
823-
return resp["count"]
824-
825-
def execute(self, ignore_cache=False):
826-
"""
827-
Execute the search and return an instance of ``Response`` wrapping all
828-
the data.
829-
830-
:arg ignore_cache: if set to ``True``, consecutive calls will hit
831-
ES, while cached result will be ignored. Defaults to `False`
832-
"""
833-
if ignore_cache or not hasattr(self, "_response"):
834-
es = get_connection(self._using)
835-
836-
self._response = self._response_class(
837-
self,
838-
es.search(index=self._index, body=self.to_dict(), **self._params).body,
839-
)
840-
return self._response
841-
842-
def scan(self):
843-
"""
844-
Turn the search into a scan search and return a generator that will
845-
iterate over all the documents matching the query.
846-
847-
Use ``params`` method to specify any additional arguments you with to
848-
pass to the underlying ``scan`` helper from ``elasticsearch-py`` -
849-
https://elasticsearch-py.readthedocs.io/en/master/helpers.html#elasticsearch.helpers.scan
850-
851-
"""
852-
es = get_connection(self._using)
853-
854-
for hit in scan(es, query=self.to_dict(), index=self._index, **self._params):
855-
yield self._get_result(hit)
856-
857-
def delete(self):
858-
"""
859-
delete() executes the query by delegating to delete_by_query()
860-
"""
861-
862-
es = get_connection(self._using)
863-
864-
return AttrDict(
865-
es.delete_by_query(index=self._index, body=self.to_dict(), **self._params)
866-
)
867-
868806

869-
class BaseMultiSearch(Request):
807+
class MultiSearchBase(Request):
870808
"""
871809
Combine multiple :class:`~elasticsearch_dsl.Search` objects into a single
872810
request.
@@ -911,28 +849,3 @@ def to_dict(self):
911849
out.append(s.to_dict())
912850

913851
return out
914-
915-
def execute(self, ignore_cache=False, raise_on_error=True):
916-
"""
917-
Execute the multi search request and return a list of search results.
918-
"""
919-
if ignore_cache or not hasattr(self, "_response"):
920-
es = get_connection(self._using)
921-
922-
responses = es.msearch(
923-
index=self._index, body=self.to_dict(), **self._params
924-
)
925-
926-
out = []
927-
for s, r in zip(self._searches, responses["responses"]):
928-
if r.get("error", False):
929-
if raise_on_error:
930-
raise ApiError("N/A", meta=responses.meta, body=r)
931-
r = None
932-
else:
933-
r = Response(s, r)
934-
out.append(r)
935-
936-
self._response = out
937-
938-
return self._response

0 commit comments

Comments
 (0)