# Replacing missing query

**URL:** https://discuss.elastic.co/t/replacing-missing-query/70652
**Category:** Elasticsearch
**Created:** [January 5, 2017, 9:20am UTC](https://discuss.elastic.co/t/replacing-missing-query/70652 "2017-01-05T09:20:35Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![pewpewlasers](https://avatars.discourse-cdn.com/v4/letter/p/e274bd/32.png) [@pewpewlasers](https://discuss.elastic.co/u/pewpewlasers)
#### Post date: [January 5, 2017, 9:20am UTC](https://discuss.elastic.co/t/replacing-missing-query/70652/1 "2017-01-05T09:20:35Z")

</div>

I recently updated to the newer version of Elasticsearch and realized that the missing query is _missing_. I understand that you could use must\_not with exists. But for my particular scenario it isn't working. I have the following configuration:

```
filtered = {
 function_score: {
      functions: [functions],
      query: {
           bool: {
                 must: [must have features],
                 must_not: [must not have features],
                 filter: filter,
            },
       },
  }
};

```

and the filter array looks like the following:

```
[
     ...
     {
           bool: {
                should: [
                     { missing: { field: "test" } },
                     {
                          script: {
                               script: "doc['id'].value == doc['test'].value"
                          }
                     },
                ]
           }
     }
     ...
]

```

Basically saying either field `test` should be missing or it should be equal to the id of the same document.

This was working fine but now I don't know how to replace this with `exists` query. I have tried the following:

```
[
     ...
     {
           bool: {
                should: [
                     {
                          bool: {
                               must_not: [
                                    { exists: {field: "test"} }
                               ]
                          }
                     },
                     {
                          script: {
                               script: "doc['id'].value == doc['test'].value"
                          }
                     },
                ]
           }
     }
     ...
]

```

This however completely ignores the script query and returns documents only if the `test` field is missing.

---

<div class="post-metadata">

### Author: ![cbuescher](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cbuescher/32/60402_2.png) [@cbuescher](https://discuss.elastic.co/u/cbuescher)
#### Post date: [January 5, 2017, 10:18am UTC](https://discuss.elastic.co/t/replacing-missing-query/70652/2 "2017-01-05T10:18:45Z")

</div>

Hi,

I tried to recreate your problem using Elasticsearch 5.1.1 and build a minimal example hoping to reproduce the issue. However, everything seems to work as expected for me when I do it like this:

```auto
PUT /i
{  
  "mappings": {
    "t": {
      "properties": {
        "id": {
          "type": "keyword"
        },
        "test" : {
          "type" : "keyword"
        }
      }
    }
  }
}

^ Note: adding the mapping because the script needs Fielddata, which is disabled on text fields by default

PUT /i/t/1
{
  "id" : "aaa",
  "test" : "aaa"
}

PUT /i/t/2
{
  "id" : "bbb"
}

PUT /i/t/3
{
  "id" : "ccc",
  "test" : "aaa"
}

GET /i/t/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "bool": {
            "should": [
              {
                "bool": {
                  "must_not": [
                    {
                      "exists": {
                        "field": "test"
                      }
                    }
                  ]
                }
              },
               {
                "script": {
                  "script": "doc['id'].value == doc['test'].value"
                }
              }
            ]
          }
        }
      ]
    }
  }
}

The Output is:

"hits": [
      {
        "_index": "i",
        "_type": "t",
        "_id": "2",
        "_score": 0,
        "_source": {
          "id": "bbb"
        }
      },
      {
        "_index": "i",
        "_type": "t",
        "_id": "1",
        "_score": 0,
        "_source": {
          "id": "aaa",
          "test": "aaa"
        }
      }
    ]

```

I think this is what you expected. I suspect there is something else going on here that is not related to the replacement of the "missing" query alone. I'd suggest taking out parts of your query independently and testing them on a very small dataset so debug this further.

---

<div class="post-metadata">

### Author: ![pewpewlasers](https://avatars.discourse-cdn.com/v4/letter/p/e274bd/32.png) [@pewpewlasers](https://discuss.elastic.co/u/pewpewlasers)
#### Post date: [January 7, 2017, 1:46pm UTC](https://discuss.elastic.co/t/replacing-missing-query/70652/3 "2017-01-07T13:46:32Z")

</div>

Hey! Thanks! The problem was the mapping. Earlier I tried `"script": "doc['id'].value == doc['test'].value"` and `"script": "doc['id'].keyword.value == doc['test'].keyword.value"` and both weren't working. This is solved after mapping the field correctly.

Without the mapping, the script query is ignored I believe.

---

<div class="post-metadata">

### Author: ![cbuescher](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cbuescher/32/60402_2.png) [@cbuescher](https://discuss.elastic.co/u/cbuescher)
#### Post date: [January 7, 2017, 2:01pm UTC](https://discuss.elastic.co/t/replacing-missing-query/70652/4 "2017-01-07T14:01:20Z")

</div>

Great to hear, just for reference and if others are reading this thread: which version of ES are you using? I got errors when trying to run the script part on a "text" field in 5.1.1.

---

<div class="post-metadata">

### Author: ![pewpewlasers](https://avatars.discourse-cdn.com/v4/letter/p/e274bd/32.png) [@pewpewlasers](https://discuss.elastic.co/u/pewpewlasers)
#### Post date: [January 7, 2017, 2:25pm UTC](https://discuss.elastic.co/t/replacing-missing-query/70652/5 "2017-01-07T14:25:34Z")

</div>

I am currently on 5.0.1. I don't get errors though. It is simply ignored and the bool\>must\_not\>exists query above it takes over.

---

<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 4, 2017, 2:26pm UTC](https://discuss.elastic.co/t/replacing-missing-query/70652/6 "2017-02-04T14:26:20Z")

</div>

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