Merge pull request #372 from kielni/filters_and_facets
Add method to replace a filter - thx to @kleini.
This commit is contained in:
commit
e007fff15a
49
src/model.js
49
src/model.js
@ -510,6 +510,23 @@ my.Query = Backbone.Model.extend({
|
||||
filters.push(ourfilter);
|
||||
this.trigger('change:filters:new-blank');
|
||||
},
|
||||
replaceFilter: function(filter) {
|
||||
// delete filter on the same field, then add
|
||||
var filters = this.get('filters');
|
||||
var idx = -1;
|
||||
_.each(this.get('filters'), function(filter, key, list) {
|
||||
if (filter.field == filter.field) {
|
||||
idx = key;
|
||||
}
|
||||
});
|
||||
// trigger just one event (change:filters:new-blank) instead of one for remove and
|
||||
// one for add
|
||||
if (idx >= 0) {
|
||||
filters.splice(idx, 1);
|
||||
this.set({filters: filters});
|
||||
}
|
||||
this.addFilter(filter);
|
||||
},
|
||||
updateFilter: function(index, value) {
|
||||
},
|
||||
// ### removeFilter
|
||||
@ -526,7 +543,7 @@ my.Query = Backbone.Model.extend({
|
||||
// Add a Facet to this query
|
||||
//
|
||||
// See <http://www.elasticsearch.org/guide/reference/api/search/facets/>
|
||||
addFacet: function(fieldId) {
|
||||
addFacet: function(fieldId, size, silent) {
|
||||
var facets = this.get('facets');
|
||||
// Assume id and fieldId should be the same (TODO: this need not be true if we want to add two different type of facets on same field)
|
||||
if (_.contains(_.keys(facets), fieldId)) {
|
||||
@ -535,8 +552,13 @@ my.Query = Backbone.Model.extend({
|
||||
facets[fieldId] = {
|
||||
terms: { field: fieldId }
|
||||
};
|
||||
if (!_.isUndefined(size)) {
|
||||
facets[fieldId].terms.size = size;
|
||||
}
|
||||
this.set({facets: facets}, {silent: true});
|
||||
this.trigger('facet:add', this);
|
||||
if (!silent) {
|
||||
this.trigger('facet:add', this);
|
||||
}
|
||||
},
|
||||
addHistogramFacet: function(fieldId) {
|
||||
var facets = this.get('facets');
|
||||
@ -548,7 +570,30 @@ my.Query = Backbone.Model.extend({
|
||||
};
|
||||
this.set({facets: facets}, {silent: true});
|
||||
this.trigger('facet:add', this);
|
||||
},
|
||||
removeFacet: function(fieldId) {
|
||||
var facets = this.get('facets');
|
||||
// Assume id and fieldId should be the same (TODO: this need not be true if we want to add two different type of facets on same field)
|
||||
if (!_.contains(_.keys(facets), fieldId)) {
|
||||
return;
|
||||
}
|
||||
delete facets[fieldId];
|
||||
this.set({facets: facets}, {silent: true});
|
||||
this.trigger('facet:remove', this);
|
||||
},
|
||||
clearFacets: function() {
|
||||
var facets = this.get('facets');
|
||||
_.each(_.keys(facets), function(fieldId) {
|
||||
delete facets[fieldId];
|
||||
});
|
||||
this.trigger('facet:remove', this);
|
||||
},
|
||||
// trigger a facet add; use this to trigger a single event after adding
|
||||
// multiple facets
|
||||
refreshFacets: function() {
|
||||
this.trigger('facet:add', this);
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
|
||||
|
||||
@ -347,6 +347,30 @@ test('Query', function () {
|
||||
deepEqual({terms: {field: 'xyz'}}, query.get('facets')['xyz']);
|
||||
});
|
||||
|
||||
test('Query.addFacet', function () {
|
||||
var query = new recline.Model.Query();
|
||||
query.addFacet('xyz', 25);
|
||||
deepEqual({terms: {field: 'xyz', "size": 25}}, query.get('facets')['xyz']);
|
||||
});
|
||||
|
||||
test('Query.removeFacet', function () {
|
||||
var query = new recline.Model.Query();
|
||||
query.addFacet('xyz');
|
||||
deepEqual({terms: {field: 'xyz'}}, query.get('facets')['xyz']);
|
||||
query.removeFacet('xyz');
|
||||
equal(undefined, query.get('facets')['xyz']);
|
||||
});
|
||||
|
||||
test('Query.clearFacets', function () {
|
||||
var query = new recline.Model.Query();
|
||||
query.addFacet('abc');
|
||||
query.addFacet('xyz');
|
||||
deepEqual({terms: {field: 'xyz'}}, query.get('facets')['xyz']);
|
||||
deepEqual({terms: {field: 'abc'}}, query.get('facets')['abc']);
|
||||
query.clearFacets();
|
||||
deepEqual({}, query.get('facets'));
|
||||
});
|
||||
|
||||
test('Query.addFilter', function () {
|
||||
var query = new recline.Model.Query();
|
||||
query.addFilter({type: 'term', field: 'xyz'});
|
||||
@ -374,4 +398,24 @@ test('Query.addFilter', function () {
|
||||
deepEqual(exp, query.get('filters')[2]);
|
||||
});
|
||||
|
||||
test('Query.replaceFilter', function () {
|
||||
var query = new recline.Model.Query();
|
||||
query.addFilter({type: 'term', field: 'xyz'});
|
||||
var exp = {
|
||||
field: 'xyz',
|
||||
type: 'term',
|
||||
term: ''
|
||||
};
|
||||
deepEqual(query.get('filters')[0], exp);
|
||||
|
||||
query.replaceFilter({type: 'term', field: 'abc'});
|
||||
exp = {
|
||||
field: 'abc',
|
||||
type: 'term',
|
||||
term: ''
|
||||
};
|
||||
deepEqual(query.get('filters')[0], exp);
|
||||
|
||||
});
|
||||
|
||||
})(this.jQuery);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user