-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathindex.js
More file actions
174 lines (135 loc) · 4.76 KB
/
Copy pathindex.js
File metadata and controls
174 lines (135 loc) · 4.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
This utility will convert the nested data structure
returned from an ElasticSearch query into a tabular
data structure represented as an array of row objects.
*/
function tabify(response, options) {
let table;
if (typeof (options) === 'undefined') {
options = {
debug: false
}
}
if (response.aggregations) {
const tree = collectBucket(response.aggregations);
table = flatten(tree);
} else if (response.hits) {
table = response.hits.hits.map((d) => d._source);
} else if (Array.isArray(response)) {
table = response;
} else {
throw new Error("Tabify() invoked with invalid result set. Result set must have either 'aggregations' or 'hits' defined.");
}
if (options.debug) {
console.log("Results from tabify (first 3 rows only):");
// This one shows where there are "undefined" values.
console.log(table)
// This one shows the full structure pretty-printed.
console.log(JSON.stringify(table.slice(0, 3), null, 2))
}
return table;
}
function collectBucket(node, stack=[]) {
if (!node)
return;
const keys = Object.keys(node);
// Use old school `for` so we can break control flow by returning.
for(let i = 0; i < keys.length; i++) {
const key = keys[i];
const value = node[key];
if (typeof value === 'object' && value !== null) {
if ("hits" in value && Array.isArray(value.hits) && value.hits.length === 1) {
if ("sort" in value.hits[0]) {
value.hits[0]._source['sort'] = value.hits[0].sort[0];
}
return value.hits[0]._source;
}
if (Array.isArray(value)) {
return extractTree(value, [...stack, key]);
}
// Here we are sure to have an object
if (key === "buckets" && Object.keys(value).length > 1)
{
return extractBuckets(value, [...stack, key]);
}
return collectBucket(value, [...stack, key]);
}
if (key === "value" && typeof value !== "object" && stack.length === 1) {
let collectedObject = collectBucket({[stack[0]]: value});
node = collectedObject;
}
}
return node;
}
function extractBuckets(buckets, stack) {
const keys = Object.keys(buckets);
let results = [];
for(let i = 0; i < keys.length; i++) {
const key = keys[i];
const value = buckets[key];
let currentObject = collectBucket({[key]: value});
if (!currentObject)
continue;
currentObject[stack[stack.length - 2]] = key;
results.push(currentObject)
}
return results;
}
function extractTree(buckets, stack) {
return buckets.map((bucket) => {
return Object.keys(bucket).reduce(function (tree, key) {
let value = bucket[key];
if (typeof value === "object") {
if("value" in value){
value = value.value;
} else {
value = collectBucket(value, [...stack, key]);
}
}
if(key === "key"){
key = stack[stack.length - 2]
}
tree[key] = value;
return tree;
}, {});
});
}
function flatten(tree, parentNode={}){
if (!tree)
return [];
if (!Array.isArray(tree))
tree = [tree];
return tree
// Have the child node inherit values from the parent.
.map((childNode) => Object.assign({}, parentNode, childNode))
// Each node object here has values inherited from its parent.
.map((node) => {
// Detect properties whose values are arrays.
const childTrees = Object.keys(node)
.map((key) => {
const value = node[key];
if (Array.isArray(value)) {
return value;
}
return false;
})
.filter((d) => d);
switch (childTrees.length) {
// Leaf node case, return the node.
case 0:
return node;
// Non-leaf node case, recurse on the child nodes.
case 1:
const childTree = childTrees[0];
if(childTree.length === 0){
return node;
}
return flatten(childTree, node);
default:
throw new Error("This case should never happen");
}
})
// Flatten the nested arrays.
.reduce((a, b) => a.concat(b), []);
}
module.exports = tabify;