# If else condition in ES query

**URL:** <https://discuss.elastic.co/t/if-else-condition-in-es-query/271319>\
**Category:** Kibana\
**Created:** [April 27, 2021, 6:33am UTC](https://discuss.elastic.co/t/if-else-condition-in-es-query/271319 "2021-04-27T06:33:12Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pallavibhushan](https://avatars.discourse-cdn.com/v4/letter/p/13edae/32.png) [@Pallavibhushan](https://discuss.elastic.co/u/Pallavibhushan)\
**Post date:** [April 27, 2021, 6:33am UTC](https://discuss.elastic.co/t/if-else-condition-in-es-query/271319/1 "2021-04-27T06:33:12Z")

</div>

Hi,  
I am trying to write a query for below data:  
sample data:  
{  
"\_index" : "target\_data",  
"\_type" : "\_doc",  
"\_id" : "Training\_2020",  
"\_score" : 1.0,  
"\_source" : {  
"year" : "2020",  
"date\_epoch" : 1577817000000,  
"field1" : "Training 2",  
"data\_target" : "90",  
"@timestamp" : "2020-10-05T12:25:02.021Z",  
"document\_id" : "Training 2\_Training\_2020"  
}  
},  
{  
"\_index" : "target\_data",  
"\_type" : "\_doc",  
"\_id" : "Training\_2020",  
"\_score" : 1.0,  
"\_source" : {  
"year" : "2020",  
"Org\_path" : "ABC/XYZ",  
"date\_epoch" : 1577817000000,  
"field1" : "Training 2",  
"data\_target" : "95",  
"@timestamp" : "2020-10-05T12:25:02.021Z",  
"document\_id" : "Training 2\_Training\_2020"  
}  
},  
I need--\> if Org\_Path == ABC/XYZ then return data\_target as 95  
else return data\_target as 90

How to write single query and use condition.

Please help me with this

Thanks in advance

---

<div class="post-metadata">

**Author:** ![matw](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/matw/32/13913_2.png) [@matw](https://discuss.elastic.co/u/matw)\
**Post date:** [April 27, 2021, 7:30am UTC](https://discuss.elastic.co/t/if-else-condition-in-es-query/271319/2 "2021-04-27T07:30:29Z")

</div>

Hi

In which part of Kibana do you need such a query? should data\_target be calculated or is it already in `_source`?  
Thx,  
Matthias

---

<div class="post-metadata">

**Author:** ![Pallavibhushan](https://avatars.discourse-cdn.com/v4/letter/p/13edae/32.png) [@Pallavibhushan](https://discuss.elastic.co/u/Pallavibhushan)\
**Post date:** [April 27, 2021, 7:47am UTC](https://discuss.elastic.co/t/if-else-condition-in-es-query/271319/3 "2021-04-27T07:47:23Z")

</div>

Data\_target field is already available. just need to get it basis on Org\_path field  
I am trying to right a script .. if Org\_path == ABC/XYZ then return data target else return data target where Org\_path is not available

---

<div class="post-metadata">

**Author:** ![matw](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/matw/32/13913_2.png) [@matw](https://discuss.elastic.co/u/matw)\
**Post date:** [April 27, 2021, 7:53am UTC](https://discuss.elastic.co/t/if-else-condition-in-es-query/271319/4 "2021-04-27T07:53:34Z")

</div>

Pls provide some details about your use case, and if I follow your logic, if the data\_target field is always available, there's no need to add a condition? since always the field value is returned? thx!

---

<div class="post-metadata">

**Author:** ![Pallavibhushan](https://avatars.discourse-cdn.com/v4/letter/p/13edae/32.png) [@Pallavibhushan](https://discuss.elastic.co/u/Pallavibhushan)\
**Post date:** [April 27, 2021, 8:35am UTC](https://discuss.elastic.co/t/if-else-condition-in-es-query/271319/5 "2021-04-27T08:35:24Z")

</div>

Hi Matthias,

Yes, data\_target will always be available.  
Here what i am trying to do.  
Suppose, I have dashboard for client A,B and C. which share same data\_target.  
but, client D will have different data\_target. In ES I will have 2 entries.

1. for client A, B and C ( here I will not have Org\_path field)
2. For client D ( here I will have "Org\_path" : "client D" )  
So I want to hit only one time to db.  
Now on UI if i select client A , B or C from drop down then it should take generic value.  
But when I select Client D then data\_target value should change.

Sorry, I know this is very confusing.

I tried righting script in terms aggregation but Org\_path is tree so I am getting error  
GET target\_data\_new/\_search  
{  
"size": 200,  
"query": {  
"match\_all": {}  
},  
"aggs": {  
"NAME": {  
"terms": {  
"script": {  
"source": "doc['Org\_path'].value == params.tz",  
"params": {  
"tz":"/client/D"  
}  
}  
}  
}  
}  
}

---

<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:** [May 25, 2021, 8:35am UTC](https://discuss.elastic.co/t/if-else-condition-in-es-query/271319/6 "2021-05-25T08:35:39Z")

</div>

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