-
Notifications
You must be signed in to change notification settings - Fork 582
/
elasticsearchBody.ts
191 lines (171 loc) · 6.04 KB
/
elasticsearchBody.ts
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
import { SearchBody as esSearchBody } from "elastic-ts";
import { decodeCursor } from "@webiny/api-elasticsearch/cursors";
import { ElasticsearchBoolQueryConfig } from "@webiny/api-elasticsearch/types";
import { createSort } from "@webiny/api-elasticsearch/sort";
import { createLimit } from "@webiny/api-elasticsearch/limit";
import { ElasticsearchQueryBuilderOperatorPlugin } from "@webiny/api-elasticsearch/plugins/definition/ElasticsearchQueryBuilderOperatorPlugin";
import { SubmissionElasticsearchFieldPlugin } from "~/plugins/SubmissionElasticsearchFieldPlugin";
import { SubmissionElasticsearchSortModifierPlugin } from "~/plugins/SubmissionElasticsearchSortModifierPlugin";
import { SubmissionElasticsearchBodyModifierPlugin } from "~/plugins/SubmissionElasticsearchBodyModifierPlugin";
import { FormBuilderStorageOperationsListSubmissionsParams } from "@webiny/api-form-builder/types";
import { SubmissionElasticsearchQueryModifierPlugin } from "~/plugins/SubmissionElasticsearchQueryModifierPlugin";
import { PluginsContainer } from "@webiny/plugins";
import { applyWhere } from "@webiny/api-elasticsearch/where";
const createInitialQueryValue = (): ElasticsearchBoolQueryConfig => {
return {
must: [
/**
* We add the __type filtering in the initial query because it must be applied.
*/
{
term: {
"__type.keyword": "fb.submission"
}
}
],
must_not: [],
should: [],
filter: []
};
};
export const createSubmissionElasticType = (): string => {
return "fb.submission";
};
interface CreateElasticsearchQueryParams extends CreateElasticsearchBodyParams {
fieldPlugins: Record<string, SubmissionElasticsearchFieldPlugin>;
}
const createElasticsearchQuery = (params: CreateElasticsearchQueryParams) => {
const { plugins, where: initialWhere, fieldPlugins } = params;
const query = createInitialQueryValue();
/**
* Be aware that, if having more registered operator plugins of same type, the last one will be used.
*/
const operatorPlugins: Record<string, ElasticsearchQueryBuilderOperatorPlugin> = plugins
.byType<ElasticsearchQueryBuilderOperatorPlugin>(
ElasticsearchQueryBuilderOperatorPlugin.type
)
.reduce((acc, plugin) => {
acc[plugin.getOperator()] = plugin;
return acc;
}, {});
const where: FormBuilderStorageOperationsListSubmissionsParams["where"] = {
...initialWhere
};
/**
* !!! IMPORTANT !!! There are few specific cases where we hardcode the query conditions.
*
* When ES index is shared between tenants, we need to filter records by tenant ID.
* No need for the tenant filtering otherwise as each index is for single tenant.
*/
const sharedIndex = process.env.ELASTICSEARCH_SHARED_INDEXES === "true";
if (sharedIndex) {
query.must.push({
term: {
"tenant.keyword": where.tenant
}
});
}
/**
* Remove tenant so it is not applied again later.
* Possibly tenant is not defined, but just in case, remove it.
*/
delete where.tenant;
/**
* Add the locale to filtering.
*/
query.must.push({
term: {
"locale.keyword": where.locale
}
});
delete where.locale;
/**
* And add the parent (form) to the filtering, if it exists.
*/
query.must.push({
term: {
"form.parent.keyword": where.formId
}
});
delete where.formId;
/**
* We apply other conditions as they are passed via the where value.
*/
applyWhere({
query,
where,
fields: fieldPlugins,
operators: operatorPlugins
});
return query;
};
interface CreateElasticsearchBodyParams {
plugins: PluginsContainer;
where: FormBuilderStorageOperationsListSubmissionsParams["where"];
limit: number;
after?: string;
sort: string[];
}
export const createElasticsearchBody = (params: CreateElasticsearchBodyParams): esSearchBody => {
const { plugins, where, limit: initialLimit, sort: initialSort, after } = params;
const fieldPlugins: Record<string, SubmissionElasticsearchFieldPlugin> = plugins
.byType<SubmissionElasticsearchFieldPlugin>(SubmissionElasticsearchFieldPlugin.type)
.reduce((acc, plugin) => {
acc[plugin.field] = plugin;
return acc;
}, {});
const limit = createLimit(initialLimit, 100);
const query = createElasticsearchQuery({
...params,
fieldPlugins
});
const sort = createSort({
sort: initialSort,
fieldPlugins
});
const queryModifiers = plugins.byType<SubmissionElasticsearchQueryModifierPlugin>(
SubmissionElasticsearchQueryModifierPlugin.type
);
for (const plugin of queryModifiers) {
plugin.modifyQuery({
query,
where
});
}
const sortModifiers = plugins.byType<SubmissionElasticsearchSortModifierPlugin>(
SubmissionElasticsearchSortModifierPlugin.type
);
for (const plugin of sortModifiers) {
plugin.modifySort({
sort
});
}
const body = {
query: {
constant_score: {
filter: {
bool: {
...query
}
}
}
},
size: limit + 1,
/**
* Casting as any is required due to search_after is accepting an array of values.
* Which is correct in some cases. In our case, it is not.
* https://www.elastic.co/guide/en/elasticsearch/reference/7.13/paginate-search-results.html
*/
search_after: decodeCursor(after) as any,
sort
};
const bodyModifiers = plugins.byType<SubmissionElasticsearchBodyModifierPlugin>(
SubmissionElasticsearchBodyModifierPlugin.type
);
for (const plugin of bodyModifiers) {
plugin.modifyBody({
body
});
}
return body;
};