Skip to content

Commit

Permalink
Better support for elasticsearch/opensearch (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
barseghyanartur committed Jul 22, 2022
1 parent 2a110f1 commit e389192
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 18 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ are used for versioning (schema follows below):
0.3.4 to 0.4).
- All backwards incompatible changes are mentioned in this document.

0.1.3
-----
2022-07-22

- Better support of ``elasticsearch``/``opensearch``.

0.1.2
-----
2022-07-21
Expand Down
23 changes: 22 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,28 @@ For ``OpenSearch``:
Usage
=====
``elasticsearch``/``opensearch``
----------------------------------------
How-to
~~~~~~
In ``elasticsearch`` you would do:

.. code-block:: python
from elasticsearch import Elasticsearch
In ``opensearch-dsl`` you would do:

.. code-block:: python
from opensearch_py import OpenSearch
With ``anysearch`` you would change that to:

.. code-block:: python
from anysearch.search import AnySearch
``elasticsearch-dsl``/``opensearch-dsl``
----------------------------------------
How-to
Expand All @@ -106,7 +128,6 @@ With ``anysearch`` you would change that to:
from anysearch.search_dsl import AggsProxy, connections, Keyword
from anysearch.search_dsl.document import Document
``django-elasticsearch-dsl``/``django-opensearch-dsl``
------------------------------------------------------
How-to
Expand Down
73 changes: 59 additions & 14 deletions anysearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,11 @@
from typing import Set

__title__ = "anysearch"
__version__ = "0.1.2"
__version__ = "0.1.3"
__author__ = "Artur Barseghyan <artur.barseghyan@gmail.com>"
__copyright__ = "2022 Artur Barseghyan"
__license__ = "MIT"


__title__ = "anysearch"
__version__ = "0.1.1"
__author__ = "Artur Barseghyan <artur.barseghyan@gmail.com>"
__copyright__ = "2022 Artur Barseghyan"
__license__ = "MIT"


LOGGER = logging.getLogger(__name__)


Expand Down Expand Up @@ -255,7 +247,57 @@ class _SearchMovedItems(_LazyModule):


_search_moved_attributes = [
# TODO add more moved items
# elasticsearch/opensearch
# **********************************************
# **************** Moved modules ***************
# **********************************************
# .

# **********************************************
# ************* Moved attributes ***************
# **********************************************
# .
MovedAttribute("__version__", "elasticsearch", "opensearchpy"),
MovedAttribute(
"AnySearch",
"elasticsearch",
"opensearchpy",
"Elasticsearch",
"OpenSearch",
),
MovedAttribute("Transport", "elasticsearch", "opensearchpy"),
MovedAttribute("ConnectionPool", "elasticsearch", "opensearchpy"),
MovedAttribute("ConnectionSelector", "elasticsearch", "opensearchpy"),
MovedAttribute("RoundRobinSelector", "elasticsearch", "opensearchpy"),
MovedAttribute("JSONSerializer", "elasticsearch", "opensearchpy"),
MovedAttribute("Connection", "elasticsearch", "opensearchpy"),
MovedAttribute("RequestsHttpConnection", "elasticsearch", "opensearchpy"),
MovedAttribute("Urllib3HttpConnection", "elasticsearch", "opensearchpy"),
MovedAttribute("ImproperlyConfigured", "elasticsearch", "opensearchpy"),
MovedAttribute(
"AnySearchException",
"elasticsearch",
"opensearchpy",
"ElasticsearchException",
"OpenSearchException",
),
MovedAttribute("SerializationError", "elasticsearch", "opensearchpy"),
MovedAttribute("TransportError", "elasticsearch", "opensearchpy"),
MovedAttribute("NotFoundError", "elasticsearch", "opensearchpy"),
MovedAttribute("ConflictError", "elasticsearch", "opensearchpy"),
MovedAttribute("RequestError", "elasticsearch", "opensearchpy"),
MovedAttribute("ConnectionError", "elasticsearch", "opensearchpy"),
MovedAttribute("SSLError", "elasticsearch", "opensearchpy"),
MovedAttribute("ConnectionTimeout", "elasticsearch", "opensearchpy"),
MovedAttribute("AuthenticationException", "elasticsearch", "opensearchpy"),
MovedAttribute("AuthorizationException", "elasticsearch", "opensearchpy"),
MovedAttribute(
"AnySearchDeprecationWarning",
"elasticsearch",
"opensearchpy",
"ElasticsearchDeprecationWarning",
"OpenSearchDeprecationWarning",
),
]

for _search_attr in _search_moved_attributes:
Expand All @@ -268,7 +310,6 @@ class _SearchMovedItems(_LazyModule):
except:
pass


_SearchMovedItems._moved_attributes = _search_moved_attributes

search = _SearchMovedItems(__name__ + ".search")
Expand Down Expand Up @@ -345,9 +386,13 @@ class _SearchDSLMovedItems(_LazyModule):
MovedAttribute("InnerDoc", "elasticsearch_dsl", "opensearch_dsl"),
MovedAttribute("MetaField", "elasticsearch_dsl", "opensearch_dsl"),
# .exceptions
# MovedAttribute(
# "ElasticsearchDslException", "elasticsearch_dsl", "opensearch_dsl"
# ),
MovedAttribute(
"AnySearchDslException",
"elasticsearch_dsl",
"opensearch_dsl",
"ElasticsearchDslException",
"OpenSearchDslException",
),
MovedAttribute("IllegalOperation", "elasticsearch_dsl", "opensearch_dsl"),
MovedAttribute("UnknownDslObject", "elasticsearch_dsl", "opensearch_dsl"),
MovedAttribute(
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from setuptools import setup

version = "0.1.2"
version = "0.1.3"

try:
readme = open(os.path.join(os.path.dirname(__file__), "README.rst")).read()
Expand Down
91 changes: 89 additions & 2 deletions test_anysearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,53 @@ def MovedAttribute(*args):
"""Moved attribute."""
return args

SEARCH_MOVED_MODULES = [

]
SEARCH_MOVED_ATTRIBUTES = [
# .
# MovedAttribute("__version__", "elasticsearch", "opensearchpy"),
MovedAttribute(
"AnySearch",
"elasticsearch",
"opensearchpy",
"Elasticsearch",
"OpenSearch",
),
MovedAttribute("Transport", "elasticsearch", "opensearchpy"),
MovedAttribute("ConnectionPool", "elasticsearch", "opensearchpy"),
MovedAttribute("ConnectionSelector", "elasticsearch", "opensearchpy"),
MovedAttribute("RoundRobinSelector", "elasticsearch", "opensearchpy"),
MovedAttribute("JSONSerializer", "elasticsearch", "opensearchpy"),
MovedAttribute("Connection", "elasticsearch", "opensearchpy"),
MovedAttribute("RequestsHttpConnection", "elasticsearch", "opensearchpy"),
MovedAttribute("Urllib3HttpConnection", "elasticsearch", "opensearchpy"),
MovedAttribute("ImproperlyConfigured", "elasticsearch", "opensearchpy"),
MovedAttribute(
"AnySearchException",
"elasticsearch",
"opensearchpy",
"ElasticsearchException",
"OpenSearchException",
),
MovedAttribute("SerializationError", "elasticsearch", "opensearchpy"),
MovedAttribute("TransportError", "elasticsearch", "opensearchpy"),
MovedAttribute("NotFoundError", "elasticsearch", "opensearchpy"),
MovedAttribute("ConflictError", "elasticsearch", "opensearchpy"),
MovedAttribute("RequestError", "elasticsearch", "opensearchpy"),
MovedAttribute("ConnectionError", "elasticsearch", "opensearchpy"),
MovedAttribute("SSLError", "elasticsearch", "opensearchpy"),
MovedAttribute("ConnectionTimeout", "elasticsearch", "opensearchpy"),
MovedAttribute("AuthenticationException", "elasticsearch", "opensearchpy"),
MovedAttribute("AuthorizationException", "elasticsearch", "opensearchpy"),
# MovedAttribute(
# "AnySearchDeprecationWarning",
# "elasticsearch",
# "opensearchpy",
# "ElasticsearchDeprecationWarning",
# "OpenSearchDeprecationWarning",
# ),
]

SEARCH_DSL_MOVED_MODULES = [
MovedModule("aggs", "elasticsearch_dsl", "opensearch_dsl"),
Expand Down Expand Up @@ -215,12 +262,14 @@ def _check_expected_attribute_path(self, attr, package, name):
else:
LOGGER.exception(f"Pass: {attr.__name__} == {name}")

def _test_module_moved_attributes(self, module, name, package):
def _test_module_moved_attributes(self, module, name, package, orig_name=None):
with self.subTest(f"name: {name}, package: {package}"):
if not orig_name:
orig_name = name
module_path = f"anysearch.{module}"
_module = import_module(module_path)
attr = getattr(_module, name)
self._assert_expected_attribute_path(attr, package, name)
self._assert_expected_attribute_path(attr, package, orig_name)
# self._check_expected_attribute_path(attr, package, name)

def _test_module_moved_attributes_type_shortcuts(
Expand All @@ -244,6 +293,44 @@ def _test_module_moved_modules(self, module, name, package):
# self._check_expected_module_path(attr, package, name)


class SearchTestCase(AnySearchBaseTestCase):
"""Test search."""

def _test_moved_attributes(self, name, package, orig_name=None):
self._test_module_moved_attributes("search", name, package, orig_name)

# **************************************************
# ******************** opensearch ******************
# **************************************************
@mock.patch.dict("os.environ", {"ANYSEARCH_PREFERRED_BACKEND": OPENSEARCH})
@unittest.skipIf(
detect_search_backend() != OPENSEARCH,
"Skipped, because opensearch is not installed.",
)
def test_opensearch_moved_attributes(self):
"""Test OpenSearch."""
for name, _, package, *options in SEARCH_MOVED_ATTRIBUTES:
orig_name = options[1] if options else name
self._test_moved_attributes(name, package, orig_name)

# **************************************************
# ******************* elasticsearch ****************
# **************************************************

@mock.patch.dict(
"os.environ", {"ANYSEARCH_PREFERRED_BACKEND": ELASTICSEARCH}
)
@unittest.skipIf(
detect_search_backend() != ELASTICSEARCH,
"Skipped, because elasticsearch is not installed.",
)
def test_elasticsearch_moved_attributes(self):
"""Test Elasticsearch."""
for name, package, _, *options in SEARCH_MOVED_ATTRIBUTES:
orig_name = options[0] if options else name
self._test_moved_attributes(name, package, orig_name)


class SearchDSLTestCase(AnySearchBaseTestCase):
"""Test search DSL."""

Expand Down

0 comments on commit e389192

Please sign in to comment.