# Retrieve document where certain field exists

**URL:** https://discuss.elastic.co/t/retrieve-document-where-certain-field-exists/48150
**Category:** Elasticsearch
**Created:** [April 22, 2016, 9:05am UTC](https://discuss.elastic.co/t/retrieve-document-where-certain-field-exists/48150 "2016-04-22T09:05:05Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![bharath\_meka](https://avatars.discourse-cdn.com/v4/letter/b/858c86/32.png) [@bharath\_meka](https://discuss.elastic.co/u/bharath_meka)
#### Post date: [April 22, 2016, 9:05am UTC](https://discuss.elastic.co/t/retrieve-document-where-certain-field-exists/48150/1 "2016-04-22T09:05:05Z")

</div>

Hi,

I'm trying to find documents in which certain field exist using POST. Here is the code snippet that I'm using. For example, I would like to find all the documents in which "activity\_name" field is present.

```
> {
> "query": {
> "filtered": {
> "query": {
> "bool": {
> "should": [
> {
> "query_string": {
> "query": "type:\"help-me\""
> }
> }
> ]
> }
> },
> "filter": {
> "bool": {
> "must": [
> {
> "exists": {
> "field": "activity_name"
> }            
> }
> ]
> }
> } 
> } 
> },
> "size": 5  
> }

```

The above code doesn't return any hits. Is this the right way to do it or am I missing something here?

Regards,  
Bharath

---

<div class="post-metadata">

### Author: ![bluepuma77](https://avatars.discourse-cdn.com/v4/letter/b/a9adbd/32.png) [@bluepuma77](https://discuss.elastic.co/u/bluepuma77)
#### Post date: [April 22, 2016, 12:38pm UTC](https://discuss.elastic.co/t/retrieve-document-where-certain-field-exists/48150/2 "2016-04-22T12:38:33Z")

</div>

Maybe simplify the filter

```
{
    query: {
        match_all: {}
    },
    filter : {
        exists : { "field" : "activity_name" }
    }
}
```

---

<div class="post-metadata">

### Author: ![bharath\_meka](https://avatars.discourse-cdn.com/v4/letter/b/858c86/32.png) [@bharath\_meka](https://discuss.elastic.co/u/bharath_meka)
#### Post date: [April 22, 2016, 12:56pm UTC](https://discuss.elastic.co/t/retrieve-document-where-certain-field-exists/48150/3 "2016-04-22T12:56:42Z")

</div>

But this would mean, it will find all the documents which has activity\_name field irrespective of the 'type' right?

---

<div class="post-metadata">

### Author: ![bluepuma77](https://avatars.discourse-cdn.com/v4/letter/b/a9adbd/32.png) [@bluepuma77](https://discuss.elastic.co/u/bluepuma77)
#### Post date: [April 23, 2016, 8:16pm UTC](https://discuss.elastic.co/t/retrieve-document-where-certain-field-exists/48150/4 "2016-04-23T20:16:57Z")

</div>

Yes, this was just an example for the filter, you can change the query part.

---

<div class="post-metadata">

### Author: ![bharath\_meka](https://avatars.discourse-cdn.com/v4/letter/b/858c86/32.png) [@bharath\_meka](https://discuss.elastic.co/u/bharath_meka)
#### Post date: [April 24, 2016, 1:21pm UTC](https://discuss.elastic.co/t/retrieve-document-where-certain-field-exists/48150/5 "2016-04-24T13:21:14Z")

</div>

It worked. 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 5, 2017, 10:56pm UTC](https://discuss.elastic.co/t/retrieve-document-where-certain-field-exists/48150/6 "2017-07-05T22:56:41Z")

</div>


