Facets.js
14.2 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
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
define(function(require) {
'use strict';
var _ = require('underscore');
var Backbone = require('backbone');
var NestedModels = require('backbone-nested-models');
var Facet = require('./Facet');
var Sort = require('./Sort');
var Util = require('./Util');
var Facets = NestedModels.mixin(Backbone.Model.extend({
initialize: function(data, options) {
this.url = function() {
var url = options.search.url;
return _.isFunction(url) ? url.call() : url;
};
_.each(this.keys(), _.bind(function(facetName) {
var facet = this.get(facetName);
_.each({
'item-change': 'item-change',
'change:query': 'child-query',
'change:queryMin': 'item-change',
'change:queryMax': 'item-change',
}, function(eventValue, eventKey) {
facet.on(eventKey, function(model, value, options) {
console.log('trigger', facet.get('formName'), eventValue);
this.trigger(eventValue, facet, null, options);
}, this);
}, this);
}, this));
this._doSuggestions = _.debounce(_.bind(function(childFacet) {
if (childFacet.get('query')) {
this.fetch({
data: this.toJSON({childFacet: childFacet, facetSuggestions: true}),
childFacet: childFacet,
facetSuggestions: true,
merge: true,
traditional: true,
});
} else {
childFacet.set('suggestions', []);
}
}, this), 250);
this.on('child-query', this._doSuggestions, this);
return Facets.__super__.initialize.apply(this, arguments);
},
resetSearch: function(options) {
_.each(this.values(), function(facet) {
facet.get('items').reset(null, options);
});
},
applyFacetResults: function(data, options) {
options = options || {};
var facetCounts = Util.getField(data, 'facet_counts');
var facetIntervals = Util.getField(facetCounts, 'facet_intervals');
var facetRanges = Util.getField(facetCounts, 'facet_ranges');
var facetFields = Util.getField(facetCounts, 'facet_fields');
var facetQueries = Util.getField(facetCounts, 'facet_queries');
var statsFields = Util.getField(data.stats, 'stats_fields');
function getFacetList(facetName, type) {
switch (type) {
case 'year':
case 'range':
return facetRanges[facetName] ? facetRanges[facetName].counts : null;
case 'interval':
return facetIntervals[facetName] ? facetIntervals[facetName].counts : null;
case 'year-field':
case 'field':
return facetFields[facetName];
}
}
_.each(this.keys(), _.bind(function(facetName) {
var facet = this.get(facetName);
var type = facet.facetType;
var suggestions = [];
_.each(getFacetList('suggestions:' + facetName, type), function(value, index) {
if (index % 2 === 0) {
key = value;
} else if (value > 0) {
suggestions.push({key: key, value: value});
}
});
facet.set('suggestions', suggestions);
//if (!!options.facetSuggestions && options.childFacet === facet) {
if (!!options.facetSuggestions) {
return;
}
var key;
var list = getFacetList(facetName, type);
var newItems = [];
var items = facet.get('items');
items.invoke('set', 'hidden', true);
var valueOverrides = {};
var excludeValues = {};
function recordIncludeValueIntoExclude(includeValue, key) {
excludeValues[includeValue] = true;
}
switch (type) {
case 'year-field':
case 'field':
_.each(facet.bins, function(includeValues, key) {
var tag = facetName + ':' + key;
valueOverrides[key] = facetQueries[tag];
_.each(includeValues, recordIncludeValueIntoExclude);
});
break;
}
var addNewItem = _.bind(function addNewItem(key, value) {
if (valueOverrides[key] !== undefined) {
value = valueOverrides[key];
if (!value) {
return;
}
} else if (excludeValues[key]) {
return;
}
var item = items.get({id: key});
if (item) {
item.set({hidden: value === 0, value: value});
} else {
item = new Facet.Item({key: key, value: value});
}
newItems.push(item);
}, this);
function checkOther(set, key) {
if (set.indexOf(facet.other) !== -1) {
addNewItem(key, facetRanges[facetName][key]);
}
}
checkOther(['all', 'before'], 'before');
_.each(list, function(value, index) {
if (index % 2 === 0) {
key = value;
} else if (value > 0) {
addNewItem(key, value);
}
});
switch (type) {
case 'year-field':
case 'field':
_.each(facet.bins, function(includeValues, key) {
var tag = facetName + ':' + key;
addNewItem(key, facetQueries[tag]);
});
break;
}
items.set(newItems, {remove: false});
if (facet.facetStats) {
var thisFacetStats = statsFields[facetName];
facet.set({minValue: thisFacetStats.min, maxValue: thisFacetStats.max});
}
}, this));
},
getFacetFormData: function(options) {
options = options || {};
var result = {
facet: true,
stats: true,
'facet.missing': true,
'facet.field': [],
'facet.range': [],
'facet.interval': [],
'facet.query': [],
'stats.field': [],
fq: [],
};
_.each(this.keys(), _.bind(function(facetName) {
var facet = this.get(facetName);
var queryField = facet.queryField;
var facetField = facet.facetField ? facet.facetField : queryField;
var type = facet.facetType;
var valueFormatter;
var facetOptions = {};
var quoteFormatter = function(value) {
return value.replace ? ('"' + value.replace(/"/, '\\"') + '"') : value;
};
var rangeFormatter = function(from, to) {
return '[' + quoteFormatter(from) + ' TO ' + quoteFormatter(to) + ']';
};
switch (type) {
case 'year':
type = 'range';
quoteFormatter = function(value) {
return value.toISOString();
};
valueFormatter = function(item) {
var key = item.get('key');
var fromDate, toDate;
if (key === 'before') {
return '[* TO ' + facet.rangeStart + '-1SECOND]';
}
fromDate = new Date(key);
toDate = new Date(key);
toDate.setUTCFullYear(toDate.getUTCFullYear() + 1);
return rangeFormatter(fromDate, toDate);
};
if (facet.other) {
facetOptions['facet.range.other'] = facet.other;
}
break;
case 'range':
case 'interval':
valueFormatter = function(item) {
var key = parseInt(item.get('key'));
return rangeFormatter(key, key + facet.rangeGap);
};
break;
case 'year-field':
type = 'field';
quoteFormatter = function(value) {
return new Date(value).toISOString();
};
valueFormatter = facet.queryValue ? facet.queryValue : function(value) {
return quoteFormatter(Util.getItemKeyAccessor(value));
};
_.each(facet.bins, function(includeValues, key) {
var query = _.map(includeValues, function(includeValue, index) {
return queryField + ':\'' + includeValue + '\'';
}).join(' OR ');
result['facet.query'].push('{!key=' + facetName + ':' + key + ' tag=' + facetName + ':' + key + '}(' + query + ')');
});
break;
case 'field':
valueFormatter = facet.queryValue ? facet.queryValue : function(value) {
return quoteFormatter(Util.getItemKeyAccessor(value));
};
_.each(facet.bins, function(includeValues, key) {
var query = _.map(includeValues, function(includeValue, index) {
return queryField + ':' + includeValue;
}).join(' OR ');
result['facet.query'].push('{!key=' + facetName + ':' + key + ' tag=' + facetName + ':' + key + '}(' + query + ')');
});
break;
}
switch (type) {
case 'range':
facetOptions['facet.range.start'] = facet.rangeStart;
facetOptions['facet.range.end'] = facet.rangeEnd;
facetOptions['facet.range.gap'] = facet.rangeGap;
break;
case 'interval':
facetOptions['facet.interval.set'] = '[*,*]';
break;
}
var facetValues = [];
var queryMax = facet.get('queryMax');
var queryMin = facet.get('queryMin');
if (queryMax !== undefined && queryMin !== undefined) {
facetValues.push(rangeFormatter(queryMin, queryMax));
}
facet.get('items').each(function(item, index) {
if (!item.get('hidden') && item.get('checked')) {
facetValues.push(valueFormatter(item));
}
});
facetOptions.key = facetName;
if (facetValues.length) {
facetOptions.ex = facetName;
result.fq.push('{!tag=' + facetName + '}' + facetField + ':(' + facetValues.join(' OR ') + ')');
} else {
facetOptions.tag = facetName;
}
if (!!options.facetSuggestions) {
var childFacet = options.childFacet;
if (childFacet === facet) {
facetOptions['facet.contains'] = facet.get('query');
facetOptions['facet.contains.ignoreCase'] = true;
facetOptions['facet.mincount'] = 1;
}
}
var facetOptionList = [];
_.each(facetOptions, function(value, key) {
if (value !== undefined) {
facetOptionList.push(key + '=' + value);
}
});
result['facet.' + type].push('{!' + facetOptionList.join(' ') + '}' + queryField);
if (facet.facetStats) {
result['stats.field'].push('{!' + facetOptionList.join(' ') + '}' + queryField);
}
var facetQuery = facet.get('query');
if (facetQuery !== null && facetQuery !== '') {
facetOptions.key = 'suggestions:' + facetName;
facetOptionList = [];
_.each(facetOptions, function(value, key) {
if (value !== undefined) {
facetOptionList.push(key + '=' + value);
}
});
result['facet.' + type].push('{!' + facetOptionList.join(' ') + '}' + queryField);
}
}, this));
return result;
},
toJSON: function toJSON(options) {
if (!!options.facetSuggestions) {
return _.extend({
rows: 0,
facet: true,
wt: 'json',
q: '*:*',
}, this.getFacetFormData(options));
}
},
parse: function parse(data, options) {
if (!!options.facetSuggestions) {
this.applyFacetResults(data, options);
}
return null;
},
}));
Facets.Sort = Sort;
Facets.Facet = Facet;
return Facets;
});