elasticsearchnestedfieldkibanakibana-6

Defining scripted field based on nested object Array


I have index mapping like this:

{
    "mappings": {
        "session": {
            "dynamic_templates": [
                {
                    "string_fields": {
                        "match": "*",
                        "match_mapping_type": "string",
                        "mapping": {
                            "norms": "false",
                            "type": "keyword"
                        }
                    }
                }
            ],
            "properties": {
                "applicationType": {
                    "type": "keyword"
                },
                "appVersion": {
                    "type": "keyword"
                },
                "bounce": {
                    "type": "boolean"
                },
                "browserFamily": {
                    "type": "keyword"
                },
                "browserMajorVersion": {
                    "type": "keyword"
                },
                "browserMonitorId": {
                    "type": "keyword"
                },
                "browserMonitorName": {
                    "type": "keyword"
                },
                "browserType": {
                    "type": "keyword"
                },
                "carrier": {
                    "type": "keyword"
                },
                "city": {
                    "type": "keyword"
                },
                "clientTimeOffset": {
                    "type": "integer"
                },
                "clientType": {
                    "type": "keyword"
                },
                "continent": {
                    "type": "keyword"
                },
                "connectionType": {
                    "type": "keyword"
                },
                "country": {
                    "type": "keyword"
                },
                "dateProperties": {
                    "type": "nested",
                    "properties": {
                        "value": {
                            "type": "date"
                        }
                    }
                },
                "device": {
                    "type": "keyword"
                },
                "displayResolution": {
                    "type": "keyword"
                },
                "doubleProperties": {
                    "type": "nested",
                    "properties": {
                        "value": {
                            "type": "double"
                        }
                    }
                },
                "duration": {
                    "type": "integer"
                },
                "endReason": {
                    "type": "keyword"
                },
                "endTime": {
                    "type": "date"
                },
                "events": {
                    "type": "nested",
                    "properties": {
                        "application": {
                            "type": "keyword"
                        },
                        "internalApplicationId": {
                            "type": "keyword"
                        },
                        "name": {
                            "type": "keyword"
                        },
                        "startTime": {
                            "type": "date"
                        },
                        "type": {
                            "type": "keyword"
                        }
                    }
                },
                "errors": {
                    "type": "nested",
                    "properties": {
                        "application": {
                            "type": "keyword"
                        },
                        "internalApplicationId": {
                            "type": "keyword"
                        },
                        "name": {
                            "type": "keyword"
                        },
                        "startTime": {
                            "type": "date"
                        },
                        "type": {
                            "type": "keyword"
                        }
                    }
                },
                "hasCrash": {
                    "type": "boolean"
                },
                "hasSessionReplay": {
                    "type": "boolean"
                },
                "internalUserId": {
                    "type": "keyword"
                },
                "ip": {
                    "type": "ip"
                },
                "isp": {
                    "type": "text"
                },
                "longProperties": {
                    "type": "nested",
                    "properties": {
                        "value": {
                            "type": "long"
                        }
                    }
                },
                "manufacturer": {
                    "type": "keyword"
                },
                "matchingConversionGoals": {
                    "type": "keyword"
                },
                "networkTechnology": {
                    "type": "keyword"
                },
                "newUser": {
                    "type": "boolean"
                },
                "numberOfRageClicks": {
                    "type": "integer"
                },
                "osFamily": {
                    "type": "keyword"
                },
                "osVersion": {
                    "type": "keyword"
                },
                "region": {
                    "type": "keyword"
                },
                "replayStart": {
                    "type": "date"
                },
                "replayEnd": {
                    "type": "date"
                },
                "screenHeight": {
                    "type": "integer"
                },
                "screenOrientation": {
                    "type": "keyword"
                },
                "screenWidth": {
                    "type": "integer"
                },
                "startTime": {
                    "type": "date"
                },
                "stringProperties": {
                    "type": "nested"
                },
                "syntheticEvents": {
                    "type": "nested",
                    "properties": {
                        "errorCode": {
                            "type": "short"
                        },
                        "errorName": {
                            "type": "keyword"
                        },
                        "name": {
                            "type": "keyword"
                        },
                        "sequenceNumber": {
                            "type": "integer"
                        },
                        "syntheticEventId": {
                            "type": "keyword"
                        },
                        "timestamp": {
                            "type": "date"
                        },
                        "type": {
                            "type": "keyword"
                        }
                    }
                },
                "tenantId": {
                    "type": "keyword"
                },
                "totalErrorCount": {
                    "type": "integer"
                },
                "totalLicenceCreditCount": {
                    "type": "integer"
                },
                "userActionCount": {
                    "type": "integer"
                },
                "userActions": {
                    "type": "nested",
                    "properties": {
                        "apdexCategory": {
                            "type": "keyword"
                        },
                        "application": {
                            "type": "keyword"
                        },
                        "cdnResources": {
                            "type": "integer"
                        },
                        "cdnBusyTime": {
                            "type": "integer"
                        },
                        "documentInteractiveTime": {
                            "type": "integer"
                        },
                        "domCompleteTime": {
                            "type": "long"
                        },
                        "domContentLoadedTime": {
                            "type": "long"
                        },
                        "domain": {
                            "type": "keyword"
                        },
                        "duration": {
                            "type": "integer"
                        },
                        "endTime": {
                            "type": "date"
                        },
                        "errorCount": {
                            "type": "integer"
                        },
                        "failedImages": {
                            "type": "integer"
                        },
                        "failedXhrRequests": {
                            "type": "integer"
                        },
                        "firstPartyBusyTime": {
                            "type": "integer"
                        },
                        "firstPartyResources": {
                            "type": "integer"
                        },
                        "frontendTime": {
                            "type": "integer"
                        },
                        "hasCrash": {
                            "type": "boolean"
                        },
                        "httpRequestsWithErrors": {
                            "type": "integer"
                        },
                        "internalApplicationId": {
                            "type": "keyword"
                        },
                        "internalKeyUserActionId": {
                            "type": "keyword"
                        },
                        "keyUserAction": {
                            "type": "boolean"
                        },
                        "loadEventEnd": {
                            "type": "double"
                        },
                        "loadEventStart": {
                            "type": "double"
                        },
                        "name": {
                            "type": "keyword"
                        },
                        "navigationStart": {
                            "type": "long"
                        },
                        "networkTime": {
                            "type": "integer"
                        },
                        "requestStart": {
                            "type": "long"
                        },
                        "responseEnd": {
                            "type": "long"
                        },
                        "responseStart": {
                            "type": "long"
                        },
                        "serverTime": {
                            "type": "integer"
                        },
                        "speedIndex": {
                            "type": "long"
                        },
                        "startTime": {
                            "type": "date"
                        },
                        "syntheticEvent": {
                            "type": "keyword"
                        },
                        "syntheticEventId": {
                            "type": "keyword"
                        },
                        "targetUrl": {
                            "type": "keyword"
                        },
                        "thirdPartyBusyTime": {
                            "type": "integer"
                        },
                        "thirdPartyResources": {
                            "type": "integer"
                        },
                        "type": {
                            "type": "keyword"
                        },
                        "visuallyCompleteTime": {
                            "type": "long"
                        }
                    }
                },
                "userExperienceScore": {
                    "type": "keyword"
                },
                "userId": {
                    "type": "keyword"
                },
                "userSessionId": {
                    "type": "keyword"
                },
                "userType": {
                    "type": "keyword"
                }
            }
        }
    }
}

As you can see there is nested array of userAction objects. Each user action has name. What I wan't to get is visualisation that presents top user actions that were last for users. In such case, it should be easy to create script field that will iterate over array and pick the last one. After that I should be able to chart name of this action because it should appear in root of my document. The problem is that I can do anything I want in painless editor and I will result with empty data.

doc['userActions.name'].values.length

Such query will return this:

[
 {
  "_id": "f2846475-da35-410f-a277-a9c780505c7b-2321974068_90-1592566612368",
  "leaveAction": [
   0
  ]
 },
 {
  "_id": "f2846475-da35-410f-a277-a9c780505c7b-PSMRABCFTIKAAPHTBRCWFCUHIFKVRCAI-0-1592566070043",
  "leaveAction": [
   0
  ]
 },
 {
  "_id": "f2846475-da35-410f-a277-a9c780505c7b-VHNFKTGJAKAUITINTQBLQTTABMOPRCQG-0-1592549380378",
  "leaveAction": [
   0
  ]
 },
 {
  "_id": "f2846475-da35-410f-a277-a9c780505c7b-IPJMSOAHLRFTLOTUQFLSACMEUQPVNSUK-0-1592564823345",
  "leaveAction": [
   0
  ]
 },
 {
  "_id": "f2846475-da35-410f-a277-a9c780505c7b-2407681831_23-1592566608240",
  "leaveAction": [
   0
  ]
 },
 {
  "_id": "f2846475-da35-410f-a277-a9c780505c7b-AKLKLLJAUJMECTPUMFQCEFMIMRDLLBMF-0-1592566679605",
  "leaveAction": [
   0
  ]
 },
 {
  "_id": "f2846475-da35-410f-a277-a9c780505c7b-CSCJIRGURTMMDKHRUTKIOHFBCDCTNKMM-0-1592566670783",
  "leaveAction": [
   0
  ]
 },
 {
  "_id": "f2846475-da35-410f-a277-a9c780505c7b-UMRNDMCUEPMPELFAPOGLKUUPWJSJHCMG-0-1592566704280",
  "leaveAction": [
   0
  ]
 },
 {
  "_id": "f2846475-da35-410f-a277-a9c780505c7b-CQSIAUKAAHHDAMTHCRMVUARHQAUQFMGB-0-1592566709232",
  "leaveAction": [
   0
  ]
 },
 {
  "_id": "f2846475-da35-410f-a277-a9c780505c7b-SFMSPRJHUUDWHUHRKQCFPFTPRUNFGDVW-0-1592566721460",
  "leaveAction": [
   0
  ]
 }
]

so even if there are user actions, I cannot count them (there is no user session without any action).

In general this should return what I need:

doc['userActions.name'].values[doc['userActions.name'].values.length - 1];

But I receive null values. I've check that this field is searchable and aggregatable, but nothing works here. I'm using ELK and Kibana in 6.8 version.

Kind regards, Sebastian


Solution

  • {
      "script_fields": {
        "userActions": {
          "script": {
            "source": 
            """
            def actions = params['_source']['userActions'];
            return actions[actions.length- 1].name;
            """, 
            "lang": "painless"
          }
        }
      }
    }
    

    It will work for you.