Skip to content

Commit

Permalink
fix(getRefinements): hierarchical facets
Browse files Browse the repository at this point in the history
Bug fix of not retrieving the count on a hierarchical facet

It now only returns the last value (will be needed by
currentRefinedValues).

Incidentally, getRefinements now also returns the type of facet
for convenience.
  • Loading branch information
Jerska committed Nov 30, 2015
1 parent fc3e15f commit fe0fc5d
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 62 deletions.
92 changes: 47 additions & 45 deletions src/lib/__tests__/utils-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,11 @@ describe('getRefinements', function() {
hierarchicalFacets: [{
name: 'hierarchicalFacet1',
attributes: ['hierarchicalFacet1.lvl0', 'hierarchicalFacet1.lvl1'],
separator: '>'
separator: ' > '
}, {
name: 'hierarchicalFacet2',
attributes: ['hierarchicalFacet2.lvl0', 'hierarchicalFacet2.lvl1'],
separator: '>'
separator: ' > '
}]
});
results = {};
Expand All @@ -150,24 +150,24 @@ describe('getRefinements', function() {
it('should retrieve one tag', function() {
helper.addTag('tag1');
const expected = [
{attributeName: '_tags', name: 'tag1'}
{type: 'tag', attributeName: '_tags', name: 'tag1'}
];
expect(utils.getRefinements(results, helper.state)).toInclude(expected[0]);
});

it('should retrieve multiple tags', function() {
helper.addTag('tag1').addTag('tag2');
const expected = [
{attributeName: '_tags', name: 'tag1'},
{attributeName: '_tags', name: 'tag2'}
{type: 'tag', attributeName: '_tags', name: 'tag1'},
{type: 'tag', attributeName: '_tags', name: 'tag2'}
];
expect(utils.getRefinements(results, helper.state)).toInclude(expected[0]);
});

it('should retrieve one facetRefinement', function() {
helper.toggleRefinement('facet1', 'facet1val1');
const expected = [
{attributeName: 'facet1', name: 'facet1val1'}
{type: 'facet', attributeName: 'facet1', name: 'facet1val1'}
];
expect(utils.getRefinements(results, helper.state)).toInclude(expected[0]);
});
Expand All @@ -177,8 +177,8 @@ describe('getRefinements', function() {
.toggleRefinement('facet1', 'facet1val1')
.toggleRefinement('facet1', 'facet1val2');
const expected = [
{attributeName: 'facet1', name: 'facet1val1'},
{attributeName: 'facet1', name: 'facet1val2'}
{type: 'facet', attributeName: 'facet1', name: 'facet1val1'},
{type: 'facet', attributeName: 'facet1', name: 'facet1val2'}
];
expect(utils.getRefinements(results, helper.state)).toInclude(expected[0]);
expect(utils.getRefinements(results, helper.state)).toInclude(expected[1]);
Expand All @@ -190,9 +190,9 @@ describe('getRefinements', function() {
.toggleRefinement('facet1', 'facet1val2')
.toggleRefinement('facet2', 'facet2val1');
const expected = [
{attributeName: 'facet1', name: 'facet1val1'},
{attributeName: 'facet1', name: 'facet1val2'},
{attributeName: 'facet2', name: 'facet2val1'}
{type: 'facet', attributeName: 'facet1', name: 'facet1val1'},
{type: 'facet', attributeName: 'facet1', name: 'facet1val2'},
{type: 'facet', attributeName: 'facet2', name: 'facet2val1'}
];
expect(utils.getRefinements(results, helper.state)).toInclude(expected[0]);
expect(utils.getRefinements(results, helper.state)).toInclude(expected[1]);
Expand All @@ -210,7 +210,7 @@ describe('getRefinements', function() {
}]
};
const expected = [
{attributeName: 'facet1', name: 'facet1val1', count: 4}
{type: 'facet', attributeName: 'facet1', name: 'facet1val1', count: 4}
];
expect(utils.getRefinements(results, helper.state)).toInclude(expected[0]);
});
Expand All @@ -224,15 +224,15 @@ describe('getRefinements', function() {
}]
};
const expected = [
{attributeName: 'facet1', name: 'facet1val1', exhaustive: true}
{type: 'facet', attributeName: 'facet1', name: 'facet1val1', exhaustive: true}
];
expect(utils.getRefinements(results, helper.state)).toInclude(expected[0]);
});

it('should retrieve one facetExclude', function() {
helper.toggleExclude('facet1', 'facet1exclude1');
const expected = [
{attributeName: 'facet1', name: 'facet1exclude1', exclude: true}
{type: 'exclude', attributeName: 'facet1', name: 'facet1exclude1', exclude: true}
];
expect(utils.getRefinements(results, helper.state)).toInclude(expected[0]);
});
Expand All @@ -242,8 +242,8 @@ describe('getRefinements', function() {
.toggleExclude('facet1', 'facet1exclude1')
.toggleExclude('facet1', 'facet1exclude2');
const expected = [
{attributeName: 'facet1', name: 'facet1exclude1', exclude: true},
{attributeName: 'facet1', name: 'facet1exclude2', exclude: true}
{type: 'exclude', attributeName: 'facet1', name: 'facet1exclude1', exclude: true},
{type: 'exclude', attributeName: 'facet1', name: 'facet1exclude2', exclude: true}
];
expect(utils.getRefinements(results, helper.state)).toInclude(expected[0]);
expect(utils.getRefinements(results, helper.state)).toInclude(expected[1]);
Expand All @@ -255,9 +255,9 @@ describe('getRefinements', function() {
.toggleExclude('facet1', 'facet1exclude2')
.toggleExclude('facet2', 'facet2exclude1');
const expected = [
{attributeName: 'facet1', name: 'facet1exclude1', exclude: true},
{attributeName: 'facet1', name: 'facet1exclude2', exclude: true},
{attributeName: 'facet2', name: 'facet2exclude1', exclude: true}
{type: 'exclude', attributeName: 'facet1', name: 'facet1exclude1', exclude: true},
{type: 'exclude', attributeName: 'facet1', name: 'facet1exclude2', exclude: true},
{type: 'exclude', attributeName: 'facet2', name: 'facet2exclude1', exclude: true}
];
expect(utils.getRefinements(results, helper.state)).toInclude(expected[0]);
expect(utils.getRefinements(results, helper.state)).toInclude(expected[1]);
Expand All @@ -267,7 +267,7 @@ describe('getRefinements', function() {
it('should retrieve one disjunctiveFacetRefinement', function() {
helper.addDisjunctiveFacetRefinement('disjunctiveFacet1', 'disjunctiveFacet1val1');
const expected = [
{attributeName: 'disjunctiveFacet1', name: 'disjunctiveFacet1val1'}
{type: 'disjunctive', attributeName: 'disjunctiveFacet1', name: 'disjunctiveFacet1val1'}
];
expect(utils.getRefinements(results, helper.state)).toInclude(expected[0]);
});
Expand All @@ -277,8 +277,8 @@ describe('getRefinements', function() {
.addDisjunctiveFacetRefinement('disjunctiveFacet1', 'disjunctiveFacet1val1')
.addDisjunctiveFacetRefinement('disjunctiveFacet1', 'disjunctiveFacet1val2');
const expected = [
{attributeName: 'disjunctiveFacet1', name: 'disjunctiveFacet1val1'},
{attributeName: 'disjunctiveFacet1', name: 'disjunctiveFacet1val2'}
{type: 'disjunctive', attributeName: 'disjunctiveFacet1', name: 'disjunctiveFacet1val1'},
{type: 'disjunctive', attributeName: 'disjunctiveFacet1', name: 'disjunctiveFacet1val2'}
];
expect(utils.getRefinements(results, helper.state)).toInclude(expected[0]);
expect(utils.getRefinements(results, helper.state)).toInclude(expected[1]);
Expand All @@ -290,9 +290,9 @@ describe('getRefinements', function() {
.toggleRefinement('disjunctiveFacet1', 'disjunctiveFacet1val2')
.toggleRefinement('disjunctiveFacet2', 'disjunctiveFacet2val1');
const expected = [
{attributeName: 'disjunctiveFacet1', name: 'disjunctiveFacet1val1'},
{attributeName: 'disjunctiveFacet1', name: 'disjunctiveFacet1val2'},
{attributeName: 'disjunctiveFacet2', name: 'disjunctiveFacet2val1'}
{type: 'disjunctive', attributeName: 'disjunctiveFacet1', name: 'disjunctiveFacet1val1'},
{type: 'disjunctive', attributeName: 'disjunctiveFacet1', name: 'disjunctiveFacet1val2'},
{type: 'disjunctive', attributeName: 'disjunctiveFacet2', name: 'disjunctiveFacet2val1'}
];
expect(utils.getRefinements(results, helper.state)).toInclude(expected[0]);
expect(utils.getRefinements(results, helper.state)).toInclude(expected[1]);
Expand All @@ -310,7 +310,7 @@ describe('getRefinements', function() {
}]
};
const expected = [
{attributeName: 'disjunctiveFacet1', name: 'disjunctiveFacet1val1', count: 4}
{type: 'disjunctive', attributeName: 'disjunctiveFacet1', name: 'disjunctiveFacet1val1', count: 4}
];
expect(utils.getRefinements(results, helper.state)).toInclude(expected[0]);
});
Expand All @@ -324,15 +324,15 @@ describe('getRefinements', function() {
}]
};
const expected = [
{attributeName: 'disjunctiveFacet1', name: 'disjunctiveFacet1val1', exhaustive: true}
{type: 'disjunctive', attributeName: 'disjunctiveFacet1', name: 'disjunctiveFacet1val1', exhaustive: true}
];
expect(utils.getRefinements(results, helper.state)).toInclude(expected[0]);
});

it('should retrieve one hierarchicalFacetRefinement', function() {
helper.toggleRefinement('hierarchicalFacet1', 'hierarchicalFacet1lvl0val1');
const expected = [
{attributeName: 'hierarchicalFacet1', name: 'hierarchicalFacet1lvl0val1'}
{type: 'hierarchical', attributeName: 'hierarchicalFacet1', name: 'hierarchicalFacet1lvl0val1'}
];
expect(utils.getRefinements(results, helper.state)).toInclude(expected[0]);
});
Expand All @@ -342,8 +342,8 @@ describe('getRefinements', function() {
.toggleRefinement('hierarchicalFacet1', 'hierarchicalFacet1lvl0val1')
.toggleRefinement('hierarchicalFacet2', 'hierarchicalFacet2lvl0val1');
const expected = [
{attributeName: 'hierarchicalFacet1', name: 'hierarchicalFacet1lvl0val1'},
{attributeName: 'hierarchicalFacet2', name: 'hierarchicalFacet2lvl0val1'}
{type: 'hierarchical', attributeName: 'hierarchicalFacet1', name: 'hierarchicalFacet1lvl0val1'},
{type: 'hierarchical', attributeName: 'hierarchicalFacet2', name: 'hierarchicalFacet2lvl0val1'}
];
expect(utils.getRefinements(results, helper.state)).toInclude(expected[0]);
expect(utils.getRefinements(results, helper.state)).toInclude(expected[1]);
Expand All @@ -354,8 +354,8 @@ describe('getRefinements', function() {
.toggleRefinement('hierarchicalFacet1', 'hierarchicalFacet1lvl0val1')
.toggleRefinement('hierarchicalFacet2', 'hierarchicalFacet2lvl0val1 > lvl1val1');
const expected = [
{attributeName: 'hierarchicalFacet1', name: 'hierarchicalFacet1lvl0val1'},
{attributeName: 'hierarchicalFacet2', name: 'hierarchicalFacet2lvl0val1 > lvl1val1'}
{type: 'hierarchical', attributeName: 'hierarchicalFacet1', name: 'hierarchicalFacet1lvl0val1'},
{type: 'hierarchical', attributeName: 'hierarchicalFacet2', name: 'lvl1val1'}
];
expect(utils.getRefinements(results, helper.state)).toInclude(expected[0]);
expect(utils.getRefinements(results, helper.state)).toInclude(expected[1]);
Expand All @@ -367,12 +367,12 @@ describe('getRefinements', function() {
hierarchicalFacets: [{
name: 'hierarchicalFacet1',
data: {
hierarchicalFacet1val1: 4
hierarchicalFacet1val1: {name: 'hierarchicalFacet1val1', count: 4}
}
}]
};
const expected = [
{attributeName: 'hierarchicalFacet1', name: 'hierarchicalFacet1val1', count: 4}
{type: 'hierarchical', attributeName: 'hierarchicalFacet1', name: 'hierarchicalFacet1val1', count: 4}
];
expect(utils.getRefinements(results, helper.state)).toInclude(expected[0]);
});
Expand All @@ -382,27 +382,29 @@ describe('getRefinements', function() {
results = {
hierarchicalFacets: [{
name: 'hierarchicalFacet1',
exhaustive: true
data: [
{name: 'hierarchicalFacet1val1', exhaustive: true}
]
}]
};
const expected = [
{attributeName: 'hierarchicalFacet1', name: 'hierarchicalFacet1val1', exhaustive: true}
{type: 'hierarchical', attributeName: 'hierarchicalFacet1', name: 'hierarchicalFacet1val1', exhaustive: true}
];
expect(utils.getRefinements(results, helper.state)).toInclude(expected[0]);
});

it('should retrieve a numericRefinement on one facet', function() {
helper.addNumericRefinement('numericFacet1', '>', '1');
const expected = [
{attributeName: 'numericFacet1', operator: '>', name: '1'}
{type: 'numeric', attributeName: 'numericFacet1', operator: '>', name: '1'}
];
expect(utils.getRefinements(results, helper.state)).toInclude(expected[0]);
});

it('should retrieve a numericRefinement on one disjunctive facet', function() {
helper.addNumericRefinement('numericDisjunctiveFacet1', '>', '1');
const expected = [
{attributeName: 'numericDisjunctiveFacet1', operator: '>', name: '1'}
{type: 'numeric', attributeName: 'numericDisjunctiveFacet1', operator: '>', name: '1'}
];
expect(utils.getRefinements(results, helper.state)).toInclude(expected[0]);
});
Expand All @@ -412,8 +414,8 @@ describe('getRefinements', function() {
.addNumericRefinement('numericFacet1', '>', '1')
.addNumericRefinement('numericFacet1', '>', '2');
const expected = [
{attributeName: 'numericFacet1', operator: '>', name: '1'},
{attributeName: 'numericFacet1', operator: '>', name: '2'}
{type: 'numeric', attributeName: 'numericFacet1', operator: '>', name: '1'},
{type: 'numeric', attributeName: 'numericFacet1', operator: '>', name: '2'}
];
expect(utils.getRefinements(results, helper.state)).toInclude(expected[0]);
expect(utils.getRefinements(results, helper.state)).toInclude(expected[1]);
Expand All @@ -427,11 +429,11 @@ describe('getRefinements', function() {
.addNumericRefinement('numericDisjunctiveFacet1', '>', '1')
.addNumericRefinement('numericDisjunctiveFacet1', '>', '2');
const expected = [
{attributeName: 'numericFacet1', operator: '>', name: '1'},
{attributeName: 'numericFacet1', operator: '>', name: '2'},
{attributeName: 'numericFacet1', operator: '<=', name: '3'},
{attributeName: 'numericDisjunctiveFacet1', operator: '>', name: '1'},
{attributeName: 'numericDisjunctiveFacet1', operator: '>', name: '2'}
{type: 'numeric', attributeName: 'numericFacet1', operator: '>', name: '1'},
{type: 'numeric', attributeName: 'numericFacet1', operator: '>', name: '2'},
{type: 'numeric', attributeName: 'numericFacet1', operator: '<=', name: '3'},
{type: 'numeric', attributeName: 'numericDisjunctiveFacet1', operator: '>', name: '1'},
{type: 'numeric', attributeName: 'numericDisjunctiveFacet1', operator: '>', name: '2'}
];
expect(utils.getRefinements(results, helper.state)).toInclude(expected[0]);
expect(utils.getRefinements(results, helper.state)).toInclude(expected[1]);
Expand Down
43 changes: 26 additions & 17 deletions src/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,18 +123,27 @@ function prepareTemplates(defaultTemplates, templates) {
}, {templates: {}, useCustomCompileOptions: {}});
}

function getRefinement(attributeName, name, resultsFacets) {
let res = {attributeName, name};
let facet = find(resultsFacets, (_facet) => { return _facet.name === attributeName; });
if (facet !== undefined) {
const count = get(facet, 'data.' + name);
const exhaustive = get(facet, 'exhaustive');
if (count !== undefined) {
res.count = count;
}
if (exhaustive !== undefined) {
res.exhaustive = exhaustive;
function getRefinement(state, type, attributeName, name, resultsFacets) {
let res = {type, attributeName, name};
let facet = find(resultsFacets, {name: attributeName});
let count;
if (type === 'hierarchical') {
let facetDeclaration = state.getHierarchicalFacetByName(attributeName);
let splitted = name.split(facetDeclaration.separator);
res.name = splitted[splitted.length - 1];
for (let i = 0; facet !== undefined && i < splitted.length; ++i) {
facet = find(facet.data, {name: splitted[i]});
}
count = get(facet, 'count');
} else {
count = get(facet, 'data["' + res.name + '"]');
}
const exhaustive = get(facet, 'exhaustive');
if (count !== undefined) {
res.count = count;
}
if (exhaustive !== undefined) {
res.exhaustive = exhaustive;
}
return res;
}
Expand All @@ -144,38 +153,38 @@ function getRefinements(results, state) {

forEach(state.facetsRefinements, (refinements, attributeName) => {
forEach(refinements, (name) => {
res.push(getRefinement(attributeName, name, results.facets));
res.push(getRefinement(state, 'facet', attributeName, name, results.facets));
});
});

forEach(state.facetsExcludes, (refinements, attributeName) => {
forEach(refinements, (name) => {
res.push({attributeName, name, exclude: true});
res.push({type: 'exclude', attributeName, name, exclude: true});
});
});

forEach(state.disjunctiveFacetsRefinements, (refinements, attributeName) => {
forEach(refinements, (name) => {
res.push(getRefinement(attributeName, name, results.disjunctiveFacets));
res.push(getRefinement(state, 'disjunctive', attributeName, name, results.disjunctiveFacets));
});
});

forEach(state.hierarchicalFacetsRefinements, (refinements, attributeName) => {
forEach(refinements, (name) => {
res.push(getRefinement(attributeName, name, results.hierarchicalFacets));
res.push(getRefinement(state, 'hierarchical', attributeName, name, results.hierarchicalFacets));
});
});

forEach(state.numericRefinements, (operators, attributeName) => {
forEach(operators, (values, operator) => {
forEach(values, (name) => {
res.push({attributeName, name, operator});
res.push({type: 'numeric', attributeName, name, operator});
});
});
});

forEach(state.tagRefinements, (name) => {
res.push({attributeName: '_tags', name});
res.push({type: 'tag', attributeName: '_tags', name});
});

return res;
Expand Down

0 comments on commit fe0fc5d

Please sign in to comment.