# Create a query with OR & And condition in elasticsearch

**URL:** <https://discuss.elastic.co/t/create-a-query-with-or-and-condition-in-elasticsearch/295799>\
**Category:** Elasticsearch\
**Created:** [January 31, 2022, 4:54am UTC](https://discuss.elastic.co/t/create-a-query-with-or-and-condition-in-elasticsearch/295799 "2022-01-31T04:54:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mojahid\_Ahmad](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mojahid_ahmad/32/78061_2.png) [@Mojahid\_Ahmad](https://discuss.elastic.co/u/Mojahid_Ahmad)\
**Post date:** [January 31, 2022, 4:54am UTC](https://discuss.elastic.co/t/create-a-query-with-or-and-condition-in-elasticsearch/295799/1 "2022-01-31T04:54:42Z")

</div>

Hi,

I am trying to convert this sql query to the DSL query to query in Python

```auto
select count(*) from dean_acc_p_alm_asset
where (substring(cast(sys_updated_on as varchar),1,10) >= '2021-11-01' and substring(cast(sys_updated_on as varchar),1,10) < '2021-11-02')
or (substring(cast(u_state_date as varchar),1,10) >= '2021-11-01' and substring(cast(u_state_date as varchar),1,10) < '2021-11-02')

```

I tried multiple ways to do that but could not get success. One of the example I tried is

`alm_asset_result = es.search(index=alm_asset_index,size = 10000,body={"query": {"bool": {"should": [{"range" : {"sys_updated_on" : {"gte" : tempStart, "lte": tempEnd}}},{"range" : {"u_state_date" : {"gte" : tempStart, "lte": tempEnd}}}]}}})`

Can you please correct where I am doing mistakes? Thank you

---

<div class="post-metadata">

**Author:** ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)\
**Post date:** [February 1, 2022, 1:48am UTC](https://discuss.elastic.co/t/create-a-query-with-or-and-condition-in-elasticsearch/295799/2 "2022-02-01T01:48:34Z")

</div>

> [@Mojahid\_Ahmad](#):
>
> I tried multiple ways to do that but could not get success.

What was the problem of the result?

---

<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:** [March 1, 2022, 1:49am UTC](https://discuss.elastic.co/t/create-a-query-with-or-and-condition-in-elasticsearch/295799/3 "2022-03-01T01:49:24Z")

</div>

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