jina.types.arrays.mixins.traverse module

class jina.types.arrays.mixins.traverse.TraverseMixin[source]

Bases: object

A mixin used for traversing DocumentArray or DocumentArrayMemmap.

traverse(traversal_paths, filter_fn=None)[source]

Return an Iterator of :class:TraversableSequence of the leaves when applying the traversal_paths. Each :class:TraversableSequence is either the root Documents, a ChunkArray or a MatchArray.

Parameters
  • traversal_paths (str) – a comma-separated string that represents the traversal path

  • filter_fn (Optional[Callable[[ForwardRef], bool]]) – function to filter docs during traversal

Yield

:class:TraversableSequence of the leaves when applying the traversal_paths.

Example on traversal_paths:

  • r: docs in this TraversableSequence

  • m: all match-documents at adjacency 1

  • c: all child-documents at granularity 1

  • cc: all child-documents at granularity 2

  • mm: all match-documents at adjacency 2

  • cm: all match-document at adjacency 1 and granularity 1

  • r,c: docs in this TraversableSequence and all child-documents at granularity 1

Return type

Iterable[ForwardRef]

traverse_flat_per_path(traversal_paths, filter_fn=None)[source]

Returns a flattened :class:TraversableSequence per path in traversal_paths with all Documents, that are reached by the path.

Parameters
  • traversal_paths (str) – a comma-separated string that represents the traversal path

  • filter_fn (Optional[Callable[[ForwardRef], bool]]) – function to filter docs during traversal

Yield

:class:TraversableSequence containing the document of all leaves per path.

traverse_flat(traversal_paths, filter_fn=None)[source]

Returns a single flattened :class:TraversableSequence with all Documents, that are reached via the traversal_paths.

Warning

When defining the traversal_paths with multiple paths, the returned :class:Documents are determined at once and not on the fly. This is a different behavior then in :method:traverse and :method:traverse_flattened_per_path!

Parameters
  • traversal_paths (str) – a list of string that represents the traversal path

  • filter_fn (Optional[Callable[[ForwardRef], bool]]) – function to filter docs during traversal

Return type

Union[ForwardRef, Iterable[ForwardRef]]

Returns

a single :class:TraversableSequence containing the document of all leaves when applying the traversal_paths.

flatten(copy=True)[source]

Flatten all nested chunks and matches into one DocumentArray.

Note

Flatten an already flattened DocumentArray will have no effect.

Warning

DocumentArrayMemmap do not support copy=False.

Parameters

copy (bool) – copy the document (DAM only supports copy=True), otherwise returns a view of the original

Return type

DocumentArray

Returns

a flattened DocumentArray object.