Get nested fields data with Painless scripting

I'm trying to access nested fields data through Painless inline scripting and can't seem to be able to get it to work.

Without getting into too many details, the reason for this is that we have certain fields that support a default value, and an optional custom override. To avoid having to return both fields and then have the application decide which one to use, I'd like to use a scripted field to check if the override has value and return that, or otherwise fallback to the default value. I've been able to get this working for for inner objects, but I'm not able to read the data I need for nested fields.

A sample setup is using data like:

"address": [ 
  {
    "city": "New York",
    "state": "NY"
  },
  {
    "city": "Westchester",
    "state": "NY"
  }
]

The index definition is:

{
  "mappings": {
    "my_type": {
      "properties": {
        "address": {
          "type": "nested",
          "properties": {
            "state": {
              "type": "text" 
            },
            "city": {
              "type": "text",
              "fields": {
                "raw": {
                  "type": "keyword",
                  "index": false
                }
              }              
            }
          }
        }
      }
    }
  }
}

When trying to read the city data as a scripted field for test, the response I get doesn't include it:

GET my_index/_search
{
  "script_fields": {
    "test": {
      "script":{
        "lang": "painless",
        "inline": "doc['address.city.raw']"
      }
    }
  }
}

This is the response:

  {
    "_index": "my_index",
    "_type": "my_type",
    "_id": "1",
    "_score": 1
  }

I see that it's referring to the correct field because if I use address.city, it complains that the field doesn't have fielddata enabled - hence why using .raw (keyword type).

Since address is a nested type with multiple values I assume that I need to somehow explicitly define which value I want to read, but constructs like "doc['address.0.city.raw']" or "doc['address'][0].city.raw", etc don't work.

Any insight is appreciated.

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