docarray.array.mixins.reduce module#

class docarray.array.mixins.reduce.ReduceMixin[source]#

Bases: object

A mixin that provides reducing logic for DocumentArray Reducing 2 or more DocumentArrays consists in merging all Documents into the same DocumentArray. If a Document belongs to 2 or more DocumentArrays, it is added once and data attributes are merged with priority to the Document belonging to the left-most DocumentArray. Matches and chunks are also reduced in the same way. Reduction is applied to all levels of DocumentArrays, that is, from root Documents to all their chunk and match children.

reduce(other)[source]#

Reduces other and the current DocumentArray into one DocumentArray in-place. Changes are applied to the current DocumentArray. Reducing 2 DocumentArrays consists in adding Documents in the second DocumentArray to the first DocumentArray if they do not exist. If a Document exists in both DocumentArrays, the data properties are merged with priority to the first Document (that is, to the current DocumentArray’s Document). The matches and chunks are also reduced in the same way. :type other: T :param other: DocumentArray :rtype: T :return: DocumentArray

reduce_all(others)[source]#

Reduces a list of DocumentArrays and this DocumentArray into one DocumentArray. Changes are applied to this DocumentArray in-place.

Reduction consists in reducing this DocumentArray with every DocumentArray in others sequentially using DocumentArray.:method:reduce. The resulting DocumentArray contains Documents of all DocumentArrays. If a Document exists in many DocumentArrays, data properties are merged with priority to the left-most DocumentArrays (that is, if a data attribute is set in a Document belonging to many DocumentArrays, the attribute value of the left-most DocumentArray is kept). Matches and chunks of a Document belonging to many DocumentArrays are also reduced in the same way. Other non-data properties are ignored.

Note

  • Matches are not kept in a sorted order when they are reduced. You might want to re-sort them in a later

    step.

  • The final result depends on the order of DocumentArrays when applying reduction.

Parameters:

others (List[T]) – List of DocumentArrays to be reduced

Return type:

T

Returns:

the resulting DocumentArray