# Complex Dsl Queries based on client side parameters

**URL:** <https://discuss.elastic.co/t/complex-dsl-queries-based-on-client-side-parameters/26990>\
**Category:** Elasticsearch\
**Created:** [August 6, 2015, 4:47pm UTC](https://discuss.elastic.co/t/complex-dsl-queries-based-on-client-side-parameters/26990 "2015-08-06T16:47:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ankit\_Agarwal](https://avatars.discourse-cdn.com/v4/letter/a/3e96dc/32.png) [@Ankit\_Agarwal](https://discuss.elastic.co/u/Ankit_Agarwal)\
**Post date:** [August 6, 2015, 4:47pm UTC](https://discuss.elastic.co/t/complex-dsl-queries-based-on-client-side-parameters/26990/1 "2015-08-06T16:47:03Z")

</div>

The logs are of this format:

> **17/03/15-06:29:30 31609 453749 545959 1 4 [http://www.somesite.com/index.html](http://www.somesite.com/index.html) - 0**  
> {timestamp} {process\_id} {child\_id} {device\_id} {blocked\_id} {filter\_id} {url} {log\_message}

I have an external ruby client which can query on the following fields:

- TimeStamp (Start Date to End Date)  
Child Id  
Device Id  
Blocked Id

**The possible combination of parameters passed to query the ES can be:**

> Search for:  
> Child Id alone  
> Child Id + Device Id  
> Child Id + Device Id + Blocked Id  
> Device Id alone  
> Device Id + Blocked Id  
> Start Date to End Date  
> Start Date to End Date + Blocked Id  
> Start Date to End Date + Child Id  
> Start Date to End Date + Child Id + Blocked Id  
> Start Date to End Date + Device Id  
> Start Date to End Date + Device Id + Blocked Id  
> Start Date to End Date + Device Id + Child Id  
> Start Date to End Date + Device Id + Child Id + Blocked

Since I do not know what all parameters will be sent from the client side, I have to handle all the possibilities using **if-else** conditions. Each **If** condition checks for the presence of a parameter and forms the **DSL Query accordingly**.  
I am forming too many conditional statements and forming dsl queries for each. I tried a lot to implement combination queries, range and filter queries. But some or the other condition is violated.

Need help in forming DSL query such that I can cover all possible parameters that the client can send with fewer conditions as possible.  
I know there must be a way to do this. please comment if I am not clear with my question.

Thanks in advance.

I was trying these type of condition and queries:

> if params['child\_id'].  
> q = { match: { child\_id: params['child\_id'] }}  
> if params['device\_id']  
> q = { match: { device\_id: params['device\_id'] }}  
> if params['child\_id'] and params['device\_id']  
> q = { bool: {  
> must: [  
> { match: { child\_id: params['child\_id'] }},  
> { match: { device\_id: params['device\_id'] }}  
> ]  
> }  
> }  
> end  
> if params['start\_time'] and params['end\_time']  
> start\_time = Time.parse(params['start\_time']).iso8601  
> end\_time = Time.parse(params['end\_time']).iso8601  
> q = {  
> filtered: {  
> filter: {  
> range: {  
> "@timestamp" =\> {  
> "gt" =\> start\_time,  
> "lte" =\> end\_time  
> }  
> }  
> }  
> }  
> }  
> end  
> end  
> if !params['child\_id'] and !params['device\_id'] and !params['start'] and !params['end']  
> q = {match\_all: {} }  
> end

**NOTE:** Many other conditions to check the incoming parameters and Queries are not shown. These are just for reference.

---

<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:** [July 5, 2017, 11:57pm UTC](https://discuss.elastic.co/t/complex-dsl-queries-based-on-client-side-parameters/26990/2 "2017-07-05T23:57:05Z")

</div>

Closed on July 5, 2017, 11:57pm UTC


