【发布时间】:2020-05-21 12:05:38
【问题描述】:
我已使用存储脚本在索引中存储了 3 个日期数据类型变量(date1、date2、date3)。我计算了两个日期变量(date2 - date1)之间的持续时间并存储在第三个日期变量(date3)中。这是我使用过的完整映射、脚本和更新请求。
PUT /myindex
POST /myindex/_mappings
{
"properties":{
"date1":{
"type":"date"
},
"date2":{
"type":"date"
},
"date3":{
"type":"date"
}
}
}
POST _scripts/myindexscript/
{
"script":{
"source" :"""
if (ctx._source.date1==null) {
ctx._source.date1 = new Date().getTime();
}
ctx._source.status.add(params.status);
if (!(params.status).equalsIgnoreCase('Info')) {
ctx._source.date2 = new Date().getTime();
ctx._source.date3=ctx._source.date2 - ctx._source.date1;
}
""",
"lang": "painless"
}
}
POST /myindex/_update/1
{
"script":{
"id":"myindexscript",
"params":{
"status": "Infoa"
}
}
, "upsert": {
"date1":null,
"date2":null,
"date3":null,
"status": []
},
"scripted_upsert": true
}
如您在此处看到的,我已插入 1 个文档。为了在文档中插入,我首先运行字段 status='Info' 的 _update 请求,然后字段 status!='Infoa'。在这里你可以看到 GET /myindex/_search 的输出
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "myindex",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"date3" : 5589,
"date2" : 1590062272146,
"date1" : 1590062266557,
"status" : [
"Info",
"Infoa"
]
}
}
]
}
}
现在我运行这个特定的聚合请求
GET /myindex/_search
{
"query": {
"term": {
"status.keyword": {
"value": "Infoc"
}
}
},
"aggs": {
"NAME": {
"avg": {
"field": "date3"
}
}
},
"size": 20
}
这会给出这个奇怪的输出:
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.8025915,
"hits" : [
{
"_index" : "myindex",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.8025915,
"_source" : {
"date3" : 5589,
"date2" : 1590062272146,
"date1" : 1590062266557,
"status" : [
"Info",
"Infoa"
]
}
}
]
},
"aggregations" : {
"NAME" : {
"value" : 1.142046432E14,
"value_as_string" : "5589-01-01T00:00:00.000Z"
}
}
}
为什么聚合结果中的值不是 5589 而是 "value" : 1.142046432E14 。我无法理解如何处理 date3 字段以进行平均计算。请帮忙?
【问题讨论】:
标签: date elasticsearch elasticsearch-aggregation elasticsearch-painless