docarray.array.mixins.traverse module#

class docarray.array.mixins.traverse.TraverseMixin[source]#

Bases: object

A mixin used for traversing DocumentArray.

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[[Document], 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

  • r.[attribute]: access attribute of a multi modal document

  • 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

  • r[start:end]: access sub document array using slice

Return type:

Iterable[T]

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[[Document], 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[[Document], bool]]) – function to filter docs during traversal

Return type:

DocumentArray

Returns:

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

flatten()[source]#

Flatten all nested chunks and matches into one DocumentArray.

Note

Flatten an already flattened DocumentArray will have no effect.

Return type:

DocumentArray

Returns:

a flattened DocumentArray object.