gpt4 book ai didi

javascript - kibana 的 Vega 可视化 - 聚合和访问文档字段

转载 作者:行者123 更新时间:2023-11-30 14:41:07 29 4
gpt4 key购买 nike

我也是 Vega 和 Kibana 的新手,我试图创建一个显示主题标签及其平均极性的散点图,但是我遇到了两个方面的问题,首先是获取平均极性聚合,其次是从文档中访问主题标签文本字段。

我试图获得平均极性的代码(现在只是以时间刻度显示):

      {$schema: https://vega.github.io/schema/vega-lite/v2.json
data: {
# URL object is a context-aware query to Elasticsearch
url: {
# The %-enclosed keys are handled by Kibana to modify the query
# before it gets sent to Elasticsearch. Context is the search
# filter as shown above the dashboard. Timefield uses the value
# of the time picker from the upper right corner.
%context%: true
%timefield%: timestamp
index: tw
body: {
size: 10000
_source: ["timestamp", "user_lang", "country", "polarity", "lang", "sentiment"]
}
}
# We only need the content of hits.hits array
format: {property: "hits.hits"}
}
# Parse timestamp into a javascript date value
transform: [
{calculate: "toDate(datum._source['timestamp'])", as: "time"}
]
# Draw a circle, with x being the time field, and y - number of bytes
mark: line
encoding: {
x: {field: "time", type: "temporal"}
y: {aggregate: "mean", field: "_source.polarity", type: "quantitative"}
}
}

这给了我一个错误 Cannot read property 'polarity' of undefined。一旦我摆脱聚合,它就会起作用,但我想显示不是所有数据的平均值。

此外,我不知道如何访问嵌套的主题标签文本字段,我尝试了 _source.hashtags.text 但没有成功:

示例文档:

{
"_index": "tw",
"_type": "tweet",
"_id": "_HHWSGIBbYt8wc5TlB8B",
"_score": 1,
"_source": {
"lang": "en",
"favorited": false,
"sentiment": "positive",
"user_lang": "en",
"user_screenname": "BrideWiltshire",
"timestamp": "2018-03-21T13:54:04.928556",
"user_follow_count": 147,
"hashtags": [
{
"indices": [
8,
12
],
"text": "WIN"
}
],
"user_stat_count": 3377,
"user_fav_count": 11,
"coordinates": null,
"source": """<a href="https://panel.socialpilot.co/" rel="nofollow">SocialPilot.co</a>""",
"subjectivity": 0.3333333333333333,
"user_friends_count": 62,
"polarity": 0.5333333333333333,
"text": "Want to #WIN ‘His and Hers’ luggage labels from @DavidHampton, worth more than £100? Enter our competition now",
"message": "Want to #WIN ‘His and Hers’ luggage labels from @DavidHampton, worth more than £100? Enter our competition now",
"country": null,
"user_name": "Wiltshire Bride",
"favorite_count": 0
}
},

映射:

{
"tw": {
"mappings": {
"tweet": {
"properties": {
"coordinates": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"country": {
"type": "keyword"
},
"favorite_count": {
"type": "long"
},
"favorited": {
"type": "boolean"
},
"hashtags": {
"properties": {
"indices": {
"type": "long"
},
"text": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
},
"lang": {
"type": "text"
},
"location": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"message": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"polarity": {
"type": "float"
},
"sentiment": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"source": {
"type": "text"
},
"subjectivity": {
"type": "float"
},
"text": {
"type": "text"
},
"time_zone": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"timestamp": {
"type": "date"
},
"user": {
"properties": {
"favourites_count": {
"type": "long"
},
"followers_count": {
"type": "long"
},
"friends_count": {
"type": "long"
},
"lang": {
"type": "text"
},
"name": {
"type": "text"
},
"screen_name": {
"type": "text"
},
"statuses_count": {
"type": "long"
}
}
},
"user_fav_count": {
"type": "long"
},
"user_follow_count": {
"type": "long"
},
"user_friends_count": {
"type": "long"
},
"user_lang": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"user_name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"user_screenname": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"user_stat_count": {
"type": "long"
}
}
}
}
}
}

最佳答案

如果您的 hashtags 字段是嵌套类型并且 hashtags.text 是关键字字段(或具有 hashtags.text.keyword),那么您可以使用以下散点图

{
$schema: https://vega.github.io/schema/vega-lite/v2.json
title: hashtags vs avg_polarity
data: {
url: {
index: twitter
body: {
size: 0
query: {
match_all: {}
}
aggs: {
HashTags: {
nested: {path: "hashtags"}
aggs: {
HashTags_Text: {
terms: {field: "hashtags.text"}
aggs: {
Tweet_Polarity: {
reverse_nested: {}
aggs: {
Tweet_Polarity_avg: {
avg: {field: "polarity"}
}
}
}
}
}
}
}
}
}
}
format: {property: "aggregations.HashTags.HashTags_Text.buckets"}
}
mark: {type: "line"}
encoding: {
x: {
field: key
type: Nominal
axis: {title: "HashTags"}
}
y: {
field: Tweet_Polarity.Tweet_Polarity_avg.value
type: quantitative
axis: {title: "polarity"}
}
}
}

有趣的小插图 enter image description here编辑

在开始添加文档之前,您必须如下指定索引映射

POST /tw
{
"mappings": {
"tweet": {
"properties": {
"favorite_count": {
"type": "long"
},
"favorited": {
"type": "boolean"
},
"hashtags": {
"type": "nested",
"properties": {
"indices": {
"type": "long"
},
"text": {
"type": "keyword"
}
}
},
"lang": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"message": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"polarity": {
"type": "float"
},
"sentiment": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"source": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"subjectivity": {
"type": "float"
},
"text": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"timestamp": {
"type": "date"
},
"user_fav_count": {
"type": "long"
},
"user_follow_count": {
"type": "long"
},
"user_friends_count": {
"type": "long"
},
"user_lang": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"user_name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"user_screenname": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"user_stat_count": {
"type": "long"
}
}
}
}
}

关于javascript - kibana 的 Vega 可视化 - 聚合和访问文档字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49651709/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com