/
index.js
308 lines (275 loc) · 8.42 KB
/
index.js
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import Appbase from 'appbase-js';
import valueReducer from '@appbaseio/reactivecore/lib/reducers/valueReducer';
import queryReducer from '@appbaseio/reactivecore/lib/reducers/queryReducer';
import queryOptionsReducer from '@appbaseio/reactivecore/lib/reducers/queryOptionsReducer';
import dependencyTreeReducer from '@appbaseio/reactivecore/lib/reducers/dependencyTreeReducer';
import { buildQuery, pushToAndClause } from '@appbaseio/reactivecore/lib/utils/helper';
const componentsWithHighlightQuery = [
'DataSearch',
'CategorySearch',
];
const componentsWithOptions = [
'ResultList',
'ResultCard',
'ReactiveList',
'ReactiveMap',
'SingleList',
'MultiList',
'TagCloud',
...componentsWithHighlightQuery,
];
const componentsWithoutFilters = [
'NumberBox',
'RangeSlider',
'DynamicRangeSlider',
'RangeInput',
'RatingsFilter',
];
const resultComponents = ['ResultCard', 'ResultList', 'ReactiveList', 'ReactiveMap'];
function getValue(state, id, defaultValue) {
if (!state) return defaultValue;
if (state[id]) {
try {
const parsedValue = JSON.parse(state[id]);
return parsedValue;
} catch (error) {
// eslint-disable-next-line
console.log('REACTIVESEARCH - could not parse the search state for', id);
}
}
return defaultValue;
}
function parseValue(value, component) {
if (component.source && component.source.parseValue) {
return component.source.parseValue(value, component);
}
return value;
}
function getQuery(component, value) {
// get default query of result components
if (resultComponents.includes(component.type)) {
return component.defaultQuery ? component.defaultQuery() : {};
}
// get custom or default query of sensor components
const currentValue = parseValue(value, component);
if (component.customQuery) {
return component.customQuery(currentValue, component);
}
return component.source.defaultQuery
? component.source.defaultQuery(currentValue, component)
: {};
}
export default function initReactivesearch(componentCollection, searchState, settings) {
return new Promise((resolve, reject) => {
const credentials = settings.url && settings.url.trim() !== '' && !settings.credentials
? null
: settings.credentials;
const config = {
url: settings.url && settings.url.trim() !== '' ? settings.url : 'https://scalr.api.appbase.io',
app: settings.app,
credentials,
transformRequest: settings.transformRequest || null,
type: settings.type ? settings.type : '*',
};
const appbaseRef = Appbase(config);
let components = [];
let selectedValues = {};
let queryList = {};
let queryLog = {};
let queryOptions = {};
let dependencyTree = {};
let finalQuery = [];
let orderOfQueries = [];
let hits = {};
let aggregations = {};
let state = {};
componentCollection.forEach((component) => {
components = [...components, component.componentId];
let isInternalComponentPresent = false;
const isResultComponent = resultComponents.includes(component.type);
const internalComponent = `${component.componentId}__internal`;
const label = component.filterLabel || component.componentId;
const value = getValue(searchState, label, component.defaultSelected);
// [1] set selected values
let showFilter = component.showFilter !== undefined ? component.showFilter : true;
if (componentsWithoutFilters.includes(component.type)) {
showFilter = false;
}
selectedValues = valueReducer(selectedValues, {
type: 'SET_VALUE',
component: component.componentId,
label,
value,
showFilter,
URLParams: component.URLParams || false,
});
// [2] set query options - main component query (valid for result components)
if (componentsWithOptions.includes(component.type)) {
const options = component.source.generateQueryOptions
? component.source.generateQueryOptions(component)
: null;
let highlightQuery = {};
if (
componentsWithHighlightQuery.includes(component.type)
&& component.highlight
) {
highlightQuery = component.source.highlightQuery(component);
}
if (
(options && Object.keys(options).length)
|| (highlightQuery && Object.keys(highlightQuery).length)
) {
// eslint-disable-next-line
let { aggs, size, ...otherQueryOptions } = options || {};
if (aggs && Object.keys(aggs).length) {
isInternalComponentPresent = true;
// query should be applied on the internal component
// to enable feeding the data to parent component
queryOptions = queryOptionsReducer(queryOptions, {
type: 'SET_QUERY_OPTIONS',
component: internalComponent,
options: { aggs, size: size || 100 },
});
}
// sort, highlight, size, from - query should be applied on the main component
if (
(otherQueryOptions && Object.keys(otherQueryOptions).length)
|| (highlightQuery && Object.keys(highlightQuery).length)
) {
if (!otherQueryOptions) otherQueryOptions = {};
if (!highlightQuery) highlightQuery = {};
let mainQueryOptions = { ...otherQueryOptions, ...highlightQuery, size };
if (isInternalComponentPresent) {
mainQueryOptions = { ...otherQueryOptions, ...highlightQuery };
}
if (isResultComponent) {
let currentPage = component.currentPage
? component.currentPage - 1
: 0;
if (
selectedValues[component.componentId]
&& selectedValues[component.componentId].value
) {
currentPage = selectedValues[component.componentId].value - 1 || 0;
}
const resultSize = component.size || 10;
mainQueryOptions = {
...mainQueryOptions,
...highlightQuery,
size: resultSize,
from: currentPage * resultSize,
};
}
queryOptions = queryOptionsReducer(queryOptions, {
type: 'SET_QUERY_OPTIONS',
component: component.componentId,
options: { ...mainQueryOptions },
});
}
}
}
// [3] set dependency tree
if (component.react || isInternalComponentPresent || isResultComponent) {
let { react } = component;
if (isInternalComponentPresent || isResultComponent) {
react = pushToAndClause(react, internalComponent);
}
dependencyTree = dependencyTreeReducer(dependencyTree, {
type: 'WATCH_COMPONENT',
component: component.componentId,
react,
});
}
// [4] set query list
if (isResultComponent) {
const { query } = getQuery(component);
queryList = queryReducer(queryList, {
type: 'SET_QUERY',
component: internalComponent,
query,
});
} else {
queryList = queryReducer(queryList, {
type: 'SET_QUERY',
component: component.componentId,
query: getQuery(component, value),
});
}
});
// [5] Generate finalQuery for search
componentCollection.forEach((component) => {
// eslint-disable-next-line
let { queryObj, options } = buildQuery(
component.componentId,
dependencyTree,
queryList,
queryOptions,
);
const validOptions = ['aggs', 'from', 'sort'];
// check if query or options are valid - non-empty
if (
(queryObj && !!Object.keys(queryObj).length)
|| (options && (
Object.keys(options).some(item => validOptions.includes(item)))
)
) {
if (!queryObj || (queryObj && !Object.keys(queryObj).length)) {
queryObj = { match_all: {} };
}
orderOfQueries = [...orderOfQueries, component.componentId];
const currentQuery = {
query: { ...queryObj },
...options,
...queryOptions[component.componentId],
};
queryLog = {
...queryLog,
[component.componentId]: currentQuery,
};
finalQuery = [
...finalQuery,
{
preference: component.componentId,
},
currentQuery,
];
}
});
state = {
components,
dependencyTree,
queryList,
queryOptions,
selectedValues,
queryLog,
};
appbaseRef.msearch({
type: config.type === '*' ? '' : config.type,
body: config.transformRequest ? config.transformRequest(finalQuery) : finalQuery,
}).then((res) => {
orderOfQueries.forEach((component, index) => {
const response = res.responses[index];
if (response.aggregations) {
aggregations = {
...aggregations,
[component]: response.aggregations,
};
}
hits = {
...hits,
[component]: {
hits: response.hits.hits,
total: response.hits.total,
time: response.took,
},
};
});
state = {
...state,
hits,
aggregations,
};
resolve(state);
}).catch(err => reject(err));
});
}