# SQL like query in ELasticsearch with AND OR

**URL:** <https://discuss.elastic.co/t/sql-like-query-in-elasticsearch-with-and-or/121319>\
**Category:** Elasticsearch\
**Created:** [February 24, 2018, 4:14am UTC](https://discuss.elastic.co/t/sql-like-query-in-elasticsearch-with-and-or/121319 "2018-02-24T04:14:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bhowmick.pb](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@bhowmick.pb](https://discuss.elastic.co/u/bhowmick.pb)\
**Post date:** [February 24, 2018, 4:14am UTC](https://discuss.elastic.co/t/sql-like-query-in-elasticsearch-with-and-or/121319/1 "2018-02-24T04:14:00Z")

</div>

Hi I am still playing around with elasticsearch(V6.1.3) and found it amazing but still struglling.I can do single term query search easily and code follows for my setup:

```
    from elasticsearch import Elasticsearch
es = Elasticsearch()
if not es.indices.exists(index="my-index"):
	customset={
		"settings": {
			"analysis": {
		  		"analyzer": {
		  			"ngram_analyzer": {
						"tokenizer": "ngram_tokenizer",
						"filter": [
							"lowercase",
							"asciifolding"
						]
		  			}
		  		},
		  		"tokenizer": {
		  			"ngram_tokenizer": {
						"type": "ngram",
						"min_gram": 3,
						"max_gram": 100,
						"token_chars": [
							"letter",
							"digit"
						]
		  			}
		  		}
			}
		},
	  	"mappings": {
			"my-type": {
		  		"properties": {
		  			"demodata": {
						"type": "text",
						"fields": {
							"ngram": {
			  					"type": "text",
			  					"analyzer": "ngram_analyzer",
			  					"search_analyzer": "standard"
							}
						}
		  			},
		  			"Field1": {
						"type": "text",
						"fields": {
							"ngram": {
			  					"type": "text",
			  					"analyzer": "ngram_analyzer",
			  					"search_analyzer": "standard"
							}
						}
		  			}
		  		}
			}
	  	}
	}

    es.indices.create(index="my-index", body=customset, ignore=400)
    docs=[
	{ "demodata": "hELLO" ,"field1":"Alex","field2":"Red"},
	{ "demodata": "hi" ,"field1":"Tom","field2":"Blue"},
	{ "demodata": "bye" ,"field1":"Jack","field2":"Green"},
	{ "demodata": "HeLlo WoRld!","field1":"Robert","field2":"Yellow" },
	{ "demodata": "xyz@abc.com","field1":"Dave","field2":"White" }
    ]
    for doc in docs:
	res = es.index(index="my-index", doc_type="my-type", body=doc)

es.indices.refresh(index="my-index")
res=helpers.scan(client=es,scroll='2m',index="my-index", doc_type="my-type",query={"query": {"match": {"demodata.ngram": "Hello"}}})

```

Now I want to do following SQL like query (suppose my SQL table name:my-type):

```
SELECT * FROM my-type WHERE (demodata LIKE '%ell%' OR demodata LIKE '%orld%') AND (field1 LIKE '%red%' OR demodata LIKE '%yellow%')

```

That means I have to search with multi terms in different fields. Can anyone suggest me how to do that?  
Thanks

---

<div class="post-metadata">

**Author:** ![shanec](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/shanec/32/4004_2.png) [@shanec](https://discuss.elastic.co/u/shanec)\
**Post date:** [March 2, 2018, 10:36pm UTC](https://discuss.elastic.co/t/sql-like-query-in-elasticsearch-with-and-or/121319/2 "2018-03-02T22:36:03Z")

</div>

You want a [bool](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html) query with a few `must` and `should` clauses.

---

<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 30, 2018, 10:36pm UTC](https://discuss.elastic.co/t/sql-like-query-in-elasticsearch-with-and-or/121319/3 "2018-03-30T22:36:20Z")

</div>

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