# ES query question

**URL:** <https://discuss.elastic.co/t/es-query-question/8762>\
**Category:** Elasticsearch\
**Created:** [August 16, 2012, 6:59pm UTC](https://discuss.elastic.co/t/es-query-question/8762 "2012-08-16T18:59:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![msya](https://avatars.discourse-cdn.com/v4/letter/m/5f9b8f/32.png) [@msya](https://discuss.elastic.co/u/msya)\
**Post date:** [August 16, 2012, 6:59pm UTC](https://discuss.elastic.co/t/es-query-question/8762/1 "2012-08-16T18:59:25Z")

</div>

Hello,

I have indexed the following:

\_index: community\_id\_113  
\_type: question  
group\_id : 123  
user\_id: 2  
message: "Hello, how are you?"

\_index: community\_id\_113  
\_type:question  
group\_id:143  
user\_id:5  
message: "How is it going?"

I want to perform a search for the keyword "How", but I only want to search  
within messages with user\_id: 5 and and group\_id: 143. How do I do this?

--

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [August 16, 2012, 8:15pm UTC](https://discuss.elastic.co/t/es-query-question/8762/2 "2012-08-16T20:15:58Z")

</div>

Use filtered queries and filter your "how" query with a bool filter :  
Must termFilter group\_id : 143  
Must termFilter user\_id : 5

> **[Elastic — The Search AI Company](https://www.elastic.co)**
>
> Power insights and outcomes with The Elastic Search AI Platform. See into your data and find answers that matter with enterprise solutions designed to help you accelerate time to insight. Try Elastic ...

> **[Elastic — The Search AI Company](https://www.elastic.co)**
>
> Power insights and outcomes with The Elastic Search AI Platform. See into your data and find answers that matter with enterprise solutions designed to help you accelerate time to insight. Try Elastic ...

> **[Elastic — The Search AI Company](https://www.elastic.co)**
>
> Power insights and outcomes with The Elastic Search AI Platform. See into your data and find answers that matter with enterprise solutions designed to help you accelerate time to insight. Try Elastic ...

## HTH David

Le 16 août 2012 à 20:59, msya [mohitsya@gmail.com](mailto:mohitsya@gmail.com) a écrit :

> Hello,
> 
> I have indexed the following:
> 
> \_index: community\_id\_113  
> \_type: question  
> group\_id : 123  
> user\_id: 2  
> message: "Hello, how are you?"
> 
> \_index: community\_id\_113  
> \_type:question  
> group\_id:143  
> user\_id:5  
> message: "How is it going?"
> 
> I want to perform a search for the keyword "How", but I only want to search within messages with user\_id: 5 and and group\_id: 143. How do I do this?
> 
> --

--

---

<div class="post-metadata">

**Author:** ![msya](https://avatars.discourse-cdn.com/v4/letter/m/5f9b8f/32.png) [@msya](https://discuss.elastic.co/u/msya)\
**Post date:** [August 17, 2012, 1:21am UTC](https://discuss.elastic.co/t/es-query-question/8762/3 "2012-08-17T01:21:12Z")

</div>

Thank you for replying to my message. I constructed the following query:

curl -XGET 'localhost:9200/community\_id\_1113/question/\_search?pretty=true'  
-d '  
{"query" : {  
"filtered" : {  
"query" : {  
"queryString" : {  
"default\_field": "message",  
"query" : "how"  
}  
},  
"filter" : {  
"bool" : {  
"must" : [  
{  
"term" : { "user\_id" : "143" }  
},  
{  
"term" : { "group\_id" : "5" }  
}  
]  
}  
}  
}  
}  
}'

This gave me the right results, however I haven't index a large amount of  
data. Would this query be right? In terms of performance, would it run slow  
for a large indexed dataset?

On Thursday, August 16, 2012 4:15:58 PM UTC-4, David Pilato wrote:

> Use filtered queries and filter your "how" query with a bool filter :  
> Must termFilter group\_id : 143  
> Must termFilter user\_id : 5
> 
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/filtered-query.html)  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/bool-filter.html)  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/term-filter.html)
> 
> ## HTH David
> 
> Le 16 août 2012 à 20:59, msya \<[mohi...@gmail.com](mailto:mohi...@gmail.com) \<javascript:\>\> a écrit :
> 
> Hello,
> 
> I have indexed the following:
> 
> \_index: community\_id\_113  
> \_type: question  
> group\_id : 123  
> user\_id: 2  
> message: "Hello, how are you?"
> 
> \_index: community\_id\_113  
> \_type:question  
> group\_id:143  
> user\_id:5  
> message: "How is it going?"
> 
> I want to perform a search for the keyword "How", but I only want to  
> search within messages with user\_id: 5 and and group\_id: 143. How do I do  
> this?
> 
> --

--

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [August 17, 2012, 1:53am UTC](https://discuss.elastic.co/t/es-query-question/8762/4 "2012-08-17T01:53:37Z")

</div>

IMHO the query looks nice and it will be fast on large dataset.

You will have to perform some tests and if needed adjust the number of shards and replica. But query looks ok like this.

My 2 cents.

David 😉  
Twitter : @dadoonet / @elasticsearchfr

Le 17 août 2012 à 03:21, msya [mohitsya@gmail.com](mailto:mohitsya@gmail.com) a écrit :

> Thank you for replying to my message. I constructed the following query:
> 
> curl -XGET 'localhost:9200/community\_id\_1113/question/\_search?pretty=true' -d '  
> {"query" : {  
> "filtered" : {  
> "query" : {  
> "queryString" : {  
> "default\_field": "message",  
> "query" : "how"  
> }  
> },  
> "filter" : {  
> "bool" : {  
> "must" : [  
> {  
> "term" : { "user\_id" : "143" }  
> },  
> {  
> "term" : { "group\_id" : "5" }  
> }  
> ]  
> }  
> }  
> }  
> }  
> }'
> 
> This gave me the right results, however I haven't index a large amount of data. Would this query be right? In terms of performance, would it run slow for a large indexed dataset?
> 
> On Thursday, August 16, 2012 4:15:58 PM UTC-4, David Pilato wrote:  
> Use filtered queries and filter your "how" query with a bool filter :  
> Must termFilter group\_id : 143  
> Must termFilter user\_id : 5
> 
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/filtered-query.html)  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/bool-filter.html)  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/term-filter.html)
> 
> ## HTH David
> 
> Le 16 août 2012 à 20:59, msya [mohi...@gmail.com](mailto:mohi...@gmail.com) a écrit :
> 
> > Hello,
> > 
> > I have indexed the following:
> > 
> > \_index: community\_id\_113  
> > \_type: question  
> > group\_id : 123  
> > user\_id: 2  
> > message: "Hello, how are you?"
> > 
> > \_index: community\_id\_113  
> > \_type:question  
> > group\_id:143  
> > user\_id:5  
> > message: "How is it going?"
> > 
> > I want to perform a search for the keyword "How", but I only want to search within messages with user\_id: 5 and and group\_id: 143. How do I do this?
> > 
> > --
> 
> --

--

---

<div class="post-metadata">

**Author:** ![Navvi](https://avatars.discourse-cdn.com/v4/letter/n/13edae/32.png) [@Navvi](https://discuss.elastic.co/u/Navvi)\
**Post date:** [August 17, 2012, 11:57am UTC](https://discuss.elastic.co/t/es-query-question/8762/5 "2012-08-17T11:57:44Z")

</div>

How to construct a query to search for user\_id: 5 , group\_id: 143 ,  
message is "how" . I mean it should return results based on all the field  
values given .  
If possible suggest me the code snippet using java api.

Thanks,  
Naveen Vishwanatham.

On Thursday, August 16, 2012 8:53:37 PM UTC-5, David Pilato wrote:

> IMHO the query looks nice and it will be fast on large dataset.
> 
> You will have to perform some tests and if needed adjust the number of  
> shards and replica. But query looks ok like this.
> 
> My 2 cents.
> 
> David 😉  
> Twitter : @dadoonet / @elasticsearchfr
> 
> Le 17 août 2012 à 03:21, msya \<[mohi...@gmail.com](mailto:mohi...@gmail.com) \<javascript:\>\> a écrit :
> 
> Thank you for replying to my message. I constructed the following  
> query:
> 
> curl -XGET 'localhost:9200/community\_id\_1113/question/\_search?pretty=true'  
> -d '  
> {"query" : {  
> "filtered" : {  
> "query" : {  
> "queryString" : {  
> "default\_field": "message",  
> "query" : "how"  
> }  
> },  
> "filter" : {  
> "bool" : {  
> "must" : [  
> {  
> "term" : { "user\_id" : "143" }  
> },  
> {  
> "term" : { "group\_id" : "5" }  
> }  
> ]  
> }  
> }  
> }  
> }  
> }'
> 
> This gave me the right results, however I haven't index a large amount of  
> data. Would this query be right? In terms of performance, would it run slow  
> for a large indexed dataset?
> 
> On Thursday, August 16, 2012 4:15:58 PM UTC-4, David Pilato wrote:
> 
> > Use filtered queries and filter your "how" query with a bool filter :  
> > Must termFilter group\_id : 143  
> > Must termFilter user\_id : 5
> > 
> > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/filtered-query.html)  
> > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/bool-filter.html)  
> > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/term-filter.html)
> > 
> > ## HTH David
> > 
> > Le 16 août 2012 à 20:59, msya [mohi...@gmail.com](mailto:mohi...@gmail.com) a écrit :
> > 
> > Hello,
> > 
> > I have indexed the following:
> > 
> > \_index: community\_id\_113  
> > \_type: question  
> > group\_id : 123  
> > user\_id: 2  
> > message: "Hello, how are you?"
> > 
> > \_index: community\_id\_113  
> > \_type:question  
> > group\_id:143  
> > user\_id:5  
> > message: "How is it going?"
> > 
> > I want to perform a search for the keyword "How", but I only want to  
> > search within messages with user\_id: 5 and and group\_id: 143. How do I do  
> > this?
> > 
> > --
> > 
> > --

--

---

<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:16am UTC](https://discuss.elastic.co/t/es-query-question/8762/6 "2017-07-06T03:16:05Z")

</div>

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


