# How to write AND Query in Elasticsearch?

**URL:** <https://discuss.elastic.co/t/how-to-write-and-query-in-elasticsearch/50612>\
**Category:** Elasticsearch\
**Created:** [May 21, 2016, 7:00am UTC](https://discuss.elastic.co/t/how-to-write-and-query-in-elasticsearch/50612 "2016-05-21T07:00:58Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sigehere1](https://avatars.discourse-cdn.com/v4/letter/s/d26b3c/32.png) [@Sigehere1](https://discuss.elastic.co/u/Sigehere1)\
**Post date:** [May 21, 2016, 7:00am UTC](https://discuss.elastic.co/t/how-to-write-and-query-in-elasticsearch/50612/1 "2016-05-21T07:00:59Z")

</div>

## Document 1

{  
"userid" : 1234,  
"username" : "Sigehere Smith",  
"type" : "student",  
"department" : {  
"deptname" : "Computer Science",  
"deptpath" : ["First" , "Second"]  
}  
}

## Document 2

{  
"userid" : 1235,  
"username" : "Rayan Smith",  
"type" : "teacher",  
"department" : {  
"deptname" : "Computer Science",  
"deptpath" : ["Four" , "Second"]  
}  
}

## Document 3

{  
"userid" : 1237,  
"username" : "Lucky Smith",  
"type" : "teacher",  
"department" : {  
"deptname" : "Computer Science",  
"deptpath" : ["Four" , "First"]  
}  
}

I want to retrive that document. that contains following information.

```
   "department" : {
           "deptname" : "Computer Science",
          "deptpath" : "second"
    }

```

How will i write and query on above data.

---

<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:** [May 21, 2016, 8:26am UTC](https://discuss.elastic.co/t/how-to-write-and-query-in-elasticsearch/50612/2 "2016-05-21T08:26:23Z")

</div>

Use a Bool query with must or filter clauses.  
Must if relevancy is important. Filter if it's not.

---

<div class="post-metadata">

**Author:** ![Sigehere1](https://avatars.discourse-cdn.com/v4/letter/s/d26b3c/32.png) [@Sigehere1](https://discuss.elastic.co/u/Sigehere1)\
**Post date:** [May 21, 2016, 8:40am UTC](https://discuss.elastic.co/t/how-to-write-and-query-in-elasticsearch/50612/3 "2016-05-21T08:40:03Z")

</div>

Thanks For your response.

I have tried following query: I want to apply AND operator in that query to retrive those document that department name is Computer Science AND department path contains term "Second".  
curl -XGET '[http://192.168.1.2:9200/records/\_search](http://192.168.1.2:9200/records/_search)' -d'{  
"query": {  
"bool": {  
"must": [  
{  
"match":  
{  
"department.deptname" : "Computer Science"  
}  
},  
{  
"match":  
{  
"department.deptpath" : "Second"  
}  
}  
]  
}  
}  
}'

---

<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:** [May 21, 2016, 10:03am UTC](https://discuss.elastic.co/t/how-to-write-and-query-in-elasticsearch/50612/4 "2016-05-21T10:03:21Z")

</div>

This looks correct to me. Just be careful with the analyzers you are using.

Might not work as you would expect.

BTW, I'd use filter instead of must here.

---

<div class="post-metadata">

**Author:** ![Sigehere1](https://avatars.discourse-cdn.com/v4/letter/s/d26b3c/32.png) [@Sigehere1](https://discuss.elastic.co/u/Sigehere1)\
**Post date:** [May 21, 2016, 11:26am UTC](https://discuss.elastic.co/t/how-to-write-and-query-in-elasticsearch/50612/5 "2016-05-21T11:26:21Z")

</div>

Yes It gives me all record. It will not full fill my requirement because match doesn't apply on department object it just applies on document, It will retrive all document that contains department name is Computer Science or department path "Second".

Can you please give me suggestion how will i write correct query for object.

---

<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:** [May 21, 2016, 7:21pm UTC](https://discuss.elastic.co/t/how-to-write-and-query-in-elasticsearch/50612/6 "2016-05-21T19:21:58Z")

</div>

Can you send a full script which I can use as a basis?

---

<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 5, 2017, 10:49pm UTC](https://discuss.elastic.co/t/how-to-write-and-query-in-elasticsearch/50612/7 "2017-07-05T22:49:59Z")

</div>


