# Lengthy Opensearch Query formed from SQL using opendistro

**URL:** <https://forum.opensearch.org/t/lengthy-opensearch-query-formed-from-sql-using-opendistro/12206>\
**Category:** Open Source Elasticsearch and Kibana\
**Tags:** discuss, feature-request\
**Created:** [January 20, 2023, 6:19am UTC](https://forum.opensearch.org/t/lengthy-opensearch-query-formed-from-sql-using-opendistro/12206 "2023-01-20T06:19:37Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sahil](https://avatars.discourse-cdn.com/v4/letter/s/f6c823/32.png) [@Sahil](https://forum.opensearch.org/u/Sahil)\
**Post date:** [January 20, 2023, 6:19am UTC](https://forum.opensearch.org/t/lengthy-opensearch-query-formed-from-sql-using-opendistro/12206/1 "2023-01-20T06:19:37Z")

</div>

When we hit the opendistro endpoint with SQL query we are receiving an Opensearch query. For the current query, we get huge query. Is there any way using which we can optimise it ?

```auto
POST _opendistro/_sql/_explain
{
  "query": "select * from cmbs_dealreport_snapshot_dev where (dealid in (4752, 4503, 6114, 5987)) "
}

```

The Opensearch query we get is nested with multiple bool and should clauses. Rather can it not create an array and add all the values inside one should

```auto
{
   "query":{
      "bool":{
         "should":[
            {
               "term":{
                  "dealid":{
                     "value":4752,
                     "boost":1
                  }
               }
            },
            {
               "bool":{
                  "should":[
                     {
                        "term":{
                           "dealid":{
                              "value":4503,
                              "boost":1
                           }
                        }
                     },
                     {
                        "bool":{
                           "should":[
                              {
                                 "term":{
                                    "dealid":{
                                       "value":6114,
                                       "boost":1
                                    }
                                 }
                              },
                              {
                                 "term":{
                                    "dealid":{
                                       "value":5987,
                                       "boost":1
                                    }
                                 }
                              }
                           ],
                           "adjust_pure_negative":true,
                           "boost":1
                        }
                     }
                  ],
                  "adjust_pure_negative":true,
                  "boost":1
               }
            }
         ],
         "adjust_pure_negative":true,
         "boost":1
      }
   }
}

```

Expected Query

```auto
{
  "query": {
    "bool": {
      "should": [
        {
          "terms": {
            "dealid": [
              4752,
              4503,
              6114,
              5987
            ]
          }
        }
      ],
      "adjust_pure_negative": true,
      "boost": 1
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![ganesh\_kvks](https://avatars.discourse-cdn.com/v4/letter/g/dec6dc/32.png) [@ganesh\_kvks](https://forum.opensearch.org/u/ganesh_kvks)\
**Post date:** [January 6, 2025, 11:53am UTC](https://forum.opensearch.org/t/lengthy-opensearch-query-formed-from-sql-using-opendistro/12206/2 "2025-01-06T11:53:54Z")

</div>

I was facing this same issue. Were you able to resolve it?
