Return Empty URL Scripted Field if fields do not exist

(Stephen C Wampler) #1

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

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 ''

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
         |           |

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?


(Tim Roes) #2


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.


(Stephen C Wampler) #3

Thanks for the quick response!

(system) #4

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