Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ Responsible for defining global configuration. Look for full example here - [con
- **`show_facet_stats`** `true` | `false` (Default) to retrieve the min, max, avg, sum rating values from the whole filtered dataset
- **`conjunction`** `true` (Default) stands for an _AND_ query (results have to fit all selected facet-values), `false` for an _OR_ query (results have to fit one of the selected facet-values)
- **`chosen_filters_on_top`** `true` (Default) Filters that have been selected will appear above those not selected, `false` for filters displaying in the order set out by `sort` and `order` regardless of selected status or not
- **`hide_zero_doc_count`** `true` | `false` (Default) Hide filters that have 0 results returned

- **`sortings`** you can configure different sortings like `tags_asc`, `tags_desc` with options and later use it with one key.

Expand Down
4 changes: 3 additions & 1 deletion docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ var itemsjs = require('itemsjs')(data, {
// If you want to retrieve the min, max, avg, sum rating values from the whole filtered dataset
show_facet_stats: true,
// If you don't want selected filters to be positioned at the top of the filter list
chosen_filters_on_top: false
chosen_filters_on_top: false,
// If you don't want to show filters with no results returned
hide_zero_doc_count: true
}
},
searchableFields: ['name', 'tags'],
Expand Down
11 changes: 10 additions & 1 deletion src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ const getBuckets = function(data, input, aggregations) {
let title;
let show_facet_stats;
let chosen_filters_on_top;
let hide_zero_doc_count;

if (aggregations[k]) {
order = aggregations[k].order;
Expand All @@ -341,6 +342,7 @@ const getBuckets = function(data, input, aggregations) {
title = aggregations[k].title;
show_facet_stats = aggregations[k].show_facet_stats || false;
chosen_filters_on_top = aggregations[k].chosen_filters_on_top !== false;
hide_zero_doc_count = aggregations[k].hide_zero_doc_count || false;
}

let buckets = _.chain(v)
Expand All @@ -352,12 +354,19 @@ const getBuckets = function(data, input, aggregations) {
filters = input.filters[k];
}

let doc_count = v2[1].array().length;

if (hide_zero_doc_count && doc_count === 0) {
return;
}

return {
key: v2[0],
doc_count: v2[1].array().length,
doc_count: doc_count,
selected: filters.indexOf(v2[0]) !== -1
};
})
.compact()
.value();

let iteratees;
Expand Down
20 changes: 20 additions & 0 deletions tests/facetSortingSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,5 +193,25 @@ describe('facet sorting', function() {

done();
});

it('excludes filters with zero doc_count if hide_zero_doc_count is true', function test(done) {

const result = require('./../index')(items, {
aggregations: {
genres: {
hide_zero_doc_count: true
}
}
}).aggregation({
name: 'genres',
filters: {
genres: ['Western']
}
});

assert.deepEqual(result.data.buckets.map(v => v.key), ['Western']);

done();
});
});