# XOR operator in a Query String Query

**URL:** <https://discuss.elastic.co/t/xor-operator-in-a-query-string-query/373135>\
**Category:** Elasticsearch\
**Created:** [January 13, 2025, 2:57pm UTC](https://discuss.elastic.co/t/xor-operator-in-a-query-string-query/373135 "2025-01-13T14:57:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chenko](https://avatars.discourse-cdn.com/v4/letter/c/258eb7/32.png) [@Chenko](https://discuss.elastic.co/u/Chenko)\
**Post date:** [January 13, 2025, 2:57pm UTC](https://discuss.elastic.co/t/xor-operator-in-a-query-string-query/373135/1 "2025-01-13T14:57:54Z")

</div>

Hi,

First of all a happy 2025!

I was reading the [syntax](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html#query-string-syntax) documentation of the [Query String Query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html) and the [Simple](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-simple-query-string-query.html) one.

I noticed there was no XOR operator but there is OR AND and NOT.  
So I was wondering if it was possible to use the following instead for a XOR operation: `(quick OR fox) AND NOT (quick AND fox)`

Thanks!

---

<div class="post-metadata">

**Author:** ![Chenko](https://avatars.discourse-cdn.com/v4/letter/c/258eb7/32.png) [@Chenko](https://discuss.elastic.co/u/Chenko)\
**Post date:** [January 15, 2025, 7:46am UTC](https://discuss.elastic.co/t/xor-operator-in-a-query-string-query/373135/2 "2025-01-15T07:46:25Z")

</div>

I have tested this out, and it does seem to work as an XOR query which is nice!

```auto
POST xor-indx/_search
{
  "query": {
    "query_string": {
      "default_field": "title",
      "query": "(quick OR fox) AND NOT (quick AND fox)"
    }
  }
}

```

Only works with the normal query\_string though, not the simple\_query\_string.

(Tested on ES 8.15.0)

---

<div class="post-metadata">

**Author:** ![Chenko](https://avatars.discourse-cdn.com/v4/letter/c/258eb7/32.png) [@Chenko](https://discuss.elastic.co/u/Chenko)\
**Post date:** [May 26, 2025, 5:10am UTC](https://discuss.elastic.co/t/xor-operator-in-a-query-string-query/373135/3 "2025-05-26T05:10:12Z")

</div>

Update to this;  
Seems to work on simple\_query\_string as well, which is nice.

```auto
POST xor-indx/_search
{
  "query": {
    "query_string": {
      "default_field": "title",
      "query": "(quick | fox) +-(quick + fox)"
    }
  }
}

```

Though the syntax looks a bit more confusing than the normal query\_string.  
Tested on ES 8.18.1..
