Skip to content

Commit b9ada0f

Browse files
Add option to exclude DSL class field from mapping
1 parent 8e39490 commit b9ada0f

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
@@ -417,6 +417,9 @@ def __init__(self, name: str, bases: Tuple[type, ...], attrs: Dict[str, Any]):
417417
# the mapped_field() wrapper function was used so we need
418418
# to look for the field instance and also record any
419419
# dataclass-style defaults
420+
if attr_value.get("exclude"):
421+
# skip this field
422+
continue
420423
attr_value = attrs[name].get("_field")
421424
default_value = attrs[name].get("default") or attrs[name].get(
422425
"default_factory"
@@ -514,6 +517,7 @@ def mapped_field(
514517
init: bool = True,
515518
default: Any = None,
516519
default_factory: Optional[Callable[[], Any]] = None,
520+
exclude: bool = False,
517521
**kwargs: Any,
518522
) -> Any:
519523
"""Construct a field using dataclass behaviors
@@ -523,20 +527,23 @@ def mapped_field(
523527
options.
524528
525529
:param field: The instance of ``Field`` to use for this field. If not provided,
526-
an instance that is appropriate for the type given to the field is used.
530+
an instance that is appropriate for the type given to the field is used.
527531
:param init: a value of ``True`` adds this field to the constructor, and a
528-
value of ``False`` omits it from it. The default is ``True``.
532+
value of ``False`` omits it from it. The default is ``True``.
529533
:param default: a default value to use for this field when one is not provided
530-
explicitly.
534+
explicitly.
531535
:param default_factory: a callable that returns a default value for the field,
532-
when one isn't provided explicitly. Only one of ``factory`` and
533-
``default_factory`` can be used.
536+
when one isn't provided explicitly. Only one of ``factory`` and
537+
``default_factory`` can be used.
538+
:param exclude: Set to ``True`` to exclude this field from the Elasticsearch
539+
index.
534540
"""
535541
return _FieldMetadataDict(
536542
_field=field,
537543
init=init,
538544
default=default,
539545
default_factory=default_factory,
546+
exclude=exclude,
540547
**kwargs,
541548
)
542549

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)