Skip to content

async support #1714

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 27 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
05ebfa5
async support (work in progress)
miguelgrinberg Mar 8, 2024
74eccd7
minor refactoring, fixed linter and docs
miguelgrinberg Mar 11, 2024
8fcf16c
validate _sync directory during lint phase
miguelgrinberg Mar 11, 2024
78a4f72
search unit tests
miguelgrinberg Mar 13, 2024
5f9eb27
async support for Document class
miguelgrinberg Mar 13, 2024
db05fc2
search integration tests
miguelgrinberg Mar 13, 2024
85a1f41
document tests
miguelgrinberg Mar 14, 2024
a0fb061
async support for Index class
miguelgrinberg Mar 14, 2024
a3fa07d
document integration tests
miguelgrinberg Mar 14, 2024
326f965
remove unused mock dependency
miguelgrinberg Mar 19, 2024
90c8eb5
unasync support for update_by_query
miguelgrinberg Mar 19, 2024
071ddea
remove star imports
miguelgrinberg Mar 19, 2024
eca6306
unasync support for mapping.py
miguelgrinberg Mar 19, 2024
73ffe5a
unasync index integration tests
miguelgrinberg Mar 19, 2024
3690105
unasync faceted search
miguelgrinberg Mar 20, 2024
0184a77
review imports for consistency
miguelgrinberg Mar 20, 2024
8add69f
async support for analyzer
miguelgrinberg Mar 20, 2024
4b7e9fb
async examples
miguelgrinberg Mar 21, 2024
38cd862
examples integration tests
miguelgrinberg Mar 22, 2024
7386dd5
restore sync examples unasynced by mistake
miguelgrinberg Mar 22, 2024
8b5fa41
Documentation updates
miguelgrinberg Mar 22, 2024
2468e8a
Review feedback
miguelgrinberg Mar 27, 2024
d06d804
more review feedback
miguelgrinberg Mar 28, 2024
7bdf7dc
another batch of review updates
miguelgrinberg Mar 29, 2024
9c8e63d
documentation reorg
miguelgrinberg Apr 2, 2024
c172281
more documentation improvements
miguelgrinberg Apr 2, 2024
6b56fb3
unasyncing of examples
miguelgrinberg Apr 2, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions elasticsearch_dsl/_async/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,20 @@ async def save(self, using=None):


class AsyncIndex(IndexBase):
def __init__(self, name, using="default"):
"""
:arg name: name of the index
:arg using: connection alias to use, defaults to ``'default'``
"""
super().__init__(name, AsyncMapping, using=using)

def _get_connection(self, using=None):
if self._name is None:
raise ValueError("You cannot perform API calls on the default index.")
return get_connection(using or self._using)

connection = property(_get_connection)

def get_or_create_mapping(self):
if self._mapping is None:
self._mapping = AsyncMapping()
return self._mapping

def as_template(self, template_name, pattern=None, order=None):
# TODO: should we allow pattern to be a top-level arg?
# or maybe have an IndexPattern that allows for it and have
Expand Down
12 changes: 7 additions & 5 deletions elasticsearch_dsl/_sync/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,20 @@ def save(self, using=None):


class Index(IndexBase):
def __init__(self, name, using="default"):
"""
:arg name: name of the index
:arg using: connection alias to use, defaults to ``'default'``
"""
super().__init__(name, Mapping, using=using)

def _get_connection(self, using=None):
if self._name is None:
raise ValueError("You cannot perform API calls on the default index.")
return get_connection(using or self._using)

connection = property(_get_connection)

def get_or_create_mapping(self):
if self._mapping is None:
self._mapping = Mapping()
return self._mapping

def as_template(self, template_name, pattern=None, order=None):
# TODO: should we allow pattern to be a top-level arg?
# or maybe have an IndexPattern that allows for it and have
Expand Down
8 changes: 7 additions & 1 deletion elasticsearch_dsl/index_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@


class IndexBase:
def __init__(self, name, using="default"):
def __init__(self, name, mapping_class, using="default"):
"""
:arg name: name of the index
:arg using: connection alias to use, defaults to ``'default'``
Expand All @@ -31,6 +31,7 @@ def __init__(self, name, using="default"):
self._settings = {}
self._aliases = {}
self._analysis = {}
self._mapping_class = mapping_class
self._mapping = None

def resolve_nested(self, field_path):
Expand All @@ -51,6 +52,11 @@ def resolve_field(self, field_path):
return self._mapping.resolve_field(field_path)
return None

def get_or_create_mapping(self):
if self._mapping is None:
self._mapping = self._mapping_class()
return self._mapping

def mapping(self, mapping):
"""
Associate a mapping (an instance of
Expand Down
2 changes: 1 addition & 1 deletion tests/_async/test_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ async def test_update_no_fields():
await md.update()


async def test_search_with_custom_alias_and_index(async_mock_client):
def test_search_with_custom_alias_and_index():
search_object = MyDoc.search(
using="staging", index=["custom_index1", "custom_index2"]
)
Expand Down
21 changes: 10 additions & 11 deletions tests/_async/test_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@

import json

from elasticsearch_dsl import Keyword, Nested, Text, analysis
from elasticsearch_dsl._async import mapping
from elasticsearch_dsl import AsyncMapping, Keyword, Nested, Text, analysis


def test_mapping_can_has_fields():
m = mapping.AsyncMapping()
m = AsyncMapping()
m.field("name", "text").field("tags", "keyword")

assert {
Expand All @@ -31,14 +30,14 @@ def test_mapping_can_has_fields():


def test_mapping_update_is_recursive():
m1 = mapping.AsyncMapping()
m1 = AsyncMapping()
m1.field("title", "text")
m1.field("author", "object")
m1.field("author", "object", properties={"name": {"type": "text"}})
m1.meta("_all", enabled=False)
m1.meta("dynamic", False)

m2 = mapping.AsyncMapping()
m2 = AsyncMapping()
m2.field("published_from", "date")
m2.field("author", "object", properties={"email": {"type": "text"}})
m2.field("title", "text")
Expand All @@ -64,7 +63,7 @@ def test_mapping_update_is_recursive():


def test_properties_can_iterate_over_all_the_fields():
m = mapping.AsyncMapping()
m = AsyncMapping()
m.field("f1", "text", test_attr="f1", fields={"f2": Keyword(test_attr="f2")})
m.field("f3", Nested(test_attr="f3", properties={"f4": Text(test_attr="f4")}))

Expand Down Expand Up @@ -101,7 +100,7 @@ def test_mapping_can_collect_all_analyzers_and_normalizers():
)
n3 = analysis.normalizer("unknown_custom")

m = mapping.AsyncMapping()
m = AsyncMapping()
m.field(
"title",
"text",
Expand Down Expand Up @@ -160,7 +159,7 @@ def test_mapping_can_collect_multiple_analyzers():
tokenizer=analysis.tokenizer("trigram", "nGram", min_gram=3, max_gram=3),
filter=[analysis.token_filter("my_filter2", "stop", stopwords=["c", "d"])],
)
m = mapping.AsyncMapping()
m = AsyncMapping()
m.field("title", "text", analyzer=a1, search_analyzer=a2)
m.field(
"text",
Expand Down Expand Up @@ -194,7 +193,7 @@ def test_mapping_can_collect_multiple_analyzers():

def test_even_non_custom_analyzers_can_have_params():
a1 = analysis.analyzer("whitespace", type="pattern", pattern=r"\\s+")
m = mapping.AsyncMapping()
m = AsyncMapping()
m.field("title", "text", analyzer=a1)

assert {
Expand All @@ -203,14 +202,14 @@ def test_even_non_custom_analyzers_can_have_params():


def test_resolve_field_can_resolve_multifields():
m = mapping.AsyncMapping()
m = AsyncMapping()
m.field("title", "text", fields={"keyword": Keyword()})

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


def test_resolve_nested():
m = mapping.AsyncMapping()
m = AsyncMapping()
m.field("n1", "nested", properties={"n2": Nested(properties={"k1": Keyword()})})
m.field("k2", "keyword")

Expand Down
Loading
Loading