# Create scripted field error

**URL:** <https://discuss.elastic.co/t/create-scripted-field-error/236471>\
**Category:** Kibana\
**Created:** [June 10, 2020, 9:19am UTC](https://discuss.elastic.co/t/create-scripted-field-error/236471 "2020-06-10T09:19:20Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![inchirah](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/inchirah/32/48145_2.png) [@inchirah](https://discuss.elastic.co/u/inchirah)\
**Post date:** [June 10, 2020, 9:19am UTC](https://discuss.elastic.co/t/create-scripted-field-error/236471/1 "2020-06-10T09:19:20Z")

</div>

Hello,

I created a script field "attempt complaint" which takes the values \>= -78 from another field "Contrainte"  
 ![script](https://us1.discourse-cdn.com/elastic/original/3X/d/d/dd65f45cf125a8bd1b8ccc25fa56c36ba6114f5b.png)

Can anyone tell how I can fixe that?

---

<div class="post-metadata">

**Author:** ![mikecote](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mikecote/32/58549_2.png) [@mikecote](https://discuss.elastic.co/u/mikecote)\
**Post date:** [June 10, 2020, 12:11pm UTC](https://discuss.elastic.co/t/create-scripted-field-error/236471/2 "2020-06-10T12:11:54Z")

</div>

Hi @inchirah,

The script is failing because on the first line you have `=!` instead of `!=`.

---

<div class="post-metadata">

**Author:** ![inchirah](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/inchirah/32/48145_2.png) [@inchirah](https://discuss.elastic.co/u/inchirah)\
**Post date:** [June 10, 2020, 12:33pm UTC](https://discuss.elastic.co/t/create-scripted-field-error/236471/3 "2020-06-10T12:33:20Z")

</div>

Thank for your answer, but I still get the same error

---

<div class="post-metadata">

**Author:** ![mikecote](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mikecote/32/58549_2.png) [@mikecote](https://discuss.elastic.co/u/mikecote)\
**Post date:** [June 10, 2020, 12:47pm UTC](https://discuss.elastic.co/t/create-scripted-field-error/236471/4 "2020-06-10T12:47:59Z")

</div>

What does Kibana display when you click "Get help with the syntax and preview the results of your script" and then "Preview results". It will show the underlying error there.

---

<div class="post-metadata">

**Author:** ![inchirah](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/inchirah/32/48145_2.png) [@inchirah](https://discuss.elastic.co/u/inchirah)\
**Post date:** [June 10, 2020, 12:54pm UTC](https://discuss.elastic.co/t/create-scripted-field-error/236471/5 "2020-06-10T12:54:35Z")

</div>

Here is the error:

```auto
{
 "root_cause": [
  {
   "type": "script_exception",
   "reason": "runtime error",
   "script_stack": [
    "org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:94)",
    "org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:41)",
    "if (doc['Contrainte'].value != null) { \r\n ",
    " ^---- HERE"
   ],
   "script": "if (doc['Contrainte'].value != null) { \r\n if (doc['Contrainte'].value >= -78){\r\n return\"value\";\r\n }\r\n \r\n}\r\nelse { return null }",
   "lang": "painless"
  }
 ],
 "type": "search_phase_execution_exception",
 "reason": "all shards failed",
 "phase": "query",
 "grouped": true,
 "failed_shards": [
  {
   "shard": 0,
   "index": "moov_dogbo_2g_test",
   "node": "5efdWBMvQiaV5SfQDCpnIw",
   "reason": {
    "type": "script_exception",
    "reason": "runtime error",
    "script_stack": [
     "org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:94)",
     "org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:41)",
     "if (doc['Contrainte'].value != null) { \r\n ",
     " ^---- HERE"
    ],
    "script": "if (doc['Contrainte'].value != null) { \r\n if (doc['Contrainte'].value >= -78){\r\n return\"value\";\r\n }\r\n \r\n}\r\nelse { return null }",
    "lang": "painless",
    "caused_by": {
     "type": "illegal_argument_exception",
     "reason": "No field found for [Contrainte] in mapping with types []"
    }
   }
  }
 ]
}

```

---

<div class="post-metadata">

**Author:** ![mikecote](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mikecote/32/58549_2.png) [@mikecote](https://discuss.elastic.co/u/mikecote)\
**Post date:** [June 10, 2020, 12:59pm UTC](https://discuss.elastic.co/t/create-scripted-field-error/236471/6 "2020-06-10T12:59:24Z")

</div>

It looks like it's complaining about the field `Contrainte` not existing (`No field found for [Contrainte] in mapping with types []`). What is the mapping for that field?

---

<div class="post-metadata">

**Author:** ![inchirah](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/inchirah/32/48145_2.png) [@inchirah](https://discuss.elastic.co/u/inchirah)\
**Post date:** [June 10, 2020, 1:12pm UTC](https://discuss.elastic.co/t/create-scripted-field-error/236471/7 "2020-06-10T13:12:56Z")

</div>

Sorry I put the wrong error  
Here is the error related to that script and here is the mappin of my index

```auto
{
 "root_cause": [
  {
   "type": "script_exception",
   "reason": "runtime error",
   "script_stack": [
    "org.elasticsearch.index.fielddata.ScriptDocValues$Doubles.get(ScriptDocValues.java:249)",
    "org.elasticsearch.index.fielddata.ScriptDocValues$Doubles.getValue(ScriptDocValues.java:243)",
    "if(doc['Contrainte'].value != null){\n ",
    " ^---- HERE"
   ],
   "script": "if(doc['Contrainte'].value != null){\n if (doc['Contrainte'].value >= -78){\n return \"value\"\n }\n}\nelse {return null}",
   "lang": "painless"
  }
 ],
 "type": "search_phase_execution_exception",
 "reason": "all shards failed",
 "phase": "query",
 "grouped": true,
 "failed_shards": [
  {
   "shard": 0,
   "index": "couverture_2g",
   "node": "4nrIqQuNTuGPNsups5bdzQ",
   "reason": {
    "type": "script_exception",
    "reason": "runtime error",
    "script_stack": [
     "org.elasticsearch.index.fielddata.ScriptDocValues$Doubles.get(ScriptDocValues.java:249)",
     "org.elasticsearch.index.fielddata.ScriptDocValues$Doubles.getValue(ScriptDocValues.java:243)",
     "if(doc['Contrainte'].value != null){\n ",
     " ^---- HERE"
    ],
    "script": "if(doc['Contrainte'].value != null){\n if (doc['Contrainte'].value >= -78){\n return \"value\"\n }\n}\nelse {return null}",
    "lang": "painless",
    "caused_by": {
     "type": "illegal_state_exception",
     "reason": "A document doesn't have a value for a field! Use doc[<field>].size()==0 to check if a document is missing a field!"
    }
   }
  }
 ]
}

```

```auto
{
  "mapping": {
    "properties": {
      "@timestamp": {
        "type": "date"
      },
      "@version": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "Contrainte": {
        "type": "float"
      },
      "Distance": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "Latitude": {
        "type": "float"
      },
      "Longitude": {
        "type": "float"
      },
      "Message": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "Time": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "host": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "location": {
        "type": "float"
      },
      "message": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "path": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      }
    }
  }
}

```

I really apperciate your help.

---

<div class="post-metadata">

**Author:** ![mikecote](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mikecote/32/58549_2.png) [@mikecote](https://discuss.elastic.co/u/mikecote)\
**Post date:** [June 10, 2020, 1:21pm UTC](https://discuss.elastic.co/t/create-scripted-field-error/236471/8 "2020-06-10T13:21:38Z")

</div>

Not a problem, I'm seeing `A document doesn't have a value for a field! Use doc[<field>].size()==0 to check if a document is missing a field!`. I believe instead of checking for `null`, you can replace the whole `if` condition with `doc[<field>].size()>0` or a combination of the two.

---

<div class="post-metadata">

**Author:** ![inchirah](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/inchirah/32/48145_2.png) [@inchirah](https://discuss.elastic.co/u/inchirah)\
**Post date:** [June 10, 2020, 1:35pm UTC](https://discuss.elastic.co/t/create-scripted-field-error/236471/9 "2020-06-10T13:35:10Z")

</div>

Unfortunatly your solution didn't work either .  
Could you think of something else to help me and I wonder if the word "doc" is a keyword used with all indices.

---

<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:** [July 8, 2020, 1:35pm UTC](https://discuss.elastic.co/t/create-scripted-field-error/236471/10 "2020-07-08T13:35:25Z")

</div>

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