# Explain:true cause my painless script to stop working

**URL:** <https://discuss.elastic.co/t/explain-true-cause-my-painless-script-to-stop-working/203733>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [October 16, 2019, 3:09am UTC](https://discuss.elastic.co/t/explain-true-cause-my-painless-script-to-stop-working/203733 "2019-10-16T03:09:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rick\_Choo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rick_choo/32/55965_2.png) [@Rick\_Choo](https://discuss.elastic.co/u/Rick_Choo)\
**Post date:** [October 16, 2019, 3:09am UTC](https://discuss.elastic.co/t/explain-true-cause-my-painless-script-to-stop-working/203733/1 "2019-10-16T03:09:59Z")

</div>

resume mapping

```
PUT resume
{
  "mappings":{
    "properties":{
      "job_experience":{
        "type": "nested",
        "properties": {
          "company": {
            "type": "text",
            "fielddata":true
          }
          "end_date": {
            "type":"date",
            "format":"MM-yyyy"
          },
          "start_date": {
            "type": "date",
            "format": "MM-yyyy"
          }
        }
      }
    }
  }
}

```

Resume entry

```
{

      "name" : "HOLLY CONNOR",
      "job_experience" : [
        {
          "company" : "lumiminds",
          "start_date" : "01-2015",
          "end_date" : "01-2019"
        },
        {
          "company" : "asus",
          "start_date" : "01-2010",
          "end_date" : "12-2014"
        }
      ]
}

```

Query with script to check if the working experience in a particular company is more than 4 years

```
POST /resume/_search
{
  "query": {
    "nested": {
      "path": "job_experience",
      "query": {
        "bool": {
          "must": [
            {
             "match_phrase":{"job_experience.company":"lumiminds"}
            },
            {
              "script":{
                "script":" return((doc['job_experience.end_date'].value.year - doc['job_experience.start_date'].value.year) >=4)"
              }
            }
          ]
        }
      }
    }
  }
}

```

The above script is working fine until I added "explain":true, to the query

```
POST /resume/_search
    {
      "explain":true,
      "query": {
        "nested": {
          "path": "job_experience",
          "query": {
            "bool": {
              "must": [
                {
                 "match_phrase":{"job_experience.company":"lumiminds"}
                },
                {
                  "script":{
                    "script":" return((doc['job_experience.end_date'].value.year - doc['job_experience.start_date'].value.year) >=4)"
                  }
                }
              ]
            }
          }
        }
      }
    }

```

The Error that I received is

```
{
  "error": {
    "root_cause": [
      {
        "type": "script_exception",
        "reason": "runtime error",
        "script_stack": [
          "org.elasticsearch.index.fielddata.ScriptDocValues$Dates.get(ScriptDocValues.java:160)",
          "org.elasticsearch.index.fielddata.ScriptDocValues$Dates.getValue(ScriptDocValues.java:154)",
          "return((doc['job_experience.end_date'].value.year - doc['job_experience.start_date'].value.year) >=4)",
          " ^---- HERE"
        ],
        "script": " return((doc['job_experience.end_date'].value.year - doc['job_experience.start_date'].value.year) >=4)",
        "lang": "painless"
      }
    ],
    "type": "search_phase_execution_exception",
    "reason": "all shards failed",
    "phase": "query",
    "grouped": true,
    "failed_shards": [
      {
        "shard": 0,
        "index": "resume",
        "node": "qv8i6kpiRmKVGQ3-uvNBOA",
        "reason": {
          "type": "script_exception",
          "reason": "runtime error",
          "script_stack": [
            "org.elasticsearch.index.fielddata.ScriptDocValues$Dates.get(ScriptDocValues.java:160)",
            "org.elasticsearch.index.fielddata.ScriptDocValues$Dates.getValue(ScriptDocValues.java:154)",
            "return((doc['job_experience.end_date'].value.year - doc['job_experience.start_date'].value.year) >=4)",
            " ^---- HERE"
          ],
          "script": " return((doc['job_experience.end_date'].value.year - doc['job_experience.start_date'].value.year) >=4)",
          "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!"
          }
        }
      }
    ]
  },
  "status": 400
}

```

Please help me with the issue. I need to use explain because the full query that i needed is quite complex and I need to explain which search terms matches. Need to get explain and painless script working together.

Thanks

---

<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:** [October 16, 2019, 1:08pm UTC](https://discuss.elastic.co/t/explain-true-cause-my-painless-script-to-stop-working/203733/2 "2019-10-16T13:08:29Z")

</div>

I just tested this under 7.4.0 and it worked for me using this snippet

```auto
PUT resume
{
  "mappings":{
    "properties":{
      "job_experience":{
        "type": "nested",
        "properties": {
          "company": {
            "type": "text",
            "fielddata":true
          },
          "end_date": {
            "type":"date",
            "format":"MM-yyyy"
          },
          "start_date": {
            "type": "date",
            "format": "MM-yyyy"
          }
        }
      }
    }
  }
}

PUT resume/_doc/1
{

      "name" : "HOLLY CONNOR",
      "job_experience" : [
        {
          "company" : "lumiminds",
          "start_date" : "01-2015",
          "end_date" : "01-2019"
        },
        {
          "company" : "asus",
          "start_date" : "01-2010",
          "end_date" : "12-2014"
        }
      ]
}

POST /resume/_search
{
  "query": {
    "nested": {
      "path": "job_experience",
      "query": {
        "bool": {
          "must": [
            {
              "match_phrase": {
                "job_experience.company": "lumiminds"
              }
            },
            {
              "script": {
                "script": " return((doc['job_experience.end_date'].value.year - doc['job_experience.start_date'].value.year) >=4)"
              }
            }
          ]
        }
      }
    }
  }
}

POST /resume/_search
{
  "explain": true, 
  "query": {
    "nested": {
      "path": "job_experience",
      "query": {
        "bool": {
          "must": [
            {
              "match_phrase": {
                "job_experience.company": "lumiminds"
              }
            },
            {
              "script": {
                "script": " return((doc['job_experience.end_date'].value.year - doc['job_experience.start_date'].value.year) >=4)"
              }
            }
          ]
        }
      }
    }
  }
}

```

what version are you on?

---

<div class="post-metadata">

**Author:** ![Rick\_Choo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rick_choo/32/55965_2.png) [@Rick\_Choo](https://discuss.elastic.co/u/Rick_Choo)\
**Post date:** [October 17, 2019, 3:27am UTC](https://discuss.elastic.co/t/explain-true-cause-my-painless-script-to-stop-working/203733/3 "2019-10-17T03:27:53Z")

</div>

I'm on 7.3

---

<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:** [October 21, 2019, 7:36am UTC](https://discuss.elastic.co/t/explain-true-cause-my-painless-script-to-stop-working/203733/4 "2019-10-21T07:36:54Z")

</div>

I tried on 7.3.1 and it worked as well for me. Is it possible, that my sample is not reflecting your setup. Can you investigate on your side and see if you come up with a fully reproducible example on the latest elasticsearch version? Thank you!

---

<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:** [November 18, 2019, 7:36am UTC](https://discuss.elastic.co/t/explain-true-cause-my-painless-script-to-stop-working/203733/5 "2019-11-18T07:36:55Z")

</div>

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