# Elasticsearch filter multiple fields

**URL:** https://discuss.elastic.co/t/elasticsearch-filter-multiple-fields/25855
**Category:** Elasticsearch
**Created:** [July 19, 2015, 10:54am UTC](https://discuss.elastic.co/t/elasticsearch-filter-multiple-fields/25855 "2015-07-19T10:54:35Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Sivaprakash\_Shanmuga](https://avatars.discourse-cdn.com/v4/letter/s/ecae2f/32.png) [@Sivaprakash\_Shanmuga](https://discuss.elastic.co/u/Sivaprakash_Shanmuga)
#### Post date: [July 19, 2015, 10:54am UTC](https://discuss.elastic.co/t/elasticsearch-filter-multiple-fields/25855/1 "2015-07-19T10:54:35Z")

</div>

I have following data set indexed in my elasticsearch

> {  
> "title": "Professor",  
> "class": [6001, 6015, 6018],  
> "subject": [5010, 5012, 5013],  
> "salary": 12500  
> }

> {  
> "title": "Professor",  
> "class": [6012, 6013, 6014],  
> "subject": [5010, 5005, 5004],  
> "salary": 16600  
> }

> {  
> "title": "Principal",  
> "class": [7001, 7010, 6018],  
> "subject": [5010, 5012, 5013],  
> "salary": 14750  
> }

> {  
> "title": "Asst Professor",  
> "class": [6012, 6013, 6014],  
> "subject": [5010, 5005, 5004],  
> "salary": 16600  
> }  
> I'm trying to search

> class - 6001 OR 6018 OR 6013 AND subject - 5013 OR 5004 AND salary (range) - 12000 to 15000

I'm using below query

> {  
> "query" : {  
> "filtered" : {  
> "filter" : {  
> "bool" : {  
> "must" : [  
> { "term" : {"class" : [6001, 6018, 6013]}},  
> { "term" : {"subject" : [5013, 5004]}},  
> {"range" : {"salary" : {"gte": 12000,"lte": 15000}}}  
> ]  
> }}}}}

But I'm not getting the result that Im expecting, what is wrong in this?

---

<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: [July 20, 2015, 1:53am UTC](https://discuss.elastic.co/t/elasticsearch-filter-multiple-fields/25855/2 "2015-07-20T01:53:13Z")

</div>

Try using a Terms filter instead of Term. More flexibility.

[https://www.elastic.co/guide/en/elasticsearch/reference/1.6/query-dsl-terms-filter.html](https://www.elastic.co/guide/en/elasticsearch/reference/1.6/query-dsl-terms-filter.html)

Cheers,

Ivan

---

<div class="post-metadata">

### Author: ![Sivaprakash\_Shanmuga](https://avatars.discourse-cdn.com/v4/letter/s/ecae2f/32.png) [@Sivaprakash\_Shanmuga](https://discuss.elastic.co/u/Sivaprakash_Shanmuga)
#### Post date: [July 20, 2015, 5:16am UTC](https://discuss.elastic.co/t/elasticsearch-filter-multiple-fields/25855/3 "2015-07-20T05:16:47Z")

</div>

Thank you... I want to apply AND between terms? I tried the below but it doesnt apply.

> {  
> "query" : {  
> "filtered" : {  
> "filter" : {  
> "bool" : {  
> "and" : [  
> { "terms" : {"class" : ["6001", "6018"]}},  
> { "terms" : {"subject" : ["5013"]}},  
> { "range" : {"salary" : {"gte": 12000,"lte": 15000}}}  
> ]  
> }}}}}

---

<div class="post-metadata">

### Author: ![colings86](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/colings86/32/44960_2.png) [@colings86](https://discuss.elastic.co/u/colings86)
#### Post date: [July 20, 2015, 7:01am UTC](https://discuss.elastic.co/t/elasticsearch-filter-multiple-fields/25855/4 "2015-07-20T07:01:57Z")

</div>

Have a look at the documentation for the [`bool` filter](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-filter.html) for an example you can look at and the docs for the [`bool` query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html) for an explanation of what the different clause types mean. In your example above you will probably want to use `must` clauses.

---

<div class="post-metadata">

### Author: ![Sivaprakash\_Shanmuga](https://avatars.discourse-cdn.com/v4/letter/s/ecae2f/32.png) [@Sivaprakash\_Shanmuga](https://discuss.elastic.co/u/Sivaprakash_Shanmuga)
#### Post date: [July 21, 2015, 6:17pm UTC](https://discuss.elastic.co/t/elasticsearch-filter-multiple-fields/25855/5 "2015-07-21T18:17:25Z")

</div>

Hi

I have written the below, but it returns all data insted of specific result. Anything wrong in this? I want extract data in which Class has to be 7001 OR subject should be 5012 AND subject should not be 5013

> {  
> "query" : {  
> "filtered" : {  
> "filter" : {  
> "bool" : {  
> "must" : [  
> { "term" : {"class" : "7001"}},  
> { "term" : {"subject" : "5012"}}  
> ],  
> "must\_not" : {  
> "term" : {"subject" : "5013"}  
> }  
> }  
> }  
> }  
> }  
> }

---

<div class="post-metadata">

### Author: ![ayushsangani](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ayushsangani/32/9612_2.png) [@ayushsangani](https://discuss.elastic.co/u/ayushsangani)
#### Post date: [July 21, 2015, 7:13pm UTC](https://discuss.elastic.co/t/elasticsearch-filter-multiple-fields/25855/6 "2015-07-21T19:13:13Z")

</div>

I believe you may want to use `should` clause inside `must` clause that will give you basically `OR` flavor in your must clause.

---

<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, 12:00am UTC](https://discuss.elastic.co/t/elasticsearch-filter-multiple-fields/25855/7 "2017-07-06T00:00:06Z")

</div>


