I've been wondering how to go about checking if a search parameter is not empty before actually adding that term filter to a filtered query. From my limited experience with elasticsearch, it seems like I have to build my exact query ahead of time.
If I were using Node / Mongo, I know that I could do something along the lines of
var searchTerms = {};
if(req.body.country){
searchTerm['user.country'] = req.body.country;
}
if(req.body.company){
searchTerm['user.company'] = req.body.company;
}
if(req.body.salary){
searchTerm['user.salary'] = req.body.salary;
}
Users.find(searchTerm)
.exec(function(err, users) {
if(!err){
res.send(200, users);
}
else{
res.send(200, err);
}
});
And in angular I've built a query for elasticsearch that works correctly as long as all parameters are filled in. I guess my question is how to conditionally and dynamically build filtered query based on parameters that have been filled in by the user.
i.e. User A fills out country and company but leaves out salary. therefore, salary wouldn't be included as a term filter.
client.search({
index: 'firebase',
type: 'user',
body: {
"query": {
"filtered" : {
"query" : {
"match_all" : {}
},
"filter": {
"and": [
{
"term": { "country" : searchParameters.country }
},
{
"term": { "company" : searchParameters.company }
},
{
"term": { "salary" : searchParameters.salary }
}
]
}
}
}
Thank you all in advance!