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