flow
Search…
merge
Using the merge reduction strategy
merge reduces the LHS and RHS by recursively reducing shared document locations. The LHS and RHS must either both be objects, or both be arrays.
If both sides are objects, merge performs a deep merge of each property. If LHS and RHS are both arrays, items at each index of both sides are merged together, extending the shorter of the two sides by taking items off the longer:
1
collections:
2
- name: example/reductions/merge
3
schema:
4
type: object
5
reduce: { strategy: merge }
6
properties:
7
key: { type: string }
8
value:
9
# Merge only works with types "array" or "object".
10
# Others will throw an error at build time.
11
type: [array, object]
12
reduce: { strategy: merge }
13
# Deeply merge sub-locations (items or properties) by summing them.
14
items:
15
type: number
16
reduce: { strategy: sum }
17
additionalProperties:
18
type: number
19
reduce: { strategy: sum }
20
required: [key]
21
key: [/key]
22
23
tests:
24
"Expect we can merge arrays by index":
25
- ingest:
26
collection: example/reductions/merge
27
documents:
28
- { key: "key", value: [1, 1] }
29
- { key: "key", value: [2, 2, 2] }
30
- verify:
31
collection: example/reductions/merge
32
documents:
33
- { key: "key", value: [3, 3, 2] }
34
35
"Expect we can merge objects by property":
36
- ingest:
37
collection: example/reductions/merge
38
documents:
39
- { key: "key", value: { "a": 1, "b": 1 } }
40
- { key: "key", value: { "a": 1, "c": 1 } }
41
- verify:
42
collection: example/reductions/merge
43
documents:
44
- { key: "key", value: { "a": 2, "b": 1, "c": 1 } }
Copied!
Merge may also take a key, which is one or more JSON pointers that are relative to the reduced location. If both sides are arrays and a merge key is present, then a deep sorted merge of the respective items is done, as ordered by the key. Arrays must be pre-sorted and de-duplicated by the key, and merge itself always maintains this invariant.
Note that you can use a key of [“”] for natural item ordering, such as merging sorted arrays of scalars.
1
collections:
2
- name: example/reductions/merge-key
3
schema:
4
type: object
5
reduce: { strategy: merge }
6
properties:
7
key: { type: string }
8
value:
9
type: array
10
reduce:
11
strategy: merge
12
key: [/k]
13
items: { reduce: { strategy: firstWriteWins } }
14
required: [key]
15
key: [/key]
16
17
tests:
18
"Expect we can merge sorted arrays":
19
- ingest:
20
collection: example/reductions/merge-key
21
documents:
22
- { key: "key", value: [{ k: "a", v: 1 }, { k: "b", v: 1 }] }
23
- { key: "key", value: [{ k: "a", v: 2 }, { k: "c", v: 2 }] }
24
- verify:
25
collection: example/reductions/merge-key
26
documents:
27
- {
28
key: "key",
29
value: [{ k: "a", v: 1 }, { k: "b", v: 1 }, { k: "c", v: 2 }],
30
}
Copied!
As with append, the left-hand side of merge may be null, in which case the reduction is treated as a no-op and its result remains null.
Last modified 3mo ago
Copy link