# Query question

**URL:** https://discuss.elastic.co/t/query-question/17346
**Category:** Elasticsearch
**Created:** [May 5, 2014, 10:36am UTC](https://discuss.elastic.co/t/query-question/17346 "2014-05-05T10:36:03Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![11148](https://avatars.discourse-cdn.com/v4/letter/1/db5fbb/32.png) [@11148](https://discuss.elastic.co/u/11148)
#### Post date: [May 5, 2014, 10:36am UTC](https://discuss.elastic.co/t/query-question/17346/1 "2014-05-05T10:36:03Z")

</div>

Hi,

I'm new to Elasticsearch and try to integrate it into our project but met a  
problem. In our data source, it has two fields: mode and schedule which are  
all integer. Through UI, it should can query records based on these two  
fields like:  
_SELECT \* FROM doc WHERE mode = 1 AND (schedule = 1 OR schedule = 3)_

I tried below query JSONs but none return the expected results, anyone can  
help? Thank you!

_{_

- "query": {\*
- "bool": {\*
- 

```
 "must": [*

```

- 

```
   { "match": { "mode": 1 } }*

```

- 

```
 ],*

```

- 

```
 "should": [*

```

- { "match": { "schedule": 1 } },\*
- 

```
   { "match": { "schedule": 3 } }*

```

- 

```
 ]*

```

- }\*
- }\*  
_}_

* * *

_{ "query": { "filtered": { "query": { "match\_all": {} },  
"filter": { "and" : [ { "term"  
: { "mode" : "1" } } ] }, "filter":  
{ "and" : [ { "term" : {  
"schedule" : "1" } }, { "term" : {  
"schedule" : "3" } } ] } } }}_

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/45bd7de6-ffe9-4d9f-bef6-be11e19b051f%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/45bd7de6-ffe9-4d9f-bef6-be11e19b051f%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)
#### Post date: [May 7, 2014, 4:10pm UTC](https://discuss.elastic.co/t/query-question/17346/2 "2014-05-07T16:10:42Z")

</div>

Your two clauses, mode and schedule, are joined via an AND, so those two  
clauses should be part of the \*must \*section. The schedule clauses is then  
an OR between two clauses, so it should be a nested bool filter using  
_should_. Hopefully that made sense. 🙂

Since you are using term queries on what are hopefully non-analyzed fields  
(numeric fields are always non-analyzed), I will use a match all query with  
filters since it should be more efficient. The query should looking  
something like:

{  
"query": {  
"filtered": {  
"query": {  
"match\_all": {}  
},  
"filter": {  
"bool": {  
"must": [  
{  
"term": { "mode": "1" }  
},  
{  
"bool": {  
"should": [  
{  
"term": { "schedule": "1" }  
},  
{  
"term": { "schedule": "3" }  
}  
]  
}  
}  
]  
}  
}  
}  
}  
}

--  
Ivan

On Mon, May 5, 2014 at 3:36 AM, 曾岩 [eater2046@gmail.com](mailto:eater2046@gmail.com) wrote:

> Hi,
> 
> I'm new to Elasticsearch and try to integrate it into our project but met  
> a problem. In our data source, it has two fields: mode and schedule which  
> are all integer. Through UI, it should can query records based on these two  
> fields like:  
> _SELECT \* FROM doc WHERE mode = 1 AND (schedule = 1 OR schedule = 3)_
> 
> I tried below query JSONs but none return the expected results, anyone can  
> help? Thank you!
> 
> _{_
> 
> - "query": {\*
> - "bool": {\*
> - 
> 
> ```
> "must": [*
> 
> ```
> 
> - 
> 
> ```
> { "match": { "mode": 1 } }*
> 
> ```
> 
> - 
> 
> ```
> ],*
> 
> ```
> 
> - 
> 
> ```
> "should": [*
> 
> ```
> 
> - { "match": { "schedule": 1 } },\*
> - 
> 
> ```
> { "match": { "schedule": 3 } }*
> 
> ```
> 
> - 
> 
> ```
> ]*
> 
> ```
> 
> - }\*
> - }\*  
> _}_
> 
> * * *
> 
> _{ "query": { "filtered": { "query": { "match\_all": {} },  
> "filter": { "and" : [ { "term"  
> : { "mode" : "1" } } ] }, "filter":  
> { "and" : [ { "term" : {  
> "schedule" : "1" } }, { "term" : {  
> "schedule" : "3" } } ] } } }}_
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/45bd7de6-ffe9-4d9f-bef6-be11e19b051f%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/45bd7de6-ffe9-4d9f-bef6-be11e19b051f%40googlegroups.com)[https://groups.google.com/d/msgid/elasticsearch/45bd7de6-ffe9-4d9f-bef6-be11e19b051f%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/45bd7de6-ffe9-4d9f-bef6-be11e19b051f%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQC1qjqZBgKXQujiXpp8TCKS0s4QRv97YHXO7\_48\_Ld8ZA%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQC1qjqZBgKXQujiXpp8TCKS0s4QRv97YHXO7_48_Ld8ZA%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

### Author: ![11148](https://avatars.discourse-cdn.com/v4/letter/1/db5fbb/32.png) [@11148](https://discuss.elastic.co/u/11148)
#### Post date: [May 13, 2014, 2:34am UTC](https://discuss.elastic.co/t/query-question/17346/3 "2014-05-13T02:34:30Z")

</div>

Thank you very much!

--  
Eateral

在 2014年5月8日星期四UTC+8上午12时10分42秒，Ivan Brusic写道：

> Your two clauses, mode and schedule, are joined via an AND, so those two  
> clauses should be part of the \*must \*section. The schedule clauses is  
> then an OR between two clauses, so it should be a nested bool filter using  
> _should_. Hopefully that made sense. 🙂
> 
> Since you are using term queries on what are hopefully non-analyzed fields  
> (numeric fields are always non-analyzed), I will use a match all query with  
> filters since it should be more efficient. The query should looking  
> something like:
> 
> {  
> "query": {  
> "filtered": {  
> "query": {  
> "match\_all": {}  
> },  
> "filter": {  
> "bool": {  
> "must": [  
> {  
> "term": { "mode": "1" }  
> },  
> {  
> "bool": {  
> "should": [  
> {  
> "term": { "schedule": "1" }  
> },  
> {  
> "term": { "schedule": "3" }  
> }  
> ]  
> }  
> }  
> ]  
> }  
> }  
> }  
> }  
> }
> 
> --  
> Ivan
> 
> On Mon, May 5, 2014 at 3:36 AM, 曾岩 \<[eate...@gmail.com](mailto:eate...@gmail.com) \<javascript:\>\>wrote:
> 
> > Hi,
> > 
> > I'm new to Elasticsearch and try to integrate it into our project but met  
> > a problem. In our data source, it has two fields: mode and schedule which  
> > are all integer. Through UI, it should can query records based on these two  
> > fields like:  
> > _SELECT \* FROM doc WHERE mode = 1 AND (schedule = 1 OR schedule = 3)_
> > 
> > I tried below query JSONs but none return the expected results, anyone  
> > can help? Thank you!
> > 
> > _{_
> > 
> > - "query": {\*
> > - "bool": {\*
> > - 
> > 
> > ```
> > "must": [*
> > 
> > ```
> > 
> > - 
> > 
> > ```
> > { "match": { "mode": 1 } }*
> > 
> > ```
> > 
> > - 
> > 
> > ```
> > ],*
> > 
> > ```
> > 
> > - 
> > 
> > ```
> > "should": [*
> > 
> > ```
> > 
> > - { "match": { "schedule": 1 } },\*
> > - 
> > 
> > ```
> > { "match": { "schedule": 3 } }*
> > 
> > ```
> > 
> > - 
> > 
> > ```
> > ]*
> > 
> > ```
> > 
> > - }\*
> > - }\*  
> > _}_
> > 
> > * * *
> > 
> > _{ "query": { "filtered": { "query": { "match\_all": {} },  
> > "filter": { "and" : [ { "term"  
> > : { "mode" : "1" } } ] }, "filter":  
> > { "and" : [ { "term" : {  
> > "schedule" : "1" } }, { "term" : {  
> > "schedule" : "3" } } ] } } }}_
> > 
> > --  
> > You received this message because you are subscribed to the Google Groups  
> > "elasticsearch" group.  
> > To unsubscribe from this group and stop receiving emails from it, send an  
> > email to [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> > To view this discussion on the web visit  
> > [https://groups.google.com/d/msgid/elasticsearch/45bd7de6-ffe9-4d9f-bef6-be11e19b051f%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/45bd7de6-ffe9-4d9f-bef6-be11e19b051f%40googlegroups.com)[https://groups.google.com/d/msgid/elasticsearch/45bd7de6-ffe9-4d9f-bef6-be11e19b051f%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/45bd7de6-ffe9-4d9f-bef6-be11e19b051f%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > .  
> > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/0e8e2332-d5b7-416b-a16e-f965884cc42f%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/0e8e2332-d5b7-416b-a16e-f965884cc42f%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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, 1:29am UTC](https://discuss.elastic.co/t/query-question/17346/4 "2017-07-06T01:29:55Z")

</div>


