# Elasticsearch Query Multiple Must Nots

**URL:** <https://discuss.elastic.co/t/elasticsearch-query-multiple-must-nots/349570>\
**Category:** Elasticsearch\
**Created:** [December 18, 2023, 3:27pm UTC](https://discuss.elastic.co/t/elasticsearch-query-multiple-must-nots/349570 "2023-12-18T15:27:33Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Elk\_huh](https://avatars.discourse-cdn.com/v4/letter/e/d26b3c/32.png) [@Elk\_huh](https://discuss.elastic.co/u/Elk_huh)\
**Post date:** [December 18, 2023, 3:27pm UTC](https://discuss.elastic.co/t/elasticsearch-query-multiple-must-nots/349570/1 "2023-12-18T15:27:33Z")

</div>

Is it possible to have 2 different must not query strings across two different fields

I have this but it doesnt let me have 2 query strings

```auto

GET winevents/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "event.code": {
              "value": "4724"
            }
          }
        }
      ],
      "must_not": [
        {
          "query_string": {
            "query": "*SVC",
            "default_field": "winlog.event_data.SubjectUserName"
          }
        }
      ],
            "must_not": [
        {
          "query_string": {
            "query": "*testcacount",
            "default_field": "winlog.event_data.TargetUserName"
          }
        }
      ],
      "filter": [
        {
          "range": {
            "@timestamp": {
              "from": "now-7d"
            }
          }
        }
      ]
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [December 18, 2023, 3:33pm UTC](https://discuss.elastic.co/t/elasticsearch-query-multiple-must-nots/349570/2 "2023-12-18T15:33:54Z")

</div>

Hi @Elk_huh

> [@Elk\_huh](#):
>
> ```auto
> "must_not": [
> {
> "query_string": {
> "query": "*SVC",
> "default_field": "winlog.event_data.SubjectUserName"
> }
> },
> {
> "query_string": {
> "query": "*testcacount",
> "default_field": "winlog.event_data.TargetUserName"
> }
> }
> ],
> 
> ```

The `must_not` takes an array so put the two queries in the same must not separated by a comma.

---

<div class="post-metadata">

**Author:** ![Elk\_huh](https://avatars.discourse-cdn.com/v4/letter/e/d26b3c/32.png) [@Elk\_huh](https://discuss.elastic.co/u/Elk_huh)\
**Post date:** [December 18, 2023, 4:20pm UTC](https://discuss.elastic.co/t/elasticsearch-query-multiple-must-nots/349570/3 "2023-12-18T16:20:38Z")

</div>

Even though its two seperate fields that i am trying to do a query string

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [December 18, 2023, 4:25pm UTC](https://discuss.elastic.co/t/elasticsearch-query-multiple-must-nots/349570/4 "2023-12-18T16:25:07Z")

</div>

Yes, Give a try...The `must_not` takes an array of queries that must not be matched.

---

<div class="post-metadata">

**Author:** ![Elk\_huh](https://avatars.discourse-cdn.com/v4/letter/e/d26b3c/32.png) [@Elk\_huh](https://discuss.elastic.co/u/Elk_huh)\
**Post date:** [December 18, 2023, 5:13pm UTC](https://discuss.elastic.co/t/elasticsearch-query-multiple-must-nots/349570/5 "2023-12-18T17:13:07Z")

</div>

> [@stephenb](#):
>
> T

```auto
It doesnt liekit says duplicate query 
GET winevents/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "event.code": {
              "value": "4724"
            }
          }
        }
      ],
      "must_not": [
        {
          "query_string": {
            "query": "*SVC",
            "default_field": "FIELD1",
                        "query": "*testcacount",
            "default_field": "FIELD2"
          }
        }
      ],

      "filter": [
        {
          "range": {
            "@timestamp": {
              "from": "now-7d"
            }
          }
        }
      ]
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [December 18, 2023, 5:38pm UTC](https://discuss.elastic.co/t/elasticsearch-query-multiple-must-nots/349570/6 "2023-12-18T17:38:24Z")

</div>

Hi @Elk_huh

You did not follow my pattern.

Here is a sample working query

```auto
GET logs-*/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "term": {
            "kubernetes.namespace": {
              "value": "checkout"
            }
          }
        },
        {
          "term": {
            "kubernetes.namespace": {
              "value": "cart"
            }
          }
        }
      ]
    }
  }
}

```

So yours would look something like this as I showed before 2 separate query\_strings in the Must Not Array

```auto
...
"must_not": [
        {
          "query_string": {
            "query": "*SVC",
            "default_field": "winlog.event_data.SubjectUserName"
          }
        },
        {
          "query_string": {
            "query": "*testcacount",
            "default_field": "winlog.event_data.TargetUserName"
          }
        }
     ]
...

```

BTW Leading `*` queries are very expensive / not efficient

---

<div class="post-metadata">

**Author:** ![Elk\_huh](https://avatars.discourse-cdn.com/v4/letter/e/d26b3c/32.png) [@Elk\_huh](https://discuss.elastic.co/u/Elk_huh)\
**Post date:** [December 18, 2023, 7:12pm UTC](https://discuss.elastic.co/t/elasticsearch-query-multiple-must-nots/349570/7 "2023-12-18T19:12:25Z")

</div>

Right ive tried that also , it says duplicate query\_string, TERM1 and Term2 are two different fields  
i understand if it was 1 field  
it would be `"query" : "tesaccount OR *SVC"`  
` " default_field" : "winlog.event_data.TargeUserName"`

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [December 18, 2023, 7:28pm UTC](https://discuss.elastic.co/t/elasticsearch-query-multiple-must-nots/349570/8 "2023-12-18T19:28:37Z")

</div>

This worked fine for me

```auto
GET logs-*/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "query_string": {
            "query": "checkout",
            "default_field": "kubernetes.namespace"
          }
        },
        {
          "query_string": {
            "query": "cart",
            "default_field": "kubernetes.namespace"
          }
        }
      ]
    }
  }
}

```

Just substitute your values

Or better yet use term query

```auto
GET logs-*/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "term": {
            "winlog.event_data.SubjectUserName": {
              "value": "*SVC"
            }
          }
        },
        {
          "term": {
            "winlog.event_data.TargetUserName": {
              "value": "*testcacount"
            }
          }
        }
      ]
    }
  }
}

```

and again leading `*` is not really good

---

<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:** [January 15, 2024, 7:29pm UTC](https://discuss.elastic.co/t/elasticsearch-query-multiple-must-nots/349570/9 "2024-01-15T19:29:03Z")

</div>

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