# Help to convert SQL to ElasticSearch

**URL:** https://discuss.elastic.co/t/help-to-convert-sql-to-elasticsearch/102083
**Category:** Elasticsearch
**Created:** [September 28, 2017, 9:08am UTC](https://discuss.elastic.co/t/help-to-convert-sql-to-elasticsearch/102083 "2017-09-28T09:08:17Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![irfanh94](https://avatars.discourse-cdn.com/v4/letter/i/8dc957/32.png) [@irfanh94](https://discuss.elastic.co/u/irfanh94)
#### Post date: [September 28, 2017, 9:08am UTC](https://discuss.elastic.co/t/help-to-convert-sql-to-elasticsearch/102083/1 "2017-09-28T09:08:17Z")

</div>

Hey guys,

can somone help me to convert this sql query to elasticsearch query:

SELECT id, supplier FROM s\_articles WHERE supplier NOT IN (1,2) OR id IN (1,2,3,4,5,6)

---

<div class="post-metadata">

### Author: ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)
#### Post date: [September 28, 2017, 9:10am UTC](https://discuss.elastic.co/t/help-to-convert-sql-to-elasticsearch/102083/2 "2017-09-28T09:10:35Z")

</div>

What have you tried so far?

---

<div class="post-metadata">

### Author: ![irfanh94](https://avatars.discourse-cdn.com/v4/letter/i/8dc957/32.png) [@irfanh94](https://discuss.elastic.co/u/irfanh94)
#### Post date: [September 28, 2017, 9:11am UTC](https://discuss.elastic.co/t/help-to-convert-sql-to-elasticsearch/102083/3 "2017-09-28T09:11:54Z")

</div>

I have tried to use should =\> must\_not and should, but its not working.  
Everything i try its not working..

Last query i have tried:

```
{
	"query": {
			"bool":{
				
				"should":{
					"bool":{
						"must_not":[
							{"term":{"supplier":1}},
							{"term":{"supplier":2}}
						],
						"should":[
							{"term":{"id":1}},
							{"term":{"id":2}},
							{"term":{"id":3}},
							{"term":{"id":4}},
							{"term":{"id":5}},
							{"term":{"id":6}}
						]
					}
				}
			}
		}
		
}
```

---

<div class="post-metadata">

### Author: ![irfanh94](https://avatars.discourse-cdn.com/v4/letter/i/8dc957/32.png) [@irfanh94](https://discuss.elastic.co/u/irfanh94)
#### Post date: [September 28, 2017, 2:51pm UTC](https://discuss.elastic.co/t/help-to-convert-sql-to-elasticsearch/102083/4 "2017-09-28T14:51:05Z")

</div>

I have found the solution, thanks

---

<div class="post-metadata">

### Author: ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)
#### Post date: [September 28, 2017, 9:02pm UTC](https://discuss.elastic.co/t/help-to-convert-sql-to-elasticsearch/102083/5 "2017-09-28T21:02:38Z")

</div>

Can you share it, it might help someone in the future 🙂

---

<div class="post-metadata">

### Author: ![irfanh94](https://avatars.discourse-cdn.com/v4/letter/i/8dc957/32.png) [@irfanh94](https://discuss.elastic.co/u/irfanh94)
#### Post date: [September 29, 2017, 7:40am UTC](https://discuss.elastic.co/t/help-to-convert-sql-to-elasticsearch/102083/6 "2017-09-29T07:40:31Z")

</div>

Ofcourse, this is an solution.

```
{
	"query" : {
		"bool":{
			"should":[
					{
						"bool":{
							"must_not":[
								{"term":{"supplier":1}},
								{"term":{"supplier":2}}
							]
						}
					},
					{
						"bool":{
							"should":[
								{"term":{"id":1}},
								{"term":{"id":2}},
								{"term":{"id":3}},
								{"term":{"id":4}},
								{"term":{"id":5}},
								{"term":{"id":6}}
							]
						}
					}
			]
		}
	}
}
```

---

<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: [October 27, 2017, 7:40am UTC](https://discuss.elastic.co/t/help-to-convert-sql-to-elasticsearch/102083/7 "2017-10-27T07:40:35Z")

</div>

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