基于嵌套对象数组定义脚本字段

Defining scripted field based on nested object Array

我有这样的索引映射:

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

如您所见,存在嵌套的 userAction 对象数组。每个用户操作都有名称。我不想得到的是为用户呈现最后一次用户操作的可视化。在这种情况下,创建将遍历数组并选择最后一个的脚本字段应该很容易。之后我应该能够绘制此操作的名称,因为它应该出现在我的文档的根目录中。问题是我可以在无痛编辑器中做任何我想做的事,但我会得到空数据。

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

这样的查询将return这样:

[
 {
  "_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
  ]
 }
]

所以即使有用户操作,我也无法计数(没有任何操作的用户会话)。

一般来说,这应该return我需要的:

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

但是我收到空值。我检查过该字段是否可搜索和可聚合,但这里没有任何作用。我在 6.8 版本中使用 ELK 和 Kibana。

亲切的问候, 塞巴斯蒂安

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

它会为你工作。