Elasticsearch custom scoring script -- check if field exists?

elasticsearch

Solution

If this field has a numeric type, which includes dates that are indexed as long, the missing value is represented by 0.

Problem

I'd like check if a field exists in my custom scoring script when searching in elasticsearch. This search works: ``` { "query": { "custom_score": { "script": "((doc['region'].value == '1') ? 1 : 0)", "query": { "constant_score": { "filter": { "and": [ { "term": { "id": "100" } } ] } } } } }, "size": 1 } ``` It will score the result as 1 if the region field value is 1. However this query will not work: it will not score the document as 1 if the region field does not exist in the document: ``` { "query": { "custom_score": { "script": "((doc['region'].value == null) ? 1 : 0)", "query": { "constant_score": { "filter": { "and": [ { "term": { "id": "100" } } ] } } } } }, "size": 1 } ``` How do I check for missing values in a script?

Original source