# Building a query filtered query

**URL:** <https://discuss.elastic.co/t/building-a-query-filtered-query/3533>\
**Category:** Elasticsearch\
**Created:** [November 8, 2010, 7:43am UTC](https://discuss.elastic.co/t/building-a-query-filtered-query/3533 "2010-11-08T07:43:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mattias\_Pfeiffer](https://avatars.discourse-cdn.com/v4/letter/m/cdc98d/32.png) [@Mattias\_Pfeiffer](https://discuss.elastic.co/u/Mattias_Pfeiffer)\
**Post date:** [November 8, 2010, 7:43am UTC](https://discuss.elastic.co/t/building-a-query-filtered-query/3533/1 "2010-11-08T07:43:29Z")

</div>

Hi,

I'm having some trouble generating a query with a filter, and after  
hours trying to get it working I'm resorting to the mailing list.

I have a bunch of documents indexed in the form of:

"user": {  
"username": "John Doe",  
"description": "John is one of our best customers",  
"age": 50,  
"has\_image": true,  
"tags":  
"primary": ["accountant", "rich", "bald"],  
"secondary": ["jetsetter", "friendly"]  
}

Now I want to construct a query based on:

Query:

- "John" found in either "username" or "description"

Filters:

- "age" should be within 20 or 60
- "has\_image" should be true
- "tags" should include "jetsetter"
- "tags"."primary" should have both "accountant" and "bald"

I can't seem to find a way to include all of these, and the error  
messages from ES are hard to understand (to say the least).

Could somebody point me in the right direction?

Thanks!

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [November 8, 2010, 9:57pm UTC](https://discuss.elastic.co/t/building-a-query-filtered-query/3533/2 "2010-11-08T21:57:45Z")

</div>

Hi,

Here is the suggested query, haven't run it, so ping if its not working:

{  
"query" : {  
"filtered" : {  
"query" : {  
"query\_string" : {  
"query": "username:John description:John"  
}  
},  
"filter" : {  
"and" : [  
{  
"range" : {  
"age" : { "from" : 20, "to" : 60 }  
},  
},  
{  
"term" : {  
"has\_image" : true  
}  
},  
{  
"or" : [  
{  
"term" : {  
"tags.primary" : "jetsetter"  
}  
},  
{  
"term" : {  
"tags.secondary" : "jetsetter"  
}  
}  
]  
},  
{  
"term" : {  
"tags.primary" : "accountant"  
}  
},  
{  
"term" : {  
"tags.primary" : "bald"  
}  
}  
]  
}  
}  
}  
}

On Mon, Nov 8, 2010 at 9:43 AM, Mattias Pfeiffer \<[mattias.pfeiffer@gmail.com](mailto:mattias.pfeiffer@gmail.com)

> wrote:

> Hi,
> 
> I'm having some trouble generating a query with a filter, and after  
> hours trying to get it working I'm resorting to the mailing list.
> 
> I have a bunch of documents indexed in the form of:
> 
> "user": {  
> "username": "John Doe",  
> "description": "John is one of our best customers",  
> "age": 50,  
> "has\_image": true,  
> "tags":  
> "primary": ["accountant", "rich", "bald"],  
> "secondary": ["jetsetter", "friendly"]  
> }
> 
> Now I want to construct a query based on:
> 
> Query:
> 
> - "John" found in either "username" or "description"
> 
> Filters:
> 
> - "age" should be within 20 or 60
> - "has\_image" should be true
> - "tags" should include "jetsetter"
> - "tags"."primary" should have both "accountant" and "bald"
> 
> I can't seem to find a way to include all of these, and the error  
> messages from ES are hard to understand (to say the least).
> 
> Could somebody point me in the right direction?
> 
> Thanks!

---

<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, 4:16am UTC](https://discuss.elastic.co/t/building-a-query-filtered-query/3533/3 "2017-07-06T04:16:55Z")

</div>

Closed on July 6, 2017, 4:16am UTC


