# Kibana Query - match and not eixsts on same field in a single query

**URL:** <https://discuss.elastic.co/t/kibana-query-match-and-not-eixsts-on-same-field-in-a-single-query/215765>\
**Category:** Elasticsearch\
**Created:** [January 20, 2020, 3:32pm UTC](https://discuss.elastic.co/t/kibana-query-match-and-not-eixsts-on-same-field-in-a-single-query/215765 "2020-01-20T15:32:43Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![suren-raymond](https://avatars.discourse-cdn.com/v4/letter/s/3be4f8/32.png) [@suren-raymond](https://discuss.elastic.co/u/suren-raymond)\
**Post date:** [January 20, 2020, 3:32pm UTC](https://discuss.elastic.co/t/kibana-query-match-and-not-eixsts-on-same-field-in-a-single-query/215765/1 "2020-01-20T15:32:43Z")

</div>

i've a index document field called "historicalData". Usually it has a value set [true,false]. But few documents of an index are not having the above said field "historicalData" at all. Wanted to query the documents with the below conditions. Please help out:

documents with historicalData='false' and no field "historicalData".

Index has 5 documents:  
2 documents -\> with historicalData='true'  
2 documents -\> with historicalData ='false'  
1 document -\> no field exists with historicalData

my query should return 3 documents.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [January 20, 2020, 3:49pm UTC](https://discuss.elastic.co/t/kibana-query-match-and-not-eixsts-on-same-field-in-a-single-query/215765/2 "2020-01-20T15:49:41Z")

</div>

Welcome!

What about a [bool query](https://www.elastic.co/guide/en/elasticsearch/reference/7.5/query-dsl-bool-query.html) which has a `must` clause with a term query on `historicalData` with value `false` and a `must_not` clause with an [exist query](https://www.elastic.co/guide/en/elasticsearch/reference/7.5/query-dsl-exists-query.html) on field `historicalData`?

---

<div class="post-metadata">

**Author:** ![suren-raymond](https://avatars.discourse-cdn.com/v4/letter/s/3be4f8/32.png) [@suren-raymond](https://discuss.elastic.co/u/suren-raymond)\
**Post date:** [January 21, 2020, 5:59am UTC](https://discuss.elastic.co/t/kibana-query-match-and-not-eixsts-on-same-field-in-a-single-query/215765/3 "2020-01-21T05:59:44Z")

</div>

thanks for the response. Used below query but no use: index name is enrl

GET /enrl/\_search  
{  
"query": {  
"bool": {  
"must": [  
{"term": {  
"FIELD": {  
"historicalData": "false"  
}  
}}  
],  
"must\_not": [  
{"exists": {  
"field": "historicalData"  
}}  
]  
}  
}  
}

output of the query is:  
{  
"error": {  
"root\_cause": [  
{  
"type": "parsing\_exception",  
"reason": "[term] query does not support [historicalData]",  
"line": 7,  
"col": 31  
}  
],  
"type": "parsing\_exception",  
"reason": "[term] query does not support [historicalData]",  
"line": 7,  
"col": 31  
},  
"status": 400  
}

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [January 21, 2020, 6:23am UTC](https://discuss.elastic.co/t/kibana-query-match-and-not-eixsts-on-same-field-in-a-single-query/215765/4 "2020-01-21T06:23:38Z")

</div>

Please don't post unformatted code, logs, or configuration as it's very hard to read.

Instead, paste the text and format it with \</\> icon or pairs of triple backticks (```), and check the preview window to make sure it's properly formatted before posting it. This makes it more likely that your question will receive a useful answer.

It would be great if you could update your post to solve this.

There is an example on how to write a Term Query in the documentation: [https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-term-query.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-term-query.html)

You need to fix that part.

If you still can't find the way to write it, could you provide **a full** recreation script as described in [About the Elasticsearch category](https://discuss.elastic.co/t/about-the-elasticsearch-category/21). It will help to better understand what you are doing. Please, try to keep the example as simple as possible.

A full reproduction script will help readers to understand, reproduce and if needed fix your problem. It will also most likely help to get a faster answer.

---

<div class="post-metadata">

**Author:** ![suren-raymond](https://avatars.discourse-cdn.com/v4/letter/s/3be4f8/32.png) [@suren-raymond](https://discuss.elastic.co/u/suren-raymond)\
**Post date:** [January 21, 2020, 7:30am UTC](https://discuss.elastic.co/t/kibana-query-match-and-not-eixsts-on-same-field-in-a-single-query/215765/5 "2020-01-21T07:30:52Z")

</div>

Sorry.. since this is first time post here, did not check the preview. Below is the code that i've used. and error i see is "parsing exception". Reason mentioned in the error as :"[term] query does not support [historicalData]"

```
GET /enrollment/_search
{
  "query": {
    "bool": {
      "must": [
        {"term": {
          "FIELD": {
            "historicalData": "false"
          }
        }}
      ],
      "must_not": [
        {"exists": {
          "field": "historicalData"
        }}
      ]
    }
  }
}
```

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [January 21, 2020, 7:39am UTC](https://discuss.elastic.co/t/kibana-query-match-and-not-eixsts-on-same-field-in-a-single-query/215765/6 "2020-01-21T07:39:33Z")

</div>

I answered you already about the term query issue. Please read the documentation I linked to. And...

> If you still can't find the way to write it, could you provide **a full** recreation script.

---

<div class="post-metadata">

**Author:** ![suren-raymond](https://avatars.discourse-cdn.com/v4/letter/s/3be4f8/32.png) [@suren-raymond](https://discuss.elastic.co/u/suren-raymond)\
**Post date:** [January 21, 2020, 9:10am UTC](https://discuss.elastic.co/t/kibana-query-match-and-not-eixsts-on-same-field-in-a-single-query/215765/7 "2020-01-21T09:10:31Z")

</div>

here you go. Thanks a lot for your prompt response.

```
POST enrl/_bulk
  {"index":{"_id":200}}
  {"name":"Espresso Machine","price":199,"in_stock":5,"historicalData":false}
  {"index":{"_id":201}}
  {"name":"Milk Frother","price":250,"in_stock":15,"historicalData":true}
  {"index":{"_id":202}}
  {"name":"Toaster","price":199,"in_stock":35,"historicalData":false}
  {"index":{"_id":203}}
  {"name":"Appetizer","price":200,"in_stock":25,"historicalData":true}
  {"index":{"_id":204}}
  {"name":"Mixer","price":300,"in_stock":5}

GET enrl/_search
{
  "query": {
    "bool": {
      "must": [
        {"term": {
          "historicalData": {
            "value": "false"
          }
        }}
      ],
      "must_not": [
        {"exists": {
          "field": "historicalData"
        }}
      ]
    }
  }
}
```

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [January 21, 2020, 9:25am UTC](https://discuss.elastic.co/t/kibana-query-match-and-not-eixsts-on-same-field-in-a-single-query/215765/8 "2020-01-21T09:25:41Z")

</div>

> [@suren-raymond](#):
>
> documents with historicalData='false' and no field "historicalData".

So I guess you wanted to write:

> documents with historicalData='false' **OR** no field "historicalData".

In which case, you can do:

```auto
DELETE enrl
POST enrl/_bulk
{"index":{"_id":200}}
{"name":"Espresso Machine","price":199,"in_stock":5,"historicalData":false}
{"index":{"_id":201}}
{"name":"Milk Frother","price":250,"in_stock":15,"historicalData":true}
{"index":{"_id":202}}
{"name":"Toaster","price":199,"in_stock":35,"historicalData":false}
{"index":{"_id":203}}
{"name":"Appetizer","price":200,"in_stock":25,"historicalData":true}
{"index":{"_id":204}}
{"name":"Mixer","price":300,"in_stock":5}

GET enrl/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "term": {
            "historicalData": {
              "value": false
            }
          }
        },
        {
          "bool": {
            "must_not": [
              {
                "exists": {
                  "field": "historicalData"
                }
              }
            ]
          }
        }
      ]
    }
  }
}

```

Which gives:

```auto
{
  "took" : 4,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 0.6931472,
    "hits" : [
      {
        "_index" : "enrl",
        "_type" : "_doc",
        "_id" : "200",
        "_score" : 0.6931472,
        "_source" : {
          "name" : "Espresso Machine",
          "price" : 199,
          "in_stock" : 5,
          "historicalData" : false
        }
      },
      {
        "_index" : "enrl",
        "_type" : "_doc",
        "_id" : "202",
        "_score" : 0.6931472,
        "_source" : {
          "name" : "Toaster",
          "price" : 199,
          "in_stock" : 35,
          "historicalData" : false
        }
      },
      {
        "_index" : "enrl",
        "_type" : "_doc",
        "_id" : "204",
        "_score" : 0.0,
        "_source" : {
          "name" : "Mixer",
          "price" : 300,
          "in_stock" : 5
        }
      }
    ]
  }
}

```

---

<div class="post-metadata">

**Author:** ![suren-raymond](https://avatars.discourse-cdn.com/v4/letter/s/3be4f8/32.png) [@suren-raymond](https://discuss.elastic.co/u/suren-raymond)\
**Post date:** [January 21, 2020, 9:28am UTC](https://discuss.elastic.co/t/kibana-query-match-and-not-eixsts-on-same-field-in-a-single-query/215765/9 "2020-01-21T09:28:12Z")

</div>

Thanks a lot.. you made my day!! that's it i wanted

---

<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:** [February 18, 2020, 9:28am UTC](https://discuss.elastic.co/t/kibana-query-match-and-not-eixsts-on-same-field-in-a-single-query/215765/10 "2020-02-18T09:28:24Z")

</div>

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