# Error when converting sql into elastic dsl for sql AND case

**URL:** <https://discuss.elastic.co/t/error-when-converting-sql-into-elastic-dsl-for-sql-and-case/229401>\
**Category:** Elasticsearch\
**Tags:** elastic-stack-sql\
**Created:** [April 23, 2020, 7:53am UTC](https://discuss.elastic.co/t/error-when-converting-sql-into-elastic-dsl-for-sql-and-case/229401 "2020-04-23T07:53:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Digil](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/digil/32/66942_2.png) [@Digil](https://discuss.elastic.co/u/Digil)\
**Post date:** [April 23, 2020, 7:53am UTC](https://discuss.elastic.co/t/error-when-converting-sql-into-elastic-dsl-for-sql-and-case/229401/1 "2020-04-23T07:53:40Z")

</div>

I have a sql query like this

`sql_1 = "SELECT * FROM dd_s3data WHERE (yelp_address = '370 Barren Rd' OR yelp_businessname ILIKE '%Computer%') AND (yelp_state = 'CT' OR yelp_category ILIKE '%flooring%');"`

I am trying to convert it to elasticsearch query. Here is the query I tried. It gives an OR result instead of AND

```auto
    es_query1 = {
        "query": {
            "constant_score": {
                "filter": {
                    "bool": {
                        "should": [
                            {"match_phrase": {"yelp_address": "370 Barren Rd"}},
                            {"match": {"yelp_businessname": "Computer"}}
                        ],
                        "should": [
                            {"match": {"yelp_state": "CT"}},
                            {"match_phrase": {"yelp_category": "flooring"}}
                        ]
                    }
                }
            }
        }
    }

```

I have an even big query that I have to convert after my first query is correct.

`sql_2 = "SELECT * FROM dd_s3data WHERE (yelp_address = '370 Barren Rd' OR yelp_businessname ILIKE '%Computer%') AND (yelp_state = 'CT' OR yelp_category ILIKE '%flooring%') AND yelp_noofreviews < 3.0 AND yelp_noofrating > 3.0;"`

How to convert my sql query so that I get an AND result instead of OR? Thanks...

---

<div class="post-metadata">

**Author:** ![matriv](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/matriv/32/43656_2.png) [@matriv](https://discuss.elastic.co/u/matriv)\
**Post date:** [April 28, 2020, 5:16pm UTC](https://discuss.elastic.co/t/error-when-converting-sql-into-elastic-dsl-for-sql-and-case/229401/2 "2020-04-28T17:16:46Z")

</div>

You can use the [SQL translate API](https://www.elastic.co/guide/en/elasticsearch/reference/current/sql-translate.html) and use `LIKE` instead of `ILIKE`.

---

<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 26, 2020, 5:16pm UTC](https://discuss.elastic.co/t/error-when-converting-sql-into-elastic-dsl-for-sql-and-case/229401/3 "2020-05-26T17:16:48Z")

</div>

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