Skip to content

Painless execute runtime context handles nested docs differently than search #92853

Open
@stu-elastic

Description

@stu-elastic

A runtime field in search cannot access a nested document inside a painless script, however the equivalent call in execute does access the nested document.

PUT /my-index-000001
{
  "mappings": {
    "properties": {
    "cause": { "type": "nested" }
    }
  }
}
POST /my-index-000001/_doc/
{
  "cause" : {
    "bytes" : 1024
  }
}
POST _scripts/painless/_execute
{
  "script": {
    "source": """
    def actual_bytes = 0.0;
    if (doc['cause.bytes'].size() != 0){
      actual_bytes = doc['cause.bytes'].value;
    }
    emit(actual_bytes);
    """
  },
  "context": "double_field",
  "context_setup": {
    "index": "my-index-000001",
    "document": {
      "cause": {
        "bytes": 1024
      }
    }
  }
}

{
  "result": [
    1024
  ]
}
GET /my-index-000001/_search
{
  "query": {
    "match_all": {}
  },
  "fields": [
    "causebytes"
  ],
  "runtime_mappings": {
    "causebytes": {
      "type": "double",
      "script": {
        "source": """
        def actual_bytes = 0.0;
        if (doc['cause.bytes'].size() != 0){
          actual_bytes = doc['cause.bytes'].value;
        }
        emit(actual_bytes);
        """
      }
    }
  }
}
Result
{
  "took": 6,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 1,
      "relation": "eq"
    },
    "max_score": 1,
    "hits": [
      {
        "_index": "my-index-000001",
        "_id": "LPykooUBSMPtAbVHu_QN",
        "_score": 1,
        "_source": {
          "cause": {
            "bytes": 1024
          }
        },
        "fields": {
          "causebytes": [
            0
          ],
          "cause": [
            {
              "ytes": [
                0
              ]
            }
          ]
        }
      }
    ]
  }
}

There also seems to be an issue with how hit.fields for the original document is rendered in this case, but that's unrelated to this issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions