Skip to content

Commit 80a3b28

Browse files
committed
Update group_by documentation
1 parent 3253f07 commit 80a3b28

File tree

1 file changed

+111
-24
lines changed
  • src/app/docs/group-query-results

1 file changed

+111
-24
lines changed

src/app/docs/group-query-results/page.md

Lines changed: 111 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@ If we need to group variables according to some criteria, we can create an aggre
1919

2020
A group by is composed of a _focus_, a _template_, and a _group_ together with a query.
2121

22-
We will demonstrate this with the following query:
22+
### Single Variable Template (Flat Array)
23+
24+
When grouping a single variable, the template produces a **flat array** of values:
2325

2426
```javascript
25-
let v = Vars("person", "label", "eyes", "group");
27+
let v = Vars("person", "label", "eyes", "group", "member");
2628
limit(1)
2729
.group_by(
2830
"eyes",
@@ -35,7 +37,7 @@ limit(1)
3537

3638
The first argument, here `"eyes"` refers to the eyes variable, and is the variable around which to form the group, the _focus_.
3739

38-
The second `["label"]` is the _template_, which refers to the variable `"label"`. The template will be those things grouped under the first variable.
40+
The second `["label"]` is the _template_, which refers to the variable `"label"`. Since the template contains only one variable, the results will be collected as a **flat array**.
3941

4042
The third variable `v.group`, is the _group_ variable, which will include groups of templates for each set of solutions which shares a _focus_.
4143

@@ -48,48 +50,133 @@ This raw query output will be:
4850
"@value": "black"
4951
},
5052
"group": [
51-
[{
53+
{
5254
"@type": "xsd:string",
5355
"@value": "Greedo"
54-
}],
55-
[{
56+
},
57+
{
5658
"@type": "xsd:string",
5759
"@value": "Nien Nunb"
58-
}],
59-
[{
60+
},
61+
{
6062
"@type": "xsd:string",
6163
"@value": "Gasgano"
62-
}],
63-
[{
64+
},
65+
{
6466
"@type": "xsd:string",
6567
"@value": "Kit Fisto"
66-
}],
67-
[{
68+
},
69+
{
6870
"@type": "xsd:string",
6971
"@value": "Plo Koon"
70-
}],
71-
[{
72+
},
73+
{
7274
"@type": "xsd:string",
7375
"@value": "Lama Su"
74-
}],
75-
[{
76+
},
77+
{
7678
"@type": "xsd:string",
7779
"@value": "Taun We"
78-
}],
79-
[{
80+
},
81+
{
8082
"@type": "xsd:string",
8183
"@value": "Shaak Ti"
82-
}],
83-
[{
84+
},
85+
{
8486
"@type": "xsd:string",
8587
"@value": "Tion Medon"
86-
}],
87-
[{
88+
},
89+
{
8890
"@type": "xsd:string",
8991
"@value": "BB8"
90-
}]
92+
}
93+
],
94+
"label": null,
95+
"person": null
96+
}
97+
```
98+
99+
Notice how each result is a flat array of values, not nested arrays. To make this into individual solutions, use the `member` operator: `member(v.member, v.group)`, which will expand the `group` array into individual elements in the v.member variable.
100+
101+
### Multiple Variable Template (Array of Tuples)
102+
103+
When grouping multiple variables, the template produces an **array of tuples**:
104+
105+
```javascript
106+
let v = Vars("person", "label", "eyes", "group");
107+
limit(1)
108+
.group_by(
109+
"eyes",
110+
["label", "eyes"],
111+
v.group,
112+
and(triple(v.person, "rdf:type", "@schema:People"),
113+
triple(v.person, "label", v.label),
114+
triple(v.person, "eye_color", v.eyes)))
115+
```
116+
117+
Now the template `["label", "eyes"]` includes two variables, so each result will be a tuple (array) containing both values:
118+
119+
```json
120+
{
121+
"eyes": {
122+
"@type": "xsd:string",
123+
"@value": "black"
124+
},
125+
"group": [
126+
[
127+
{
128+
"@type": "xsd:string",
129+
"@value": "Greedo"
130+
},
131+
{
132+
"@type": "xsd:string",
133+
"@value": "black"
134+
}
135+
],
136+
[
137+
{
138+
"@type": "xsd:string",
139+
"@value": "Nien Nunb"
140+
},
141+
{
142+
"@type": "xsd:string",
143+
"@value": "black"
144+
}
145+
],
146+
[
147+
{
148+
"@type": "xsd:string",
149+
"@value": "Gasgano"
150+
},
151+
{
152+
"@type": "xsd:string",
153+
"@value": "black"
154+
}
155+
],
156+
[
157+
{
158+
"@type": "xsd:string",
159+
"@value": "Kit Fisto"
160+
},
161+
{
162+
"@type": "xsd:string",
163+
"@value": "black"
164+
}
165+
],
166+
[
167+
{
168+
"@type": "xsd:string",
169+
"@value": "Plo Koon"
170+
},
171+
{
172+
"@type": "xsd:string",
173+
"@value": "black"
174+
}
175+
]
91176
],
92177
"label": null,
93178
"person": null
94179
}
95-
```
180+
```
181+
182+
Each element in the `group` array is now a tuple `[label, eyes]` containing both values.

0 commit comments

Comments
 (0)