# Using field or field.keyword errors

**URL:** <https://discuss.elastic.co/t/using-field-or-field-keyword-errors/200928>\
**Category:** Elasticsearch\
**Created:** [September 24, 2019, 8:09pm UTC](https://discuss.elastic.co/t/using-field-or-field-keyword-errors/200928 "2019-09-24T20:09:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pmarques](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pmarques/32/42536_2.png) [@pmarques](https://discuss.elastic.co/u/pmarques)\
**Post date:** [September 24, 2019, 8:09pm UTC](https://discuss.elastic.co/t/using-field-or-field-keyword-errors/200928/1 "2019-09-24T20:09:30Z")

</div>

Hello all,

Could you help me decipher this? The issue is the use of field/field.keyword

Not sure where to start... Well ES 7.3

> Note:  
> There are some "-------------- \>\>\>" marks in this message.  
> I hope you find it

Documents look like:

```
{ "province": "British Columbia", "q_1": "Yes" }
{ "province": "British Columbia", "q_1": "No" }
{ "province": "British Columbia", "q_1": "Maybe" }
{ "province": "Alberta", "q_1": "Yes" }
{ "province": "Alberta", "q_1": "Maybe" }

```

The mapping is:

```
"province" : {
      "type" : "text",
      "fields" : {
        "keyword" : {
          "type" : "keyword",
          "ignore_above" : 256
        }
      }
    },
    "q_1" : {
      "type" : "text",
      "fields" : {
        "keyword" : {
          "type" : "keyword",
          "ignore_above" : 256
        }
      }

```

I created (using Kibana) a scripted field named "the\_sf"

And I tried this painless code:

> Map m = new HashMap();  
> m.put("Yes", 1);  
> m.put("No", 2);  
> m.put("Maybe", 3);  
> m.put("British Columbia", 10);  
> m.put("Alberta", 20);  
> **here\_ONE\_of\_the\_commands\_bellow**

> return m.get(doc['province.keyword'].value)

```
Preview results = 
[
 {
  "_id": "ct_jUG0BloL_9a5G0aRL",
  "the_sf": [
   10 ------------------------ >>> YEH !!!!
  ]
 },
(...)

```

> return m.get(doc['province.keyword'])

```
Preview results = 
[
 {
  "_id": "ct_jUG0BloL_9a5G0aRL",
  "the_sf": [
   null ------------------- >>> Makes sense, since doc['field'] is a pointer and not the value...
  ]
 },
(...)

```

> return m.get(doc['province'].value)

```
Preview results = 
{
 "root_cause": [
  {
   "type": "script_exception",
   "reason": "runtime error",
   "script_stack": [
  "org.elasticsearch.index.mapper.TextFieldMapper$TextFieldType.fielddataBuilder(TextFieldMapper.java:759)",
"org.elasticsearch.index.fielddata.IndexFieldDataService.getForField(IndexFieldDataService.java:116)",
"org.elasticsearch.index.query.QueryShardContext.lambda$lookup$0(QueryShardContext.java:290)",
"org.elasticsearch.search.lookup.LeafDocLookup$1.run(LeafDocLookup.java:101)",
"org.elasticsearch.search.lookup.LeafDocLookup$1.run(LeafDocLookup.java:98)",
"java.base/java.security.AccessController.doPrivileged(AccessController.java:310)",
"org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:98)",
"org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:41)",
"return m.get(doc['province'].value)", ------------------- >>> WHY ????
" ^---- HERE"
   ],
   "script": "Map m = new HashMap();\n\nm.put(\"Not yet\", 1);\nm.put('Less than once a month', 2);\nm.put('A few times a month', 3);\nm.put('About once a week', 4);\nm.put('A few times a week', 5);\nm.put('Most days or every day', 6);\nm.put(\"British Columbia\", 10);\n\nreturn m.get(doc['province'].value)",
   "lang": "painless"
  }
 ],
(...)

```

> return m.get(doc['province'])

```
Preview results =
The same as above, but
"return m.get(doc['province'])", ------------------- >>> WHY ??? Well, OK it is a pointer and not the value, but it should return null as before, don't?
" ^---- HERE"

```

The issue is that I tested also with the "q\_1" field and I got different results/errors

> return m.get(doc['q\_1.keyword'])

```
[
 {
  "_id": "ct_jUG0BloL_9a5G0aRL",
  "the_sf": [
   null ------------------- >>> Makes sense, since doc['field'] is a pointer and not the value...
 ]
}, (...)

```

> return m.get(doc['q\_1.keyword'].value)

{  
"root\_cause": [  
{  
"type": "script\_exception",  
"reason": "runtime error",  
"script\_stack": [  
"org.elasticsearch.index.fielddata.ScriptDocValues$Strings.get(ScriptDocValues.java:496)",  
"org.elasticsearch.index.fielddata.ScriptDocValues$Strings.getValue(ScriptDocValues.java:503)",  
"return m.get(doc['q\_1.keyword'].value)", ------------------- \>\>\> WHY ??? I know this message is big, but if you remember up there, it worked with doc['province.keyword'].value  
" ^---- HERE"  
], (...)

> return m.get(doc['q\_1'])  
> return m.get(doc['q\_1'].value)

```
Same errors as doc['province'] and doc['province'].value

```

Sorry for the huge message... I hope someone will be patient enough to read everything and answered it.

Cheers,  
Paulo

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [September 26, 2019, 12:30pm UTC](https://discuss.elastic.co/t/using-field-or-field-keyword-errors/200928/2 "2019-09-26T12:30:57Z")

</div>

There are a few things going on here.

When you access a field using the `doc['field']` notation you are accessing so called "[doc values](https://www.elastic.co/guide/en/elasticsearch/reference/master/modules-scripting-fields.html#modules-scripting-doc-vals)". Doc values are one of the many types of data structures that Lucene creates out of your documents when you index those. The thing with doc values for string fields is that they are only available for `keyword` fields and not for `text` fields.

This is why the following works:

```auto
return m.get(doc['province.keyword'].value)

```

but this fails:

```auto
return m.get(doc['province'].value)

```

Now, why does `return m.get(doc['q_1.keyword'].value)` fail? It's hard to say, but there should be a `reason` section in the error that Elasticsearch returns that explains it. I suspect that you have at least one document that does not have a `q_1` field. Try checking whether a document actually contains `q_1` values:

```auto
if (doc['q_1.keyword'].size() != 0) {
   return m.get(doc['q_1.keyword'].value)
}

```

---

<div class="post-metadata">

**Author:** ![pmarques](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pmarques/32/42536_2.png) [@pmarques](https://discuss.elastic.co/u/pmarques)\
**Post date:** [September 26, 2019, 7:33pm UTC](https://discuss.elastic.co/t/using-field-or-field-keyword-errors/200928/3 "2019-09-26T19:33:14Z")

</div>

Hi @abdon

Thank you so much for your answer. It is **true** that not every doc has the q\_1 field.

I tried this before.

```
if (!doc['q_1.keyword'].empty) {
   return m.get(doc['q_1.keyword'].value)
}

```

But it didn't work.

Thanks for the .size() tip. It worked well.

Cheers,  
Paulo

---

<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 24, 2019, 7:47pm UTC](https://discuss.elastic.co/t/using-field-or-field-keyword-errors/200928/4 "2019-10-24T19:47:01Z")

</div>

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