Conditional query in elasticsearch

Hi,
I'm using ES 5.5 and having a query dsl with javasript API request like this

client.search({
index: 'demo',
type: 'sample',
body: {
"query": {
"bool" : {
"must": [{
"match": {
"CityName": { query: req.params.city, slop: 100 }
}
}, {
"match": {
"StateName": { query: req.params.state, slop: 100 }
},
{
"match": {
"Code": { query: req.params.code, slop: 100 }
}
}]
}
}
}
})
This query works fine when user gives all three values.But In my case these three parameters are not mandatory.Either user can give one value or more than one value and given fields "must match" the documents.Searching with one or two values doesn't return anything.Can anyone help me solve problem and give me correct query DSL for this?

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.