Return Empty URL Scripted Field if fields do not exist

I have a scripted field that generates a URL.

Example data:

{
  "field1": "foo",
  "field2": "bar",
  ... other fields ...
 }

Some records have values for field1/field2, others do not.

Scripted Field Info:
URL Template: https://{{rawValue}}
Script:

if ((doc['field1.keyword'].value != null) && (doc['field2.keyword'].value != null)) {
     // I pull field1/field2 out of `doc`, but for simplicity:
     return 'host/field1/field2'
} else {
    return 'url.does.not.exist.com/does/not/exist'
}

So currently, it generates a valid URL for documents that have field1 and field2, and generates a nonsensical URL for documents that don't have field1/field2.

So my results look like:

field1   | field 2   |scripted_field
--------------------------------------
a        | b         | https://host/a/b
         |           | https://url.does.not.exist.com/does/not/exist

What I would like is instead of generating nonsense, being able to return something that makes it not generate a URL at all:

field1   | field 2   |scripted_field
--------------------------------------
a        | b         | https://host/a/b
         |           | 

Any way that I can do this?

Thanks!

Hi,

unfortunately the URL field formatter currently will always output a URL even if the field is empty. Please feel free to open an enhancement request for that on GitHub.

Usually the best value to write in case of a non existing value is just returning null.

Cheers,
Tim

Thanks for the quick response!

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