# How to check for 1 of 2 fields to exist in record

**URL:** <https://discuss.elastic.co/t/how-to-check-for-1-of-2-fields-to-exist-in-record/185246>\
**Category:** Elasticsearch\
**Created:** [June 11, 2019, 5:19pm UTC](https://discuss.elastic.co/t/how-to-check-for-1-of-2-fields-to-exist-in-record/185246 "2019-06-11T17:19:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![cjokinen](https://avatars.discourse-cdn.com/v4/letter/c/cab0a1/32.png) [@cjokinen](https://discuss.elastic.co/u/cjokinen)\
**Post date:** [June 11, 2019, 5:19pm UTC](https://discuss.elastic.co/t/how-to-check-for-1-of-2-fields-to-exist-in-record/185246/1 "2019-06-11T17:19:12Z")

</div>

I am having an issue getting a result where 1 or 2 fields exist. either is fine but the best I can figure is how to require both using the below query for ES6.5.

```
{
  "version":"2017-02-28",
  "operation":"GET",
  "path":"/articlesnew1/json/_search",
  "params":{
    "body": {
      "size": 50,
      "query": {
        "bool": {
          "should" : [
            {
              "terms" : {
                "field_categories.__target_path" : ["he/taxonomy_term/categories/837","he/taxonomy_term/categories/840","he/taxonomy_term/categories/841","he/taxonomy_term/categories/842"]
              }
            },
            {
              "terms" : {
                "field_plays.__target_path" : ["he/taxonomy_term/plays/757","he/taxonomy_term/plays/758","he/taxonomy_term/plays/759","he/taxonomy_term/plays/761","he/taxonomy_term/plays/762"]
              }
            } 
          ],
          "must_not": [
            {
              "term": { 
              	"field_exclude_from_syndication.value": 1 
              }
            }
          ],
          "must": [
            {
              "bool": {
                "should" : [{
                  "exists": {
                    "field" : "field_categories.__target_path"
                  },
                  "exists": {
                    "field" : "field_plays.__target_path"
                  }
                }]
              }
            }
          ],
          "filter": [
            {
              "terms": {
                  "entity.bundle" : ["he_activity_highlight","he_company","he_event","he_exclusive","he_industry_voice","he_new_financing","he_news"]
               }
            },
            {
              "range" : {
                "entity.changed.timestamp" : {
                  "gte": "${context.arguments.from}",
                  "lte": "${context.arguments.to}"
                }
              }
            }
          ]
        }
      }
    }
  }
 }

```

My records may have values like this

```
"field_categories": [
     {
         "__target_path": "he/taxonomy_term/categories/878"
     }
 ],

 "field_plays": [
     {
         "__target_path": "he/taxonomy_term/plays/776"
     }
 ],

```

But not all data has both some have one some have the other. I need to return them as well as records that have both values present.

Ideas?

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [June 11, 2019, 6:22pm UTC](https://discuss.elastic.co/t/how-to-check-for-1-of-2-fields-to-exist-in-record/185246/2 "2019-06-11T18:22:32Z")

</div>

You are missing a couple of `{` `}` in the should clause around `exists`:

```auto
              [
                {
                  "exists": {
                    "field" : "field_categories.__target_path"
                  },
                  "exists": {
                    "field" : "field_plays.__target_path"
                  }
                }
              ]

```

I think it should be

```auto
              [
                {
                  "exists": {
                    "field" : "field_categories.__target_path"
                  }
                },
                {
                  "exists": {
                    "field" : "field_plays.__target_path"
                  }
                }
              ]

```

---

<div class="post-metadata">

**Author:** ![cjokinen](https://avatars.discourse-cdn.com/v4/letter/c/cab0a1/32.png) [@cjokinen](https://discuss.elastic.co/u/cjokinen)\
**Post date:** [June 11, 2019, 6:37pm UTC](https://discuss.elastic.co/t/how-to-check-for-1-of-2-fields-to-exist-in-record/185246/3 "2019-06-11T18:37:37Z")

</div>

Great eye!. that did it!

---

<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 9, 2019, 6:37pm UTC](https://discuss.elastic.co/t/how-to-check-for-1-of-2-fields-to-exist-in-record/185246/4 "2019-07-09T18:37:39Z")

</div>

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