[SOLVED] Issue with runtime field

If create a runtime field for searches with aggregations.

PUT /work-analyzers/_mapping
{
  "runtime": {
    "total_consumption": {
      "type": "double",
      "script": {
        "source": """
        emit(doc['pa1_w'].value + doc['pa2_w'].value + doc['pa3_w'].value)
        """
      }
    }
  }
}

Work fine but if anytime search when the search ever reaches a document that has (I assume) null values in all three fields that make up the runtime field, an exception is thrown.

I have looked for the output, but in all it fails, when executing the creation of the calculated field, and I can't find the topic doc[<field>].size()==0

"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!"
}

Search fails with size 100 (not with 10 or 30)

GET /work-analyzers/_search
{
  "size": 100,
  "query": {
    "range": {
      "total_consumption": {
        "gt": 700,
        "boost": 1
      }
    }
  },
  "_source": false,
  "fields": [
    {
      "field": "pa1_w"
    },
    {
      "field": "pa2_w"
    },
    {
      "field": "pa3_w"
    },
    {
      "field": "total_consumption"
    }
  ],
  "sort": [
    {
      "_doc": {
        "order": "asc"
      }
    }
  ],
  "track_total_hits": -1
}

Apreciate help.

Hi Abdelkarim,

The emit function doesn't cannot accept nulls, as per the docs.

I would recommend adding a null check for your values prior to emit. You can use either the null safe operator or and if statement:

  1. Null operator
  2. if statement
1 Like

Hi

I knew the issue apart from the fact that the error message makes it somewhat clear. What he didn't know was how to apply verification to the painles script. After some tests, I found it.

I get a solution but I do not like it very much.

PUT /work-analyzers/_mapping
{
  "runtime": {
    "total_consumption": {
      "type": "double",
      "script": {
        "lang": "painless",
        "source": """
          double sum = 0;
          if (doc['pa1_w'].size() == 0) { sum = sum + 0 } else { sum = sum + doc['pa1_w'].value}
          if (doc['pa2_w'].size() == 0) { sum = sum + 0 } else { sum = sum + doc['pa2_w'].value}
          if (doc['pa3_w'].size() == 0) { sum = sum + 0 } else { sum = sum + doc['pa3_w'].value}
          emit(sum);
        """
      }
    }
  }
}

I have tested it by going through the query in sections of 1000 docs and it does not fail.

Thanks for your reply.

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