flow
Search…
minimize and maximize
Using the minimize and maximize reduction strategies
minimize and maximize reduce by taking the smallest or largest seen value, respectively.
1
collections:
2
- name: example/reductions/min-max
3
schema:
4
type: object
5
reduce: { strategy: merge }
6
properties:
7
key: { type: string }
8
min: { reduce: { strategy: minimize } }
9
max: { reduce: { strategy: maximize } }
10
required: [key]
11
key: [/key]
12
13
tests:
14
"Expect we can min/max values":
15
- ingest:
16
collection: example/reductions/min-max
17
documents:
18
- { key: "key", min: 32, max: "abc" }
19
- { key: "key", min: 42, max: "def" }
20
- verify:
21
collection: example/reductions/min-max
22
documents:
23
- { key: "key", min: 32, max: "def" }
Copied!
minimize and maximize can also take a key, which is one or more JSON pointers that are relative to the reduced location. Keys make it possible to minimize and maximize over complex types, by ordering over an extracted composite key.
In the event that a RHS document key equals the current LHS minimum or maximum, the documents are deeply merged. This can be used to, for example, track not just the minimum value but also the number of times it’s been seen:
1
collections:
2
- name: example/reductions/min-max-key
3
schema:
4
type: object
5
reduce: { strategy: merge }
6
properties:
7
key: { type: string }
8
min:
9
$anchor: min-max-value
10
type: array
11
items:
12
- type: string
13
- type: number
14
reduce: { strategy: sum }
15
reduce:
16
strategy: minimize
17
key: [/0]
18
max:
19
$ref: "#min-max-value"
20
reduce:
21
strategy: maximize
22
key: [/0]
23
required: [key]
24
key: [/key]
25
26
tests:
27
"Expect we can min/max values using a key extractor":
28
- ingest:
29
collection: example/reductions/min-max-key
30
documents:
31
- { key: "key", min: ["a", 1], max: ["a", 1] }
32
- { key: "key", min: ["c", 2], max: ["c", 2] }
33
- { key: "key", min: ["b", 3], max: ["b", 3] }
34
- { key: "key", min: ["a", 4], max: ["a", 4] }
35
- verify:
36
collection: example/reductions/min-max-key
37
documents:
38
# Min of equal keys ["a", 1] and ["a", 4] => ["a", 5].
39
- { key: "key", min: ["a", 5], max: ["c", 2] }
Copied!

Last modified 3mo ago
Copy link
Contents