# Conditional query in elasticsearch

**URL:** <https://discuss.elastic.co/t/conditional-query-in-elasticsearch/235085>\
**Category:** Elasticsearch\
**Created:** [May 31, 2020, 7:19pm UTC](https://discuss.elastic.co/t/conditional-query-in-elasticsearch/235085 "2020-05-31T19:19:53Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![yazhini\_ravi](https://avatars.discourse-cdn.com/v4/letter/y/838e76/32.png) [@yazhini\_ravi](https://discuss.elastic.co/u/yazhini_ravi)\
**Post date:** [May 31, 2020, 7:19pm UTC](https://discuss.elastic.co/t/conditional-query-in-elasticsearch/235085/1 "2020-05-31T19:19:54Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [June 28, 2020, 7:20pm UTC](https://discuss.elastic.co/t/conditional-query-in-elasticsearch/235085/2 "2020-06-28T19:20:12Z")

</div>

Closed on June 28, 2020, 7:20pm UTC

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