Skip to content

Commit 1f3f66c

Browse files
Add option to exclude DSL class field from mapping
1 parent 891205c commit 1f3f66c

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
@@ -418,6 +418,9 @@ def __init__(self, name: str, bases: Tuple[type, ...], attrs: Dict[str, Any]):
418418
# the mapped_field() wrapper function was used so we need
419419
# to look for the field instance and also record any
420420
# dataclass-style defaults
421+
if attr_value.get("exclude"):
422+
# skip this field
423+
continue
421424
attr_value = attrs[name].get("_field")
422425
default_value = attrs[name].get("default") or attrs[name].get(
423426
"default_factory"
@@ -515,6 +518,7 @@ def mapped_field(
515518
init: bool = True,
516519
default: Any = None,
517520
default_factory: Optional[Callable[[], Any]] = None,
521+
exclude: bool = False,
518522
**kwargs: Any,
519523
) -> Any:
520524
"""Construct a field using dataclass behaviors
@@ -524,20 +528,23 @@ def mapped_field(
524528
options.
525529
526530
:param field: The instance of ``Field`` to use for this field. If not provided,
527-
an instance that is appropriate for the type given to the field is used.
531+
an instance that is appropriate for the type given to the field is used.
528532
:param init: a value of ``True`` adds this field to the constructor, and a
529-
value of ``False`` omits it from it. The default is ``True``.
533+
value of ``False`` omits it from it. The default is ``True``.
530534
:param default: a default value to use for this field when one is not provided
531-
explicitly.
535+
explicitly.
532536
:param default_factory: a callable that returns a default value for the field,
533-
when one isn't provided explicitly. Only one of ``factory`` and
534-
``default_factory`` can be used.
537+
when one isn't provided explicitly. Only one of ``factory`` and
538+
``default_factory`` can be used.
539+
:param exclude: Set to ``True`` to exclude this field from the Elasticsearch
540+
index.
535541
"""
536542
return _FieldMetadataDict(
537543
_field=field,
538544
init=init,
539545
default=default,
540546
default_factory=default_factory,
547+
exclude=exclude,
541548
**kwargs,
542549
)
543550

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)