对NESTED对象的Kibana查询返回估计价格为什么?

时间:2018-05-09 01:48:46

标签: json elasticsearch kibana aggregation kibana-6

在Kibana可视化中,我构建了一个条形图图表,用于汇总每个标签桶的订单(例如:USD / CAD)。然后我显示其中每个订单的每桶订单价格总和。

我的图表可视化返回错误的价格,似乎即将近似

enter image description here

这是我的图形查询请求调试

{
  "size": 0,
  "_source": {
    "excludes": []
  },
  "aggs": {
    "3": {
      "terms": {
        "field": "label.keyword",
        "size": 2,
        "order": {
          "_term": "desc"
        }
      },
      "aggs": {
        "2": {
          "terms": {
            "field": "orders.id",
            "size": 40,
            "order": {
              "1": "desc"
            }
          },
          "aggs": {
            "1": {
              "sum": {
                "field": "orders.price"
              }
            }
          }
        }
      }
    }
  },
  "stored_fields": [
    "*"
  ],
  "script_fields": {},
  "docvalue_fields": [],
  "query": {
    "bool": {
      "must": [
        {
          "match_all": {}
        }
      ],
      "filter": [],
      "should": [],
      "must_not": []
    }
  }
}

这是响应,它确认了orders.price值近似

{
  "took": 9,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 124,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "3": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 122,
      "buckets": [
        {
          "2": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
              {
                "1": {
                  "value": 0.003690590019687079
                },
                "key": 213481,
                "doc_count": 1
              },
              {
                "1": {
                  "value": 0.003690590019687079
                },
                "key": 213482,
                "doc_count": 1
              },
              {
                "1": {
                  "value": 0.003690590019687079
                },
                "key": 213483,
                "doc_count": 1
              },
              {
                "1": {
                  "value": 0.003690590019687079
                },
                "key": 213484,
                "doc_count": 1
              },
              {
                "1": {
                  "value": 0.003690590019687079
                },
                "key": 213485,
                "doc_count": 1
              },
              {
                "1": {
                  "value": 0.003690590019687079
                },
                "key": 213486,
                "doc_count": 1
              },
              {
                "1": {
                  "value": 0.003690590019687079
                },
                "key": 213487,
                "doc_count": 1
              },
              {
                "1": {
                  "value": 0.003690590019687079
                },
                "key": 213488,
                "doc_count": 1
              },
              {
                "1": {
                  "value": 0.003690590019687079
                },
                "key": 213489,
                "doc_count": 1
              },
              {
                "1": {
                  "value": 0.003690590019687079
                },
                "key": 213490,
                "doc_count": 1
              },
              {
                "1": {
                  "value": 0.003690590019687079
                },
                "key": 213491,
                "doc_count": 1
              },
              {
                "1": {
                  "value": 0.003690590019687079
                },
                "key": 213492,
                "doc_count": 1
              },
              {
                "1": {
                  "value": 0.003690590019687079
                },
                "key": 213493,
                "doc_count": 1
              },
              {
                "1": {
                  "value": 0.003690590019687079
                },
                "key": 213494,
                "doc_count": 1
              },
              {
                "1": {
                  "value": 0.003690590019687079
                },
                "key": 213495,
                "doc_count": 1
              },
              {
                "1": {
                  "value": 0.003690590019687079
                },
                "key": 213496,
                "doc_count": 1
              },
              {
                "1": {
                  "value": 0.003690590019687079
                },
                "key": 213497,
                "doc_count": 1
              },
              {
                "1": {
                  "value": 0.003690590019687079
                },
                "key": 213498,
                "doc_count": 1
              },
              {
                "1": {
                  "value": 0.003690590019687079
                },
                "key": 213499,
                "doc_count": 1
              },
              {
                "1": {
                  "value": 0.003690590019687079
                },
                "key": 213500,
                "doc_count": 1
              },
              {
                "1": {
                  "value": 0.003690590019687079
                },
                "key": 213501,
                "doc_count": 1
              },
              {
                "1": {
                  "value": 0.003690590019687079
                },
                "key": 213502,
                "doc_count": 1
              },
              {
                "1": {
                  "value": 0.003690590019687079
                },
                "key": 213503,
                "doc_count": 1
              },
              {
                "1": {
                  "value": 0.003690590019687079
                },
                "key": 213504,
                "doc_count": 1
              },
              {
                "1": {
                  "value": 0.003690590019687079
                },
                "key": 213505,
                "doc_count": 1
              },
              {
                "1": {
                  "value": 0.003690590019687079
                },
                "key": 213506,
                "doc_count": 1
              },
              {
                "1": {
                  "value": 0.003690590019687079
                },
                "key": 213507,
                "doc_count": 1
              },
              {
                "1": {
                  "value": 0.003690590019687079
                },
                "key": 213508,
                "doc_count": 1
              },
              {
                "1": {
                  "value": 0.003690590019687079
                },
                "key": 213509,
                "doc_count": 1
              },
              {
                "1": {
                  "value": 0.003690590019687079
                },
                "key": 213510,
                "doc_count": 1
              },
              {
                "1": {
                  "value": 0.003690590019687079
                },
                "key": 213511,
                "doc_count": 1
              },
              {
                "1": {
                  "value": 0.003690590019687079
                },
                "key": 213512,
                "doc_count": 1
              },
              {
                "1": {
                  "value": 0.003690590019687079
                },
                "key": 213513,
                "doc_count": 1
              },
              {
                "1": {
                  "value": 0.003690590019687079
                },
                "key": 213514,
                "doc_count": 1
              },
              {
                "1": {
                  "value": 0.003690590019687079
                },
                "key": 213515,
                "doc_count": 1
              },
              {
                "1": {
                  "value": 0.003690590019687079
                },
                "key": 213516,
                "doc_count": 1
              },
              {
                "1": {
                  "value": 0.003690590019687079
                },
                "key": 213517,
                "doc_count": 1
              },
              {
                "1": {
                  "value": 0.003690590019687079
                },
                "key": 213518,
                "doc_count": 1
              },
              {
                "1": {
                  "value": 0.003690590019687079
                },
                "key": 213519,
                "doc_count": 1
              },
              {
                "1": {
                  "value": 0.003690590019687079
                },
                "key": 213520,
                "doc_count": 1
              }
            ]
          },
          "key": "IOST/ETH",
          "doc_count": 1
        },
        {
          "2": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
              {
                "1": {
                  "value": 0.0003004000000146334
                },
                "key": 213321,
                "doc_count": 1
              },
              {
                "1": {
                  "value": 0.0003004000000146334
                },
                "key": 213322,
                "doc_count": 1
              },
              {
                "1": {
                  "value": 0.0003004000000146334
                },
                "key": 213323,
                "doc_count": 1
              },
              {
                "1": {
                  "value": 0.0003004000000146334
                },
                "key": 213324,
                "doc_count": 1
              },
              {
                "1": {
                  "value": 0.0003004000000146334
                },
                "key": 213325,
                "doc_count": 1
              },
              {
                "1": {
                  "value": 0.0003004000000146334
                },
                "key": 213326,
                "doc_count": 1
              },
              {
                "1": {
                  "value": 0.0003004000000146334
                },
                "key": 213327,
                "doc_count": 1
              },
              {
                "1": {
                  "value": 0.0003004000000146334
                },
                "key": 213328,
                "doc_count": 1
              },
              {
                "1": {
                  "value": 0.0003004000000146334
                },
                "key": 213329,
                "doc_count": 1
              },
              {
                "1": {
                  "value": 0.0003004000000146334
                },
                "key": 213330,
                "doc_count": 1
              },
              {
                "1": {
                  "value": 0.0003004000000146334
                },
                "key": 213331,
                "doc_count": 1
              },
              {
                "1": {
                  "value": 0.0003004000000146334
                },
                "key": 213332,
                "doc_count": 1
              },
              {
                "1": {
                  "value": 0.0003004000000146334
                },
                "key": 213333,
                "doc_count": 1
              },
              {
                "1": {
                  "value": 0.0003004000000146334
                },
                "key": 213334,
                "doc_count": 1
              },
              {
                "1": {
                  "value": 0.0003004000000146334
                },
                "key": 213335,
                "doc_count": 1
              },
              {
                "1": {
                  "value": 0.0003004000000146334
                },
                "key": 213336,
                "doc_count": 1
              },
              {
                "1": {
                  "value": 0.0003004000000146334
                },
                "key": 213337,
                "doc_count": 1
              },
              {
                "1": {
                  "value": 0.0003004000000146334
                },
                "key": 213338,
                "doc_count": 1
              },
              {
                "1": {
                  "value": 0.0003004000000146334
                },
                "key": 213339,
                "doc_count": 1
              },
              {
                "1": {
                  "value": 0.0003004000000146334
                },
                "key": 213340,
                "doc_count": 1
              },
              {
                "1": {
                  "value": 0.0003004000000146334
                },
                "key": 213341,
                "doc_count": 1
              },
              {
                "1": {
                  "value": 0.0003004000000146334
                },
                "key": 213342,
                "doc_count": 1
              },
              {
                "1": {
                  "value": 0.0003004000000146334
                },
                "key": 213343,
                "doc_count": 1
              },
              {
                "1": {
                  "value": 0.0003004000000146334
                },
                "key": 213344,
                "doc_count": 1
              },
              {
                "1": {
                  "value": 0.0003004000000146334
                },
                "key": 213345,
                "doc_count": 1
              },
              {
                "1": {
                  "value": 0.0003004000000146334
                },
                "key": 213346,
                "doc_count": 1
              },
              {
                "1": {
                  "value": 0.0003004000000146334
                },
                "key": 213347,
                "doc_count": 1
              },
              {
                "1": {
                  "value": 0.0003004000000146334
                },
                "key": 213348,
                "doc_count": 1
              },
              {
                "1": {
                  "value": 0.0003004000000146334
                },
                "key": 213349,
                "doc_count": 1
              },
              {
                "1": {
                  "value": 0.0003004000000146334
                },
                "key": 213350,
                "doc_count": 1
              },
              {
                "1": {
                  "value": 0.0003004000000146334
                },
                "key": 213351,
                "doc_count": 1
              },
              {
                "1": {
                  "value": 0.0003004000000146334
                },
                "key": 213352,
                "doc_count": 1
              },
              {
                "1": {
                  "value": 0.0003004000000146334
                },
                "key": 213353,
                "doc_count": 1
              },
              {
                "1": {
                  "value": 0.0003004000000146334
                },
                "key": 213354,
                "doc_count": 1
              },
              {
                "1": {
                  "value": 0.0003004000000146334
                },
                "key": 213355,
                "doc_count": 1
              },
              {
                "1": {
                  "value": 0.0003004000000146334
                },
                "key": 213356,
                "doc_count": 1
              },
              {
                "1": {
                  "value": 0.0003004000000146334
                },
                "key": 213357,
                "doc_count": 1
              },
              {
                "1": {
                  "value": 0.0003004000000146334
                },
                "key": 213358,
                "doc_count": 1
              },
              {
                "1": {
                  "value": 0.0003004000000146334
                },
                "key": 213359,
                "doc_count": 1
              },
              {
                "1": {
                  "value": 0.0003004000000146334
                },
                "key": 213360,
                "doc_count": 1
              }
            ]
          },
          "key": "IOST/BTC",
          "doc_count": 1
        }
      ]
    }
  },
  "status": 200

发现标签确认我的数据在那里,应该从order.price

中检索合适的价格

更奇怪的是,当我展平数据并停止使用嵌套订单数组时,相同的图形将起作用。我在构建图表时做错了什么?我需要解决这个问题,因为我的源数据来自该结构,它有一个嵌套的每个标签的订单数组

1 个答案:

答案 0 :(得分:1)

必须将订单字段映射为嵌套类型才能获得准确的结果。

"mappings": {
            "orderbook": {
                "properties": {
                    "exchange": {
                        "type": "text",
                        "fields": {
                            "keyword": {
                                "type": "keyword",
                                "ignore_above": 256
                            }
                        }
                    },
                    "id": {
                        "type": "long"
                    },
                    "label": {
                        "type": "text",
                        "fields": {
                            "keyword": {
                                "type": "keyword",
                                "ignore_above": 256
                            }
                        }
                    },
                    "length": {
                        "type": "long"
                    },
                    "orders": {
                        "type": "nested",
                        "properties": {
                            "id": {
                                "type": "long"
                            },
                            "label": {
                                "type": "text",
                                "fields": {
                                    "keyword": {
                                        "type": "keyword",
                                        "ignore_above": 256
                                    }
                                }
                            },
                            "price": {
                                "type": "float"
                            }
                        }
                    },
                    "timestamp": {
                        "type": "text",
                        "fields": {
                            "keyword": {
                                "type": "keyword",
                                "ignore_above": 256
                            }
                        }
                    }
                }
            }
        }

在Kibana,你可以使用像这样的vega

{
  "$schema": "https://vega.github.io/schema/vega-lite/v2.json",
  "title": "Order Totals vs Order Ids",
  "data": {
    "url": {
      "index": "orderbookindex",
      "body": {
        "size": 0,
        "aggs": {
          "Orders": {
            "nested": {"path": "orders"},
            "aggs": {
              "Order_Ids": {
                "terms": {"field": "orders.id"},
                "aggs": {
                  "Order_Totals": {
                    "sum": {"field": "orders.price"}
                  }
                }
              }
            }
          }
        }
      }
    },
    "format": {
      "property": "aggregations.Orders.Order_Ids.buckets"
    }
  },
  "mark": "bar",
  "encoding": {
    "x": {
      "bin": false,
      "field": "key",
      "type": "Nominal",
      "axis": {"title": "Order Ids"}
    },
    "y": {
      "field": "Order_Totals.value",
      "type": "quantitative",
      "axis": {"title": "Order Totals"}
    }
  }
}

enter image description here

我还没有尝试过另一层分组,但你需要这样的东西https://bl.ocks.org/domoritz/f5abc519dd990bfcbc3f20f634658364

还有一个插件可用于处理Kibana中的嵌套聚合,供您尝试 https://github.com/ppadovani/KibanaNestedSupportPlugin

--------------解决方案输出------------------------------- ---

来自@ sramalingam24 Vega代码的Guid&#输出

enter image description here