Explain:true cause my painless script to stop working

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

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

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?

I'm on 7.3

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!

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