# Can I query a non-indexed field using a runtime field?

**URL:** <https://discuss.elastic.co/t/can-i-query-a-non-indexed-field-using-a-runtime-field/283786>\
**Category:** Elasticsearch\
**Tags:** runtime-fields\
**Created:** [September 9, 2021, 2:07pm UTC](https://discuss.elastic.co/t/can-i-query-a-non-indexed-field-using-a-runtime-field/283786 "2021-09-09T14:07:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![shipful](https://avatars.discourse-cdn.com/v4/letter/s/b19c9b/32.png) [@shipful](https://discuss.elastic.co/u/shipful)\
**Post date:** [September 9, 2021, 2:07pm UTC](https://discuss.elastic.co/t/can-i-query-a-non-indexed-field-using-a-runtime-field/283786/1 "2021-09-09T14:07:09Z")

</div>

If I create a mapping that doesn't index any fields:

```auto
PUT test-index/
{
  "mappings": {
   "dynamic": false
  }
}

```

and then add a document

```auto
POST test-index/_doc
{
  "a": "a",
  "b": "b",
  "c": "c"
}

```

Can I then use a dynamic field to query the \_source of the document? This is what I'm trying:

```auto
GET test-index/_search
{
  "runtime_mappings": {
    "runtime-a": {
      "type": "keyword",
      "script": {
        "source": "ctx._source.a"
      }
    }
  },
  
  "query": {
    "term": {
      "runtime-a": "a"
    }
  }
}

```

But it's returning the error:

```auto
"error" : {
    "root_cause" : [
      {
        "type" : "script_exception",
        "reason" : "compile error",
        "script_stack" : [
          "ctx._source.a",
          " ^---- HERE"
        ],
  ....

```

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [September 13, 2021, 2:49pm UTC](https://discuss.elastic.co/t/can-i-query-a-non-indexed-field-using-a-runtime-field/283786/2 "2021-09-13T14:49:11Z")

</div>

This is not the right so called script context to use. This should work:

```auto
POST test-index/_doc
{
  "a": "a",
  "b": "b",
  "c": "c"
}

GET test-index/_search
{
  "runtime_mappings": {
    "runtime-a": {
      "type": "keyword",
      "script": {
        "source": "emit(doc['a.keyword'].value)"
      }
    }
  },
  
  "query": {
    "term": {
      "runtime-a": "a"
    }
  }
}

```

Hope this helps!

---

<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:** [October 11, 2021, 2:50pm UTC](https://discuss.elastic.co/t/can-i-query-a-non-indexed-field-using-a-runtime-field/283786/3 "2021-10-11T14:50:07Z")

</div>

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