[#62,faceting][s]: basic faceting fully functional on ES.
This commit is contained in:
parent
6ba8c82408
commit
80165659b1
52
src/model.js
52
src/model.js
@ -27,7 +27,6 @@ my.Dataset = Backbone.Model.extend({
|
||||
this.docCount = null;
|
||||
this.queryState = new my.Query();
|
||||
this.queryState.bind('change', this.query);
|
||||
this.facets.bind('all', this.query);
|
||||
},
|
||||
|
||||
// ### query
|
||||
@ -58,9 +57,11 @@ my.Dataset = Backbone.Model.extend({
|
||||
});
|
||||
self.currentDocuments.reset(docs);
|
||||
if (queryResult.facets) {
|
||||
_.each(queryResult.facets, function(facetResult, facetId) {
|
||||
self.facets.get(facetId).set({result: facetResult});
|
||||
var facets = _.map(queryResult.facets, function(facetResult, facetId) {
|
||||
facetResult.id = facetId;
|
||||
return new my.Facet(facetResult);
|
||||
});
|
||||
self.facets.reset(facets);
|
||||
}
|
||||
self.trigger('query:done');
|
||||
dfd.resolve(self.currentDocuments);
|
||||
@ -77,9 +78,6 @@ my.Dataset = Backbone.Model.extend({
|
||||
this.queryState.set(newQueryObj);
|
||||
}
|
||||
var out = this.queryState.toJSON();
|
||||
_.each(this.facets.toJSON(), function(facet) {
|
||||
out.facets[facet.id] = facet.query;
|
||||
});
|
||||
return out;
|
||||
},
|
||||
|
||||
@ -144,36 +142,32 @@ my.Query = Backbone.Model.extend({
|
||||
// Set (update or add) a terms filter
|
||||
// http://www.elasticsearch.org/guide/reference/query-dsl/terms-filter.html
|
||||
setFilter: function(fieldId, values) {
|
||||
}
|
||||
});
|
||||
|
||||
my.Facet = Backbone.Model.extend({
|
||||
defaults: {
|
||||
query: null,
|
||||
result: null
|
||||
}
|
||||
});
|
||||
|
||||
my.FacetList = Backbone.Collection.extend({
|
||||
model: my.Facet,
|
||||
},
|
||||
addFacet: 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 (this.include(fieldId)) {
|
||||
if (_.contains(_.keys(facets), fieldId)) {
|
||||
return;
|
||||
}
|
||||
// TODO: utilize field info to determine facet type ??
|
||||
var facet = new my.Facet({
|
||||
id: fieldId,
|
||||
query: {
|
||||
terms: {
|
||||
field: fieldId
|
||||
}
|
||||
}
|
||||
});
|
||||
this.add(facet);
|
||||
facets[fieldId] = {
|
||||
terms: { field: fieldId }
|
||||
};
|
||||
this.set({facets: facets});
|
||||
// for some reason this does not trigger automatically ...
|
||||
this.trigger('change', this);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
// ## A Facet (Result)
|
||||
my.Facet = Backbone.Model.extend({
|
||||
});
|
||||
|
||||
// ## A Collection/List of Facets
|
||||
my.FacetList = Backbone.Collection.extend({
|
||||
model: my.Facet
|
||||
});
|
||||
|
||||
// ## Backend registry
|
||||
//
|
||||
// Backends will register themselves by id into this registry
|
||||
|
||||
@ -294,9 +294,9 @@ my.FacetQueryEditor = Backbone.View.extend({
|
||||
{{#facets}} \
|
||||
<a class="btn js-facet-show-toggle" data-facet="{{id}}"><i class="icon-plus"></i> {{id}} {{label}}</a> \
|
||||
<ul class="facet-items" data-facet="{{id}}" style="display: none;"> \
|
||||
{{#result}} \
|
||||
{{#terms}} \
|
||||
<li>{{term}} ({{count}}) <input type="checkbox" class="facet-choice" data-facet="{{label}}" value="{{term}}" /></li> \
|
||||
{{/result}} \
|
||||
{{/terms}} \
|
||||
</ul> \
|
||||
{{/facets}} \
|
||||
</div> \
|
||||
@ -324,7 +324,7 @@ my.FacetQueryEditor = Backbone.View.extend({
|
||||
onAddFacet: function(e) {
|
||||
e.preventDefault();
|
||||
var fieldId = $(e.target).attr('href').slice(1);
|
||||
this.model.facets.addFacet(fieldId);
|
||||
this.model.queryState.addFacet(fieldId);
|
||||
},
|
||||
onFacetShowToggle: function(e) {
|
||||
e.preventDefault();
|
||||
|
||||
@ -57,26 +57,16 @@ test('Dataset _prepareQuery', function () {
|
||||
var out = dataset._prepareQuery();
|
||||
var exp = new recline.Model.Query().toJSON();
|
||||
deepEqual(out, exp);
|
||||
|
||||
var fieldId = 'abc';
|
||||
dataset.facets.addFacet(fieldId);
|
||||
|
||||
var out = dataset._prepareQuery();
|
||||
var exp = new recline.Model.Query().toJSON();
|
||||
exp.facets = {};
|
||||
exp.facets[fieldId] = { terms: {field: fieldId} };
|
||||
deepEqual(out, exp);
|
||||
});
|
||||
|
||||
|
||||
// =================================
|
||||
// Facet
|
||||
// Query
|
||||
|
||||
test('Facet', function () {
|
||||
var facets = new recline.Model.FacetList();
|
||||
facets.addFacet('xyz');
|
||||
equal(1, facets.length);
|
||||
deepEqual({terms: {field: 'xyz'}}, facets.get('xyz').get('query'));
|
||||
test('Query', function () {
|
||||
var query = new recline.Model.Query();
|
||||
query.addFacet('xyz');
|
||||
deepEqual({terms: {field: 'xyz'}}, query.get('facets')['xyz']);
|
||||
});
|
||||
|
||||
})(this.jQuery);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user