# Query DSL with AND, OR and NOT

**URL:** https://discuss.elastic.co/t/query-dsl-with-and-or-and-not/5199
**Category:** Elasticsearch
**Created:** [August 19, 2011, 10:24am UTC](https://discuss.elastic.co/t/query-dsl-with-and-or-and-not/5199 "2011-08-19T10:24:02Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Hugo](https://avatars.discourse-cdn.com/v4/letter/h/b38774/32.png) [@Hugo](https://discuss.elastic.co/u/Hugo)
#### Post date: [August 19, 2011, 10:24am UTC](https://discuss.elastic.co/t/query-dsl-with-and-or-and-not/5199/1 "2011-08-19T10:24:02Z")

</div>

Hi all,

I'm trying to create a query that gets all the users that have a  
specific name or contact and that live in europe.  
To do this I've created the query bellow but I always get the  
exception "SearchPhaseExecutionException".

query = {  
"filter" : {  
"and" : [  
{"or" : [  
{ "prefix" : { "name" : s\_v } },  
{ "prefix" : { "contact" : s\_v } }  
]},  
{"term" : { "region" : ''europe" }}  
],  
"not" : {  
"ids" : {  
"values" : user\_id  
}  
}  
}  
}  
}

I've tried using "custom\_filters\_score" and "filtered" but it also  
didn't work. I always get the same error.  
Can anyone please help me?

Best regards,  
Hugo

---

<div class="post-metadata">

### Author: ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)
#### Post date: [August 19, 2011, 10:29am UTC](https://discuss.elastic.co/t/query-dsl-with-and-or-and-not/5199/2 "2011-08-19T10:29:01Z")

</div>

Hi Hugo

> I'm trying to create a query that gets all the users that have a  
> specific name or contact and that live in europe.  
> To do this I've created the query bellow but I always get the  
> exception "SearchPhaseExecutionException".

Is this the actual JSON that you're passing to ES? If so, it is  
invalid, eg you have unquoted s\_v.

clint

> query = {  
> "filter" : {  
> "and" : [  
> {"or" : [  
> { "prefix" : { "name" : s\_v } },  
> { "prefix" : { "contact" : s\_v } }  
> ]},  
> {"term" : { "region" : ''europe" }}  
> ],  
> "not" : {  
> "ids" : {  
> "values" : user\_id  
> }  
> }  
> }  
> }  
> }
> 
> I've tried using "custom\_filters\_score" and "filtered" but it also  
> didn't work. I always get the same error.  
> Can anyone please help me?
> 
> Best regards,  
> Hugo

---

<div class="post-metadata">

### Author: ![Hugo](https://avatars.discourse-cdn.com/v4/letter/h/b38774/32.png) [@Hugo](https://discuss.elastic.co/u/Hugo)
#### Post date: [August 19, 2011, 10:38am UTC](https://discuss.elastic.co/t/query-dsl-with-and-or-and-not/5199/3 "2011-08-19T10:38:55Z")

</div>

Hi Clint,

Sorry, I forgot to tell that s\_v is a parameter. user\_id is also a  
parameter.  
I forgot also to tell that I added the 'not' to remove the current  
user from the results.

Thanks.  
Best regards,  
Hugo

On 19 Ago, 11:29, Clinton Gormley [cl...@traveljury.com](mailto:cl...@traveljury.com) wrote:

> Hi Hugo
> 
> > I'm trying to create a query that gets all the users that have a  
> > specific name or contact and that live in europe.  
> > To do this I've created the query bellow but I always get the  
> > exception "SearchPhaseExecutionException".
> 
> Is this the actual JSON that you're passing to ES? If so, it is  
> invalid, eg you have unquoted s\_v.
> 
> clint
> 
> > query = {  
> > "filter" : {  
> > "and" : [  
> > {"or" : [  
> > { "prefix" : { "name" : s\_v } },  
> > { "prefix" : { "contact" : s\_v } }  
> > ]},  
> > {"term" : { "region" : ''europe" }}  
> > ],  
> > "not" : {  
> > "ids" : {  
> > "values" : user\_id  
> > }  
> > }  
> > }  
> > }  
> > }
> 
> > I've tried using "custom\_filters\_score" and "filtered" but it also  
> > didn't work. I always get the same error.  
> > Can anyone please help me?
> 
> > Best regards,  
> > Hugo

---

<div class="post-metadata">

### Author: ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)
#### Post date: [August 19, 2011, 11:13am UTC](https://discuss.elastic.co/t/query-dsl-with-and-or-and-not/5199/4 "2011-08-19T11:13:56Z")

</div>

On Fri, 2011-08-19 at 03:38 -0700, Hugo wrote:

> Hi Clint,
> 
> Sorry, I forgot to tell that s\_v is a parameter. user\_id is also a  
> parameter.  
> I forgot also to tell that I added the 'not' to remove the current  
> user from the results.

So please can you gist [Discover gists · GitHub](http://gist.github.com/gists) the actual JSON that  
you're sending to ES.

ta

clint

---

<div class="post-metadata">

### Author: ![Hugo](https://avatars.discourse-cdn.com/v4/letter/h/b38774/32.png) [@Hugo](https://discuss.elastic.co/u/Hugo)
#### Post date: [August 19, 2011, 2:24pm UTC](https://discuss.elastic.co/t/query-dsl-with-and-or-and-not/5199/5 "2011-08-19T14:24:53Z")

</div>

Hi Clint,

Please check the gist [Query DSL with AND, OR and NOT · GitHub](https://gist.github.com/1156912).

Thanks.  
Best regards,  
Hugo

On 19 Ago, 12:13, Clinton Gormley [cl...@traveljury.com](mailto:cl...@traveljury.com) wrote:

> On Fri, 2011-08-19 at 03:38 -0700, Hugo wrote:
> 
> > Hi Clint,
> 
> > Sorry, I forgot to tell that s\_v is a parameter. user\_id is also a  
> > parameter.  
> > I forgot also to tell that I added the 'not' to remove the current  
> > user from the results.
> 
> So please can you gisthttp://gist.github.com/giststhe actual JSON that  
> you're sending to ES.
> 
> ta
> 
> clint

---

<div class="post-metadata">

### Author: ![Daniel\_E](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/daniel_e/32/3137_2.png) [@Daniel\_E](https://discuss.elastic.co/u/Daniel_E)
#### Post date: [August 19, 2011, 2:43pm UTC](https://discuss.elastic.co/t/query-dsl-with-and-or-and-not/5199/6 "2011-08-19T14:43:50Z")

</div>

Hugo,

There were a few DSL grammar errors and some structural weirdness in  
there. I forked your gist and typed out this. See if it works any  
better for you.

> <https://gist.github.com/deinspanjer/1156942>

On Aug 19, 10:24 am, Hugo [hugo....@gmail.com](mailto:hugo....@gmail.com) wrote:

> Hi Clint,
> 
> Please check the gisthttps://gist.github.com/1156912.
> 
> Thanks.  
> Best regards,  
> Hugo
> 
> On 19 Ago, 12:13, Clinton Gormley [cl...@traveljury.com](mailto:cl...@traveljury.com) wrote:
> 
> > On Fri, 2011-08-19 at 03:38 -0700, Hugo wrote:
> > 
> > > Hi Clint,
> 
> > > Sorry, I forgot to tell that s\_v is a parameter. user\_id is also a  
> > > parameter.  
> > > I forgot also to tell that I added the 'not' to remove the current  
> > > user from the results.
> 
> > So please can you gisthttp://gist.github.com/giststheactual JSON that  
> > you're sending to ES.
> 
> > ta
> 
> > clint

---

<div class="post-metadata">

### Author: ![Hugo](https://avatars.discourse-cdn.com/v4/letter/h/b38774/32.png) [@Hugo](https://discuss.elastic.co/u/Hugo)
#### Post date: [August 19, 2011, 4:04pm UTC](https://discuss.elastic.co/t/query-dsl-with-and-or-and-not/5199/7 "2011-08-19T16:04:13Z")

</div>

Hello Clint,

Its working. Thanks! What a great support!!!  
I tested your gist but it didn't work at first, but that was because I  
was using the version 0.16.  
Now I've upgraded to 0.17.2 and its working fine.

Thanks again!  
Best regards,  
Hugo

On 19 Ago, 15:43, Daniel E [deinspan...@gmail.com](mailto:deinspan...@gmail.com) wrote:

> Hugo,
> 
> There were a few DSL grammar errors and some structural weirdness in  
> there. I forked your gist and typed out this. See if it works any  
> better for you.[Query DSL with AND, OR and NOT · GitHub](https://gist.github.com/1156942)
> 
> On Aug 19, 10:24 am, Hugo [hugo....@gmail.com](mailto:hugo....@gmail.com) wrote:
> 
> > Hi Clint,
> 
> > Please check the gisthttps://gist.github.com/1156912.
> 
> > Thanks.  
> > Best regards,  
> > Hugo
> 
> > On 19 Ago, 12:13, Clinton Gormley [cl...@traveljury.com](mailto:cl...@traveljury.com) wrote:
> 
> > > On Fri, 2011-08-19 at 03:38 -0700, Hugo wrote:
> > > 
> > > > Hi Clint,
> 
> > > > Sorry, I forgot to tell that s\_v is a parameter. user\_id is also a  
> > > > parameter.  
> > > > I forgot also to tell that I added the 'not' to remove the current  
> > > > user from the results.
> 
> > > So please can you gisthttp://gist.github.com/giststheactualJSON that  
> > > you're sending to ES.
> 
> > > ta
> 
> > > clint

---

<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: [July 6, 2017, 3:56am UTC](https://discuss.elastic.co/t/query-dsl-with-and-or-and-not/5199/8 "2017-07-06T03:56:31Z")

</div>


