# Elastic Search Query to match Only if Specific value is present

**URL:** <https://discuss.elastic.co/t/elastic-search-query-to-match-only-if-specific-value-is-present/305796>\
**Category:** Elasticsearch\
**Created:** [May 27, 2022, 11:45am UTC](https://discuss.elastic.co/t/elastic-search-query-to-match-only-if-specific-value-is-present/305796 "2022-05-27T11:45:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Harinder\_Singh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/harinder_singh/32/106262_2.png) [@Harinder\_Singh](https://discuss.elastic.co/u/Harinder_Singh)\
**Post date:** [May 27, 2022, 11:45am UTC](https://discuss.elastic.co/t/elastic-search-query-to-match-only-if-specific-value-is-present/305796/1 "2022-05-27T11:45:48Z")

</div>

Hi,

I have data like below

```auto
{
  "instance": {
    "items": [
      {
        "name": "name_1"
      },
      {
        "name": "name_2"
      }
    ]
  }
}

```

items is an array and it can contains many values.  
I am looking for a query to match only if

items.name = "name\_1" is specific to one value

**Doc 1**

```auto
{
  "instance": {
    "items": [
      {
        "name": "name_1"
      },
      {
        "name": "name_1"
      }
    ]
  }
}

```

**Doc 2**

```auto
{
  "instance": {
    "items": [
      {
        "name": "name_2"
      },
      {
        "name": "name_2"
      }
    ]
  }
}

```

**Doc 3**

```auto
{
  "instance": {
    "items": [
      {
        "name": "name_1"
      },
      {
        "name": "name_2"
      }
    ]
  }
}

```

example

1. if items.name = name\_1 , it should only return Doc\_1
2. if items.name = name\_2, it should only return Doc\_2
3. if items.name = name\_1, name\_2, then it should return all three Docs.

---

<div class="post-metadata">

**Author:** ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)\
**Post date:** [May 28, 2022, 11:28am UTC](https://discuss.elastic.co/t/elastic-search-query-to-match-only-if-specific-value-is-present/305796/2 "2022-05-28T11:28:08Z")

</div>

Hi,

As far as I know there is no native function to negate terms. Boolean query logic is for documents.

I suppose you need script or something to do that.

> <https://stackoverflow.com/questions/70526972/search-for-existence-of-a-value-other-than-query-in-array-field-in-elasticsearch>

---

<div class="post-metadata">

**Author:** ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)\
**Post date:** [May 28, 2022, 11:30am UTC](https://discuss.elastic.co/t/elastic-search-query-to-match-only-if-specific-value-is-present/305796/3 "2022-05-28T11:30:41Z")

</div>

This is a related post.

> [@How to find docs that contain the exact specified terms](https://discuss.elastic.co/t/how-to-find-docs-that-contain-the-exact-specified-terms/295758/13):
>
> Hi @Cholley_Alexandre , I found another solution. If you don't mind duplication of 'A' or 'B', GET /test\_runtime/\_search { "query":{ "bool": { "filter": [{"term": {"my-field": "A"}}, {"term": {"my-field": "B"}}], "must\_not": [{ "regexp": { "my-field": "~(A|B)" } }] } } } will return what you want. If you want to distinguish duplication or order, you may need custom fingerprint strategy as…

---

<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 25, 2022, 11:31am UTC](https://discuss.elastic.co/t/elastic-search-query-to-match-only-if-specific-value-is-present/305796/4 "2022-06-25T11:31:37Z")

</div>

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