Skip to content

Commit 35f63cd

Browse files
Add option to exclude DSL class field from mapping
1 parent db2cf29 commit 35f63cd

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

elasticsearch/dsl/document_base.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,9 @@ def __init__(self, name: str, bases: Tuple[type, ...], attrs: Dict[str, Any]):
432432
# the mapped_field() wrapper function was used so we need
433433
# to look for the field instance and also record any
434434
# dataclass-style defaults
435+
if attr_value.get("exclude"):
436+
# skip this field
437+
continue
435438
attr_value = attrs[name].get("_field")
436439
default_value = attrs[name].get("default") or attrs[name].get(
437440
"default_factory"
@@ -529,6 +532,7 @@ def mapped_field(
529532
init: bool = True,
530533
default: Any = None,
531534
default_factory: Optional[Callable[[], Any]] = None,
535+
exclude: bool = False,
532536
**kwargs: Any,
533537
) -> Any:
534538
"""Construct a field using dataclass behaviors
@@ -538,20 +542,23 @@ def mapped_field(
538542
options.
539543
540544
:param field: The instance of ``Field`` to use for this field. If not provided,
541-
an instance that is appropriate for the type given to the field is used.
545+
an instance that is appropriate for the type given to the field is used.
542546
:param init: a value of ``True`` adds this field to the constructor, and a
543-
value of ``False`` omits it from it. The default is ``True``.
547+
value of ``False`` omits it from it. The default is ``True``.
544548
:param default: a default value to use for this field when one is not provided
545-
explicitly.
549+
explicitly.
546550
:param default_factory: a callable that returns a default value for the field,
547-
when one isn't provided explicitly. Only one of ``factory`` and
548-
``default_factory`` can be used.
551+
when one isn't provided explicitly. Only one of ``factory`` and
552+
``default_factory`` can be used.
553+
:param exclude: Set to ``True`` to exclude this field from the Elasticsearch
554+
index.
549555
"""
550556
return _FieldMetadataDict(
551557
_field=field,
552558
init=init,
553559
default=default,
554560
default_factory=default_factory,
561+
exclude=exclude,
555562
**kwargs,
556563
)
557564

test_elasticsearch/test_dsl/_async/test_document.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,7 @@ class TypedDoc(AsyncDocument):
678678
)
679679
i1: ClassVar
680680
i2: ClassVar[int]
681+
i3: str = mapped_field(exclude=True)
681682

682683
class TypedDocAnnotated(AsyncDocument):
683684
st: Annotated[str, "foo"]
@@ -705,6 +706,7 @@ class TypedDocAnnotated(AsyncDocument):
705706
else:
706707
i1: ClassVar
707708
i2: ClassVar[int]
709+
i3: Annotated[str, mapped_field(exclude=True)]
708710

709711
for doc_class in [TypedDoc, TypedDocAnnotated]:
710712
props = doc_class._doc_type.mapping.to_dict()["properties"]
@@ -741,6 +743,7 @@ class TypedDocAnnotated(AsyncDocument):
741743

742744
doc_class.i1 = "foo"
743745
doc_class.i2 = 123
746+
doc_class.i3 = "bar"
744747

745748
doc = doc_class()
746749
assert doc.k3 == "foo"
@@ -759,6 +762,7 @@ class TypedDocAnnotated(AsyncDocument):
759762

760763
assert doc_class.i1 == "foo"
761764
assert doc_class.i2 == 123
765+
assert doc_class.i3 == "bar"
762766

763767
doc.st = "s"
764768
doc.li = [1, 2, 3]

test_elasticsearch/test_dsl/_sync/test_document.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,7 @@ class TypedDoc(Document):
678678
)
679679
i1: ClassVar
680680
i2: ClassVar[int]
681+
i3: str = mapped_field(exclude=True)
681682

682683
class TypedDocAnnotated(Document):
683684
st: Annotated[str, "foo"]
@@ -705,6 +706,7 @@ class TypedDocAnnotated(Document):
705706
else:
706707
i1: ClassVar
707708
i2: ClassVar[int]
709+
i3: Annotated[str, mapped_field(exclude=True)]
708710

709711
for doc_class in [TypedDoc, TypedDocAnnotated]:
710712
props = doc_class._doc_type.mapping.to_dict()["properties"]
@@ -741,6 +743,7 @@ class TypedDocAnnotated(Document):
741743

742744
doc_class.i1 = "foo"
743745
doc_class.i2 = 123
746+
doc_class.i3 = "bar"
744747

745748
doc = doc_class()
746749
assert doc.k3 == "foo"
@@ -759,6 +762,7 @@ class TypedDocAnnotated(Document):
759762

760763
assert doc_class.i1 == "foo"
761764
assert doc_class.i2 == 123
765+
assert doc_class.i3 == "bar"
762766

763767
doc.st = "s"
764768
doc.li = [1, 2, 3]

0 commit comments

Comments
 (0)