-
Notifications
You must be signed in to change notification settings - Fork 2
/
searchState.js
237 lines (229 loc) · 8.75 KB
/
searchState.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
const FilterNode = require('./FilterNode');
class SearchState {
constructor({
type,
appliedFilters,
appliedFilterAggTypes,
availableFilters,
filterRegistry,
filtersValid,
orphanFilters,
fieldExact,
fieldBroad,
field,
sortType,
isLoadingTail,
isLoading,
results,
numResults,
initScrollPos,
currPage,
} = {}) {
this.type = type; // always required
this.appliedFilters = appliedFilters || [];
this.appliedFilterAggTypes = appliedFilterAggTypes || [];
this.availableFilters = typeof availableFilters === 'undefined' ? [] : availableFilters.map(f => f instanceof FilterNode ? f : new FilterNode(f));
this.filterRegistry = this._recreateRegistry(this.availableFilters);
this.filtersValid = filtersValid || false;
this.orphanFilters = orphanFilters || [];
this.fieldExact = fieldExact || SearchState.metadataByType[type].fieldExact;
this.fieldBroad = fieldBroad || SearchState.metadataByType[type].fieldBroad;
this.field = field || SearchState.metadataByType[type].field;
this.sortType = sortType || SearchState.metadataByType[type].sortType;
this.isLoadingTail = isLoadingTail || false;
this.isLoading = isLoading || false;
this.results = results || [];
this.numResults = numResults || 0;
this.initScrollPos = initScrollPos || 0;
this.currPage = currPage || 0;
}
_recreateRegistry(filters, registry = {}) {
for (let f of filters) {
registry[f.aggKey] = f;
registry = this._recreateRegistry(f.children, registry);
}
return registry;
}
clone(trimFilters) {
return new SearchState({
appliedFilters: [...this.appliedFilters],
appliedFilterAggTypes: [...this.appliedFilterAggTypes],
availableFilters: trimFilters ? [] : this.availableFilters,
filterRegistry: trimFilters ? {} : this.filterRegistry,
filtersValid: trimFilters ? false : this.filtersValid,
orphanFilters: this.orphanFilters,
type: this.type,
fieldExact: this.fieldExact,
fieldBroad: this.fieldBroad,
field: this.field,
sortType: this.sortType,
isLoadingTail: this.isLoadingTail,
isLoading: this.isLoading,
results: [...this.results],
numResults: this.numResults,
initScrollPos: this.initScrollPos,
currPage: this.currPage,
});
}
update({
type,
appliedFilters,
appliedFilterAggTypes,
availableFilters,
filterRegistry,
filtersValid,
orphanFilters,
fieldExact,
fieldBroad,
field,
sortType,
aggregationsToUpdate,
isLoadingTail,
isLoading,
results,
numResults,
initScrollPos,
currPage,
}) {
type = typeof type === 'undefined' ? this.type : type;
appliedFilters = typeof appliedFilters === 'undefined' ? this.appliedFilters : appliedFilters;
appliedFilterAggTypes = typeof appliedFilterAggTypes === 'undefined' ? this.appliedFilterAggTypes : appliedFilterAggTypes;
filtersValid = typeof filtersValid === 'undefined' ? this.filtersValid : filtersValid;
orphanFilters = typeof orphanFilters === 'undefined' ? this.orphanFilters : orphanFilters;
fieldExact = typeof fieldExact === 'undefined' ? this.fieldExact : fieldExact;
fieldBroad = typeof fieldBroad === 'undefined' ? this.fieldBroad : fieldBroad;
field = typeof field === 'undefined' ? this.field : field;
sortType = typeof sortType === 'undefined' ? this.sortType : sortType;
isLoadingTail = typeof isLoadingTail === 'undefined' ? this.isLoadingTail : isLoadingTail;
isLoading = typeof isLoading === 'undefined' ? this.isLoading : isLoading;
results = typeof results === 'undefined' ? this.results : results;
numResults = typeof numResults === 'undefined' ? this.numResults : numResults;
initScrollPos = typeof initScrollPos === 'undefined' ? this.initScrollPos : initScrollPos;
currPage = typeof currPage === 'undefined' ? this.currPage : currPage;
const tempAvailableFilters = availableFilters;
const tempFilterRegistry = typeof filterRegistry === 'undefined' ? this.filterRegistry : filterRegistry;
if (!!aggregationsToUpdate && this.filtersValid) {
if (typeof tempAvailableFilters !== 'undefined') {
availableFilters = this.availableFilters.filter( f => aggregationsToUpdate.indexOf(f.aggType) === -1).concat(availableFilters);
filterRegistry = this.filterRegistry;
}
} else {
availableFilters = typeof tempAvailableFilters === 'undefined' ? this.availableFilters : tempAvailableFilters;
filterRegistry = tempFilterRegistry;
}
return new SearchState({
type,
appliedFilters,
appliedFilterAggTypes,
availableFilters,
filterRegistry,
filtersValid,
orphanFilters,
fieldExact,
fieldBroad,
field,
sortType,
isLoadingTail,
isLoading,
results,
numResults,
initScrollPos,
currPage,
});
}
isEqual({
other,
fields,
}) {
if (!(other instanceof SearchState)) { return false; }
for (let field of fields) {
const thisField = this[field];
const otherField = other[field];
if (thisField instanceof Array) {
if (!(otherField instanceof Array)) { return false; }
if (thisField.length !== otherField.length) { return false; }
if (!thisField.every((v, i) => v === otherField[i])) { return false; }
} else {
if (thisField !== otherField) { return false; }
}
}
return true;
}
makeURL({ prefix, isStart }) {
// prefix: string prepended to every parameter. meant to distinguish between different type of searchState URL parameters (e.g. sheet and text)
// oneOf({'t': 'text', 's': sheet, 'g': group, 'u': user})
const aggTypes = SearchState.metadataByType[this.type].aggregation_field_array;
const url = aggTypes.reduce( (accum, aggType) => {
const aggTypeFilters = aggTypes.length > 1 ? this.appliedFilters.filter((f, i) => this.appliedFilterAggTypes[i] === aggType) : this.appliedFilters;
return accum + (aggTypeFilters.length > 0 ? `&${prefix}${aggType}Filters=${aggTypeFilters.map( f => encodeURIComponent(f)).join('|')}` : '');
}, '') +
`&${prefix}var=` + (this.field !== this.fieldExact ? '1' : '0') +
`&${prefix}sort=${this.sortType}`;
if (isStart) {
url.replace(/&/, '?');
}
return url;
}
}
SearchState.metadataByType = {
text: {
fieldExact: 'exact',
fieldBroad: 'naive_lemmatizer',
field: 'naive_lemmatizer',
aggregation_field_array: ['path'],
build_and_apply_filters: 'buildAndApplyTextFilters', // func name from Search.js
sortType: 'relevance',
sortTypeArray: [ // this array defines the sort options available for each search type
{
type: 'relevance',
name: 'Relevance',
heName: 'רלוונטיות',
fieldArray: ['pagesheetrank'],
sort_method: 'score', // if sort_method == 'score', it will combine the standard elasticsearch score with `field`
score_missing: 0.04, // this default value comes from the equation used to calculate pagesheetrank. see search.py where this field is created
},
{
type: 'chronological',
name: 'Chronological',
heName: 'כרונולוגי',
fieldArray: ['comp_date', 'order'], // if sort_method == 'sort', then we need to define fieldArray, which is a list of fields we want to sort on
sort_method: 'sort',
direction: 'asc',
}
],
},
sheet: {
fieldExact: null,
fieldBroad: null,
field: 'content',
aggregation_field_array: ['group', 'tags'],
build_and_apply_filters: 'buildAndApplySheetFilters', // func name from Search.js
sortType: 'relevance',
sortTypeArray: [
{
type: 'relevance',
name: 'Relevance',
heName: 'רלוונטיות',
fieldArray: [],
sort_method: 'score',
},
{
type: 'dateCreated',
name: 'Date Created',
heName: 'תאריך',
fieldArray: ['dateCreated'],
sort_method: 'sort',
direction: 'desc',
},
{
type: 'views',
name: 'Views',
heName: 'צפיות',
fieldArray: ['views'],
sort_method: 'sort',
direction: 'desc',
},
],
},
};
module.exports = SearchState;