# How to write ES query: field exists AND has value

**URL:** <https://discuss.elastic.co/t/how-to-write-es-query-field-exists-and-has-value/86864>\
**Category:** Elasticsearch\
**Created:** [May 23, 2017, 9:06pm UTC](https://discuss.elastic.co/t/how-to-write-es-query-field-exists-and-has-value/86864 "2017-05-23T21:06:16Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![bricheet](https://avatars.discourse-cdn.com/v4/letter/b/ecd19e/32.png) [@bricheet](https://discuss.elastic.co/u/bricheet)\
**Post date:** [May 23, 2017, 9:06pm UTC](https://discuss.elastic.co/t/how-to-write-es-query-field-exists-and-has-value/86864/1 "2017-05-23T21:06:16Z")

</div>

I need to write an ElasticSearch query that finds documents where a certain field exists AND has a particular value. I've tried doing the following:

```
query = {
    'query': {
        'bool': {
            'must': [
                {'exists': {'field': 'A'}},
                {'term': {'A': 'value1'}}
            ]
        }
    }
}

es.search('my_index', body=query)

```

But this breaks for me with a TransportError.

How can I write this type of query?

---

<div class="post-metadata">

**Author:** ![xavierfacq](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/xavierfacq/32/8744_2.png) [@xavierfacq](https://discuss.elastic.co/u/xavierfacq)\
**Post date:** [May 24, 2017, 9:42am UTC](https://discuss.elastic.co/t/how-to-write-es-query-field-exists-and-has-value/86864/2 "2017-05-24T09:42:58Z")

</div>

hi,

What is you language ? Does you query works in Curl ? What is the TransportError ?

thx  
Xavier

---

<div class="post-metadata">

**Author:** ![bricheet](https://avatars.discourse-cdn.com/v4/letter/b/ecd19e/32.png) [@bricheet](https://discuss.elastic.co/u/bricheet)\
**Post date:** [May 24, 2017, 1:12pm UTC](https://discuss.elastic.co/t/how-to-write-es-query-field-exists-and-has-value/86864/3 "2017-05-24T13:12:01Z")

</div>

I'm writing in Python. I haven't figured out how to express this in any way. The TransportError is:

```
RequestError: TransportError(400, 'search_phase_execution_exception', 'failed to create query: 
...
```

---

<div class="post-metadata">

**Author:** ![xavierfacq](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/xavierfacq/32/8744_2.png) [@xavierfacq](https://discuss.elastic.co/u/xavierfacq)\
**Post date:** [May 24, 2017, 3:00pm UTC](https://discuss.elastic.co/t/how-to-write-es-query-field-exists-and-has-value/86864/4 "2017-05-24T15:00:22Z")

</div>

Does a simple call works :

res = es.search(index="my\_index", body={"query": {"match\_all": {}}})

---

<div class="post-metadata">

**Author:** ![bricheet](https://avatars.discourse-cdn.com/v4/letter/b/ecd19e/32.png) [@bricheet](https://discuss.elastic.co/u/bricheet)\
**Post date:** [May 24, 2017, 3:01pm UTC](https://discuss.elastic.co/t/how-to-write-es-query-field-exists-and-has-value/86864/5 "2017-05-24T15:01:36Z")

</div>

Yes, and I can do searches like:

```
query = {
    'query': {
        'bool': {
            'must': [
                {'exists': {'field': 'A'}},
            ]
        }
    }
}

```

I just can't figure out the syntax for "field must exist AND value must be X".

---

<div class="post-metadata">

**Author:** ![xavierfacq](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/xavierfacq/32/8744_2.png) [@xavierfacq](https://discuss.elastic.co/u/xavierfacq)\
**Post date:** [May 24, 2017, 7:38pm UTC](https://discuss.elastic.co/t/how-to-write-es-query-field-exists-and-has-value/86864/6 "2017-05-24T19:38:06Z")

</div>

Ok, is there error in the elasticsearch log too or only in your Python ? Can you past the full stacktrace if possible ?

---

<div class="post-metadata">

**Author:** ![bricheet](https://avatars.discourse-cdn.com/v4/letter/b/ecd19e/32.png) [@bricheet](https://discuss.elastic.co/u/bricheet)\
**Post date:** [May 26, 2017, 2:17pm UTC](https://discuss.elastic.co/t/how-to-write-es-query-field-exists-and-has-value/86864/7 "2017-05-26T14:17:15Z")

</div>

Turns out the issue was that field A was not indexed, so although 'exists' worked, any sort of term search failed.

---

<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:** [June 23, 2017, 2:17pm UTC](https://discuss.elastic.co/t/how-to-write-es-query-field-exists-and-has-value/86864/8 "2017-06-23T14:17:37Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
