【问题标题】:Defining scripted field based on nested object Array基于嵌套对象数组定义脚本字段
【发布时间】:2020-10-13 11:30:54
【问题描述】:

我有这样的索引映射:

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

这样的查询将返回:

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

所以即使有用户操作,我也无法计算它们(没有任何操作就没有用户会话)。

通常这应该返回我需要的:

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

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

亲切的问候, 塞巴斯蒂安

【问题讨论】:

  • 您可以添加示例文档吗? doc['userActions'].size() 怎么样?试一试,让我知道。您想计算嵌套字段中的对象吗?对吗?
  • 这将返回空数组。但我需要做的是从数组中获取最后一个值。我需要 userAction.name 的最后一个值是具体的。此处提供示例文档:dynatrace.com/support/help/shortlink/…

标签: elasticsearch nested field kibana kibana-6


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

它会为你工作。

【讨论】:

  • 我试过了,但结果我看到 [] -> 空数组。似乎这种方式无法正常工作。我正在尝试通过 Kibana 中的图形编辑器添加此字段,但这应该不是问题。
  • 您是否也添加完整的查询。
  • 好的,经过小修复后它确实像一个魅力:) def actions = params['_source']['userActions']; return actions[actions.length - 1].name;
  • 我的错。我在打字时错过了它。感谢您解决这个问题。
猜你喜欢
  • 2020-05-08
  • 2021-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-13
  • 2016-11-17
  • 1970-01-01
相关资源
最近更新 更多