Skip to content

Commit

Permalink
[#66,model][s]: addTermFacet on Query object.
Browse files Browse the repository at this point in the history
* Also fix a major bug (that was causing weird test failures) re setting defaults (you cannot set defaults with objects).
  • Loading branch information
rufuspollock committed Apr 2, 2012
1 parent 84b9c9f commit 1eb7df9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 15 deletions.
45 changes: 30 additions & 15 deletions src/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,27 @@ my.FieldList = Backbone.Collection.extend({

// ## A Query object storing Dataset Query state
my.Query = Backbone.Model.extend({
defaults: {
size: 100
, from: 0
, facets: {}
defaults: function() {
return {
size: 100
, from: 0
, facets: {}
// http://www.elasticsearch.org/guide/reference/query-dsl/and-filter.html
// , filter: {}
// list of simple filters which will be add to 'add' filter of filter
, filters: []
}
},
// Set (update or add) a terms filter
// http://www.elasticsearch.org/guide/reference/query-dsl/terms-filter.html
setFilter: function(fieldId, values) {
addTermFilter: function(fieldId, value) {
var filters = this.get('filters');
var filter = { term: {} };
filter.term[fieldId] = value;
filters.push(filter);
this.set({filters: filters});
// change does not seem to be triggered ...
this.trigger('change');
},
addFacet: function(fieldId) {
var facets = this.get('facets');
Expand All @@ -157,16 +170,18 @@ my.Query = Backbone.Model.extend({

// ## A Facet (Result)
my.Facet = Backbone.Model.extend({
defaults: {
_type: 'terms',
// total number of tokens in the facet
total: 0,
// number of facet values not included in the returned facets
other: 0,
// number of documents which have no value for the field
missing: 0,
// term object ({term: , count: ...})
terms: []
defaults: function() {
return {
_type: 'terms',
// total number of tokens in the facet
total: 0,
// number of facet values not included in the returned facets
other: 0,
// number of documents which have no value for the field
missing: 0,
// term object ({term: , count: ...})
terms: []
}
}
});

Expand Down
6 changes: 6 additions & 0 deletions test/model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,10 @@ test('Query', function () {
deepEqual({terms: {field: 'xyz'}}, query.get('facets')['xyz']);
});

test('Query.addFilter', function () {
var query = new recline.Model.Query();
query.addTermFilter('xyz', 'this-value');
deepEqual({term: {xyz: 'this-value'}}, query.get('filters')[0]);
});

})(this.jQuery);

0 comments on commit 1eb7df9

Please sign in to comment.