-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/search filters #11
Changes from all commits
e8c1647
3da7fc8
6878f17
297665c
77a9796
cf92be1
f92c7fb
a69bd97
9c3d877
ec7dd61
85ff7d6
20e838c
5906702
953bd9e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
'use strict'; | ||
|
||
const _ = require('lodash'); | ||
const elasticsearchClient = require('../elasticsearchClient'); | ||
const queryBuilder = require('./query_builder'); | ||
|
||
const mapResults = function (results) { | ||
return _.pluck(results.hits.hits, '_source'); | ||
}; | ||
|
||
class Search { | ||
query(indexName, body) { | ||
return elasticsearchClient.search({ | ||
index: indexName, | ||
body: queryBuilder.build(body) | ||
}) | ||
.then(mapResults); | ||
} | ||
} | ||
|
||
module.exports = Search; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
'use strict'; | ||
|
||
const buildTermFilter = function (filterParams) { | ||
// Can't currenlty be a 1 liner due to https://github.com/nodejs/node/issues/2507 | ||
const term = {[filterParams.field]: filterParams.term}; | ||
return {term}; | ||
}; | ||
|
||
const buildRangeFilter = function (filterParams) { | ||
const range = {[filterParams.field]: {}}; | ||
if (filterParams.range.from) { | ||
range[filterParams.field].gte = filterParams.range.from; | ||
} | ||
if (filterParams.range.to) { | ||
range[filterParams.field].lte = filterParams.range.to; | ||
} | ||
|
||
return {range}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm sure there's a good reason but this is new to me, so curious, why return There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah yeah of course. Gonna take a while to get used to that one. I did spot it elsewhere where it wasn't the only property! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah it's new to me as well |
||
}; | ||
|
||
const buildFilter = function (filterParams) { | ||
if (filterParams.term) { | ||
return buildTermFilter(filterParams); | ||
} | ||
|
||
if (filterParams.range) { | ||
return buildRangeFilter(filterParams); | ||
} | ||
}; | ||
|
||
const buildQuery = function (searchParams) { | ||
const query = {query: {filtered: {}}}; | ||
|
||
if (searchParams.query) { | ||
query.query.filtered.query = {match: {_all: searchParams.query}}; | ||
} | ||
|
||
if (searchParams.filters && searchParams.filters.length) { | ||
query.query.filtered = { | ||
filter: {and: searchParams.filters.map(buildFilter)} | ||
}; | ||
} | ||
|
||
return query; | ||
}; | ||
|
||
module.exports = { | ||
build: buildQuery | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the reason for using esnext?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's the consistency rules from xo for ES6. Tried it with iojs but that wasn't working great because some stuff was behind flags.