# Scripted fields: text field returns null

**URL:** https://discuss.elastic.co/t/scripted-fields-text-field-returns-null/327178
**Category:** Kibana
**Tags:** painless
**Created:** [March 7, 2023, 12:46pm UTC](https://discuss.elastic.co/t/scripted-fields-text-field-returns-null/327178 "2023-03-07T12:46:23Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![RonGros](https://avatars.discourse-cdn.com/v4/letter/r/e9a140/32.png) [@RonGros](https://discuss.elastic.co/u/RonGros)
#### Post date: [March 7, 2023, 12:46pm UTC](https://discuss.elastic.co/t/scripted-fields-text-field-returns-null/327178/1 "2023-03-07T12:46:23Z")

</div>

Hi, I have an index with a lot of fields. I am trying to create a scripted field that will do some string manipulation on another field, but when I try to access it - I keep getting null  
What I've noticed is that if I try to access integers everything works perfectly, but when I try to access any text field, it returns null  
For example, taking these fields into account:

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/b/9/b91da6a7104d7b56c2ea8f899df44d9262ce7f5c.png)

if I do:  
`emit(doc['run_info.duration'].value.toString());`

everything works great  
but if I do:  
`emit(doc['run_info.test'].value.toString());`  
(with or without the toString()), it's empty. and that's true to all text vs integers.  
I tried copying the object JSON from "discover" and use it in the Painless debugger, but it all worked there, so I have no clue where to go next  
Any idea what I'm doing wrong?

---

<div class="post-metadata">

### Author: ![RonGros](https://avatars.discourse-cdn.com/v4/letter/r/e9a140/32.png) [@RonGros](https://discuss.elastic.co/u/RonGros)
#### Post date: [March 9, 2023, 7:25am UTC](https://discuss.elastic.co/t/scripted-fields-text-field-returns-null/327178/2 "2023-03-09T07:25:19Z")

</div>

No one? ☹

---

<div class="post-metadata">

### Author: ![Dzmitry](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dzmitry/32/65026_2.png) [@Dzmitry](https://discuss.elastic.co/u/Dzmitry)
#### Post date: [March 9, 2023, 3:12pm UTC](https://discuss.elastic.co/t/scripted-fields-text-field-returns-null/327178/3 "2023-03-09T15:12:58Z")

</div>

Hey @RonGros ,

Generally `doc['yourfield'].value` should be enough to get the value. Could you share the mappings for your index? It might help to better understand the issue.

Regards, DIma

---

<div class="post-metadata">

### Author: ![RonGros](https://avatars.discourse-cdn.com/v4/letter/r/e9a140/32.png) [@RonGros](https://discuss.elastic.co/u/RonGros)
#### Post date: [March 10, 2023, 1:03pm UTC](https://discuss.elastic.co/t/scripted-fields-text-field-returns-null/327178/4 "2023-03-10T13:03:09Z")

</div>

Hi @Dzmitry, thanks for answering. so the value doesn't work for text fields for me.  
Here is the mapping of this field:

```auto
"test": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }

```

We have a huge index so won't share all of it....

---

<div class="post-metadata">

### Author: ![stu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stu/32/75063_2.png) [@stu](https://discuss.elastic.co/u/stu)
#### Post date: [March 10, 2023, 4:06pm UTC](https://discuss.elastic.co/t/scripted-fields-text-field-returns-null/327178/5 "2023-03-10T16:06:28Z")

</div>

There are a couple of gotchas when working with text fields in painless.

Text fields are stored as a list of tokens in Lucene doc values, so this is what painless returns when you access them via `doc`. If that's what you want, then you'll need `"fielddata": true` for the text field. Without that setting, you will get an `illegal_argument_exception` when accessing a text field.

Typically, you'll want to use the `keyword` version of the field, as that will come back as the untokenized string.

However, if you have a long field and `"ignore_above": 256`, then the keyword field will not be available if the source data is longer than 256 characters.

Here's an example that demonstrates the difference between text and keyword fields.

```auto
PUT test
{
  "mappings": {
    "properties": {
      "test": {
        "type": "text",
        "fielddata": true,
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      }
    }
  }
}

PUT test/_doc/1
{
  "test": "abc 123 do re mi"
}

```

```auto
GET test/_search
{
  "runtime_mappings": {
    "mytext": {
      "type": "keyword",
      "script": {
        "source": "emit(doc['test'].value)"
      }
    },
    "mykeyword": {
      "type": "keyword",
      "script": {
        "source": "emit(doc['test.keyword'].value)"
      }
    }
  },
  "fields": [
    "mytext", "mykeyword"
  ]
}

```

Returns

```auto
    "hits" : [
      {
        "_index" : "test",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "test" : "abc 123 do re mi"
        },
        "fields" : {
          "mytext" : [
            "123"
          ],
          "mykeyword" : [
            "abc 123 do re mi"
          ]
        }
      }
    ]

```

I can't think of a case where `doc` would return null.

It would be easier to help with a minimal reproduction of the issue.

---

<div class="post-metadata">

### Author: ![RonGros](https://avatars.discourse-cdn.com/v4/letter/r/e9a140/32.png) [@RonGros](https://discuss.elastic.co/u/RonGros)
#### Post date: [March 12, 2023, 6:54am UTC](https://discuss.elastic.co/t/scripted-fields-text-field-returns-null/327178/6 "2023-03-12T06:54:03Z")

</div>

> [@stu](#):
>
> `fielddata`

Thanks @stu !  
I believe that the "fielddata" is the issue here. did not try it out, because reading about it, it sounds like it's going to cause performance issues (memory wise), but it means I need to find another solution. at least now I know 🙂

---

<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: [April 9, 2023, 6:54am UTC](https://discuss.elastic.co/t/scripted-fields-text-field-returns-null/327178/7 "2023-04-09T06:54:30Z")

</div>

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