-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery.ts
187 lines (161 loc) · 4.74 KB
/
query.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
import { Query } from "./es-types";
import { Document, Provider, Params, Result } from "./service";
type RangeArguments = {
gt?: string | number;
gte?: string | number;
lt?: string | number;
lte?: string | number;
};
export class OneOfFilterGroupBuilder {
private group: { field: string; terms: (string | null)[] }[] = [];
public add(field: string, terms: (string | null)[]): OneOfFilterGroupBuilder {
this.group.push({ field, terms });
return this;
}
public build(): { field: string; terms: (string | null)[] }[] {
return this.group;
}
}
const itemsWithValue = (s: string | null): s is string => !!s;
const transformToString = (s: string) => `${s}`;
export class QueryBuilder<D extends Document> {
private docsProvider: Provider<D>;
private searchParams: Params;
public constructor({ docsProvider }: { docsProvider: Provider<D> }) {
this.docsProvider = docsProvider;
this.searchParams = {
string: "",
nested: [],
filter: [],
grants: [],
facets: {},
rerank: [],
window: { from: 0, size: 0 },
};
this.searchParams.facets = {
status: { terms: { field: "status" } },
recruiters: { terms: { field: "recruiter.id" } },
};
}
public withSearch(string: string): QueryBuilder<D> {
this.searchParams.string = string;
return this;
}
public withNested(string: string): QueryBuilder<D> {
this.searchParams.nested.push(string);
return this;
}
public withFilter(field: string, terms: (string | null)[]): QueryBuilder<D> {
const filter: Query[] = [
{
terms: {
[field]: terms.filter(itemsWithValue).map(transformToString),
},
},
];
if (terms.includes(null)) {
filter.push({ bool: { must_not: { exists: { field } } } });
}
this.searchParams.filter.push({ bool: { should: filter } });
return this;
}
public withFiltersMatchPhrasePrefix(
fields: { field: string; term: string }[]
): QueryBuilder<D> {
const filters = fields.map((item) => {
const filter: Query = {
match_phrase_prefix: { [item.field]: item.term },
};
return filter;
});
this.searchParams.filter.push({ bool: { should: filters } });
return this;
}
public withNestedFilter(
source: string,
field: string,
terms: string[]
): QueryBuilder<D> {
this.searchParams.filter.push({
nested: {
path: source,
query: { terms: { [`${source}.${field}`]: terms } },
},
});
return this;
}
public withRange(field: string, values: RangeArguments): QueryBuilder<D> {
this.searchParams.filter.push({
range: { [field]: values },
});
return this;
}
// Returns an object used to build a clause that filters documents that match
// any of the clauses in the group.
public getOneOfFilterGroupBuilder(): OneOfFilterGroupBuilder {
return new OneOfFilterGroupBuilder();
}
public withOneOfFilter(
options: { field: string; terms: (string | null)[] }[]
): QueryBuilder<D> {
const group: Query[] = [];
options.forEach(({ field, terms }) => {
if (terms.includes(null)) {
group.push({ bool: { must_not: { exists: { field } } } });
}
const values = terms.filter(itemsWithValue).map(transformToString);
if (values.length === 0) {
return;
}
const filter = { terms: { [field]: values } };
group.push(filter);
});
if (group.length === 0) {
return this;
}
this.searchParams.filter.push({ bool: { should: group } });
return this;
}
public withGrants(field: string, terms: (string | null)[]): QueryBuilder<D> {
const grants: Query[] = [
{
terms: {
[field]: terms.filter(itemsWithValue).map(transformToString),
},
},
];
if (terms.includes(null)) {
grants.push({ bool: { must_not: { exists: { field } } } });
}
this.searchParams.grants.push({ bool: { should: grants } });
return this;
}
public withNestedGrants(
source: string,
field: string,
terms: string[]
): QueryBuilder<D> {
this.searchParams.grants.push({
nested: {
path: source,
query: { terms: { [`${source}.${field}`]: terms } },
},
});
return this;
}
public withCustomFilter(query: Query[]): QueryBuilder<D> {
this.searchParams.filter.push(query);
return this;
}
public withSortBy(field: string, direction: "asc" | "desc"): QueryBuilder<D> {
this.searchParams.rerank.push({ [field]: { order: direction } });
return this;
}
public onWindow(from: number, size: number): QueryBuilder<D> {
this.searchParams.window = { from, size };
return this;
}
public search(): Promise<Result<D>> {
return this.docsProvider.search(this.searchParams);
}
}