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
9 changes: 5 additions & 4 deletions src/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,14 +304,15 @@ module.exports.buckets = function(items, field, agg, aggregations) {
buckets = _.map(buckets, (val, key) => {
return {
key: key,
doc_count: val
doc_count: val,
selected: _.includes(key, agg.filters)
};
})
});

if (agg.sort === 'term') {
buckets = _.orderBy(buckets, ['key'], [agg.order || 'asc']);
buckets = _.orderBy(buckets, ['selected', 'key'], ['desc', agg.order || 'asc']);
} else {
buckets = _.orderBy(buckets, ['doc_count', 'key'], [agg.order || 'desc', 'asc']);
buckets = _.orderBy(buckets, ['selected', 'doc_count', 'key'], ['desc', agg.order || 'desc', 'asc']);
}

return buckets;
Expand Down
20 changes: 20 additions & 0 deletions tests/aggregationsSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,4 +277,24 @@ describe('aggregations', function() {
done();
});

it('returns aggregations with multi array filtering, selected on top', function test(done) {
var movies = require('./fixtures/movies_agg_selected.json')

var result = service.aggregations(movies, {
actors: {
filters: ['Morgan Freeman']
},
tags: {
filters: ['falling into a well']
}
})

assert.equal(result.tags.buckets.length, 5);
assert.equal(result.actors.buckets.length, 10);
assert.notEqual(result.actors.buckets.find(e => e.key === "Morgan Freeman") , undefined);
assert.equal(result.actors.buckets[0].key, "Morgan Freeman");

done();
});

});
142 changes: 142 additions & 0 deletions tests/fixtures/movies_agg_selected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
[
{
"name": "movie1",
"tags": ["a", "b", "c", "d"],
"actors": ["a", "b", "m"]
},
{
"tags": [
"social decay",
"well",
"deception",
"falling down a well",
"falling into a well"
],
"actors": [
"Christian Bale",
"Michael Caine",
"Liam Neeson",
"Katie Holmes",
"Gary Oldman",
"Cillian Murphy",
"Tom Wilkinson",
"Rutger Hauer",
"Ken Watanabe",
"Mark Boone Junior",
"Linus Roache",
"Morgan Freeman",
"Larry Holden",
"Gerard Murphy",
"Colin McFarlane"
],
"name": "Batman Begins"
},
{
"name": "The Dark Knight",
"tags": [
"dc comics",
"moral dilemma",
"psychopath",
"false confession",
"star died before release"
],
"actors": [
"Christian Bale",
"Michael Caine",
"Liam Neeson",
"Katie Holmes",
"Gary Oldman",
"Cillian Murphy",
"Tom Wilkinson",
"Rutger Hauer",
"Ken Watanabe",
"Mark Boone Junior",
"Linus Roache",
"Morgan Freeman",
"Larry Holden",
"Gerard Murphy",
"Colin McFarlane"
]
},
{
"name": "The Shawshank Redemption",
"tags": [
"prison",
"wrongful imprisonment",
"escape from prison",
"prison cell search",
"first person narration"
],
"actors": [
"Tim Robbins",
"Morgan Freeman",
"Bob Gunton",
"William Sadler",
"Clancy Brown",
"Gil Bellows",
"Mark Rolston",
"James Whitmore",
"Jeffrey DeMunn",
"Larry Brandenburg",
"Neil Giuntoli",
"Brian Libby",
"David Proval",
"Joseph Ragno",
"Jude Ciccolella"
]
},
{
"name": "Se7en",
"tags": [
"detective",
"serial killer",
"seven deadly sins",
"police partner",
"human monster"
],
"actors": [
"Morgan Freeman",
"Andrew Kevin Walker",
"Kevin Spacey",
"Daniel Zacapa",
"Brad Pitt",
"Gwyneth Paltrow",
"John Cassini",
"Bob Mack",
"Peter Crombie",
"Reg E. Cathey",
"R. Lee Ermey",
"George Christy",
"Endre Hules",
"Hawthorne James",
"William Davidson"
]
},
{
"tags": [
"female villain",
"mysterious woman",
"suspense",
"written by director",
"woman fights a man"
],
"actors": [
"Christian Bale",
"Gary Oldman",
"Tom Hardy",
"Joseph Gordon-Levitt",
"Anne Hathaway",
"Marion Cotillard",
"Morgan Freeman",
"Michael Caine",
"Matthew Modine",
"Alon Aboutboul",
"Ben Mendelsohn",
"Burn Gorman",
"Daniel Sunjata",
"Aidan Gillen",
"Sam Kennard"
],
"name": "The Dark Knight Rises"
}
]