From 1140658c3c37e9041ee633c883de4efb7efadd74 Mon Sep 17 00:00:00 2001 From: Jacob Mulquin Date: Sat, 30 Jul 2022 06:39:29 +1000 Subject: [PATCH 1/2] Hide empty filters through configuration --- README.md | 1 + docs/configuration.md | 4 +++- src/helpers.js | 11 ++++++++++- tests/facetSortingSpec.js | 20 ++++++++++++++++++++ 4 files changed, 34 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 897faa2..1bba78d 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/docs/configuration.md b/docs/configuration.md index 6b9f177..9a0a561 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -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'], diff --git a/src/helpers.js b/src/helpers.js index c728f95..1c7d2cc 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -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; @@ -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) @@ -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(this) .value(); let iteratees; diff --git a/tests/facetSortingSpec.js b/tests/facetSortingSpec.js index 0fd86f5..8ad2973 100644 --- a/tests/facetSortingSpec.js +++ b/tests/facetSortingSpec.js @@ -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(); + }); }); From f76be5705bbe41c1460f6263304480e2ff0b8cb0 Mon Sep 17 00:00:00 2001 From: Jacob Mulquin Date: Sat, 30 Jul 2022 08:11:53 +1000 Subject: [PATCH 2/2] Remove this from compact() call --- src/helpers.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/helpers.js b/src/helpers.js index 1c7d2cc..361175e 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -366,7 +366,7 @@ const getBuckets = function(data, input, aggregations) { selected: filters.indexOf(v2[0]) !== -1 }; }) - .compact(this) + .compact() .value(); let iteratees;