弹性搜索数据的聚合操作

时间:2018-03-13 07:13:46

标签: elasticsearch elasticsearch-plugin elasticsearch-5

我有以下数据,但我需要构建一个查询,它将给出特定Nodetype的CpuAverageLoad的平均值。由于某些许可证问题,Kibana无法使用。

> {   "took" : 6,   "timed_out" : false,   "_shards" : {
>     "total" : 5,
>     "successful" : 5,
>     "failed" : 0   },   "hits" : {
>     "total" : 11,
>     "max_score" : 1.0,
>     "hits" : [
>       {
>         "_index" : "kpi",
>         "_type" : "kpi",
>         "_id" : "AWIZB9Ds_xnN0Y7Qo1A7",
>         "_score" : 1.0,
>         "_source" : {
>           "date" : "2018-03-12 12:36:05",
>           "kpi" : [
>             {
>               "CpuAverageLoad" : 3,
>               "HaGroupId" : "1001",
>               "LbGroupId" : "",
>               "MemFree" : 100,
>               "MemUsed" : 0,
>               "NodeId" : "kishan",
>               "NodeType" : "tpt",
>               "State" : "online",
>               "Static_limit" : 0
>             },
>             {
>               "CpuAverageLoad" : 0,
>               "HaGroupId" : "102",
>               "LbGroupId" : "",
>               "MemFree" : 0,
>               "MemUsed" : 0,
>               "NodeId" : "kalyan",
>               "NodeType" : "tpt1",
>               "State" : "online"
>             }
>           ]
>         }
>       },
>       {
>         "_index" : "kpi",
>         "_type" : "kpi",
>         "_id" : "AWIZCGzjH8WRs8qwDKh4",
>         "_score" : 1.0,
>         "_source" : {
>           "date" : "2018-03-12 12:36:45",
>           "kpi" : [
>             {
>               "CpuAverageLoad" : 1,
>               "HaGroupId" : "1001",
>               "LbGroupId" : "",
>               "MemFree" : 100,
>               "MemUsed" : 0,
>               "NodeId" : "kishan",
>               "NodeType" : "tpt",
>               "State" : "online",
>               "Static_limit" : 0
>             },
>             {
>               "CpuAverageLoad" : 0,
>               "HaGroupId" : "102",
>               "LbGroupId" : "",
>               "MemFree" : 0,
>               "MemUsed" : 0,
>               "NodeId" : "kalyan",
>               "NodeType" : "tpt1",
>               "State" : "online"
>             }
>           ]
>         }
>       },
>       {
>         "_index" : "kpi",
>         "_type" : "kpi",
>         "_id" : "AWIZCQlFnbh5r7Xr9nn0",
>         "_score" : 1.0,
>         "_source" : {
>           "date" : "2018-03-12 12:37:25",
>           "kpi" : [
>             {
>               "CpuAverageLoad" : 0,
>               "HaGroupId" : "1001",
>               "LbGroupId" : "",
>               "MemFree" : 100,
>               "MemUsed" : 0,
>               "NodeId" : "kishan",
>               "NodeType" : "tpt",
>               "State" : "online",
>               "Static_limit" : 0
>             },
>             {
>               "CpuAverageLoad" : 0,
>               "HaGroupId" : "102",
>               "LbGroupId" : "",
>               "MemFree" : 0,
>               "MemUsed" : 0,
>               "NodeId" : "kalyan",
>               "NodeType" : "tpt1",
>               "State" : "online"
>             }
>           ]
>         }
>       },
>       {
>         "_index" : "kpi",
>         "_type" : "kpi",
>         "_id" : "AWIZCfOonbh5r7Xr9nn1",
>         "_score" : 1.0,
>         "_source" : {
>           "date" : "2018-03-12 12:38:25",
>           "kpi" : [
>             {
>               "CpuAverageLoad" : 0,
>               "HaGroupId" : "1001",
>               "LbGroupId" : "",
>               "MemFree" : 100,
>               "MemUsed" : 0,
>               "NodeId" : "kishan",
>               "NodeType" : "tpt",
>               "State" : "online",
>               "Static_limit" : 0
>             },
>             {
>               "CpuAverageLoad" : 0,
>               "HaGroupId" : "102",
>               "LbGroupId" : "",
>               "MemFree" : 0,
>               "MemUsed" : 0,
>               "NodeId" : "kalyan",
>               "NodeType" : "tpt1",
>               "State" : "online"
>             }
>           ]
>         }
>       },
>       {
>         "_index" : "kpi",
>         "_type" : "kpi",
>         "_id" : "AWIZCo_B_xnN0Y7Qo1A-",
>         "_score" : 1.0,
>         "_source" : {
>           "date" : "2018-03-12 12:39:05",
>           "kpi" : [
>             {
>               "CpuAverageLoad" : 3,
>               "HaGroupId" : "1001",
>               "LbGroupId" : "",
>               "MemFree" : 100,
>               "MemUsed" : 0,
>               "NodeId" : "kishan",
>               "NodeType" : "tpt",
>               "State" : "online",
>               "Static_limit" : 0
>             },
>             {
>               "CpuAverageLoad" : 0,
>               "HaGroupId" : "102",
>               "LbGroupId" : "",
>               "MemFree" : 0,
>               "MemUsed" : 0,
>               "NodeId" : "kalyan",
>               "NodeType" : "tpt1",
>               "State" : "online"
>             }
>           ]
>         }
>       },
>       {
>         "_index" : "kpi",
>         "_type" : "kpi",
>         "_id" : "AWIZCLsE_xnN0Y7Qo1A8",
>         "_score" : 1.0,
>         "_source" : {
>           "date" : "2018-03-12 12:37:05",
>           "kpi" : [
>             {
>               "CpuAverageLoad" : 3,
>               "HaGroupId" : "1001",
>               "LbGroupId" : "",
>               "MemFree" : 100,
>               "MemUsed" : 0,
>               "NodeId" : "kishan",
>               "NodeType" : "tpt",
>               "State" : "online",
>               "Static_limit" : 0
>             },
>             {
>               "CpuAverageLoad" : 0,
>               "HaGroupId" : "102",
>               "LbGroupId" : "",
>               "MemFree" : 0,
>               "MemUsed" : 0,
>               "NodeId" : "kalyan",
>               "NodeType" : "tpt1",
>               "State" : "online"
>             }
>           ]
>         }
>       },
>       {
>         "_index" : "kpi",
>         "_type" : "kpi",
>         "_id" : "AWIZCVc9H8WRs8qwDKh5",
>         "_score" : 1.0,
>         "_source" : {
>           "date" : "2018-03-12 12:37:45",
>           "kpi" : [
>             {
>               "CpuAverageLoad" : 0,
>               "HaGroupId" : "1001",
>               "LbGroupId" : "",
>               "MemFree" : 100,
>               "MemUsed" : 0,
>               "NodeId" : "kishan",
>               "NodeType" : "tpt",
>               "State" : "online",
>               "Static_limit" : 0
>             },
>             {
>               "CpuAverageLoad" : 0,
>               "HaGroupId" : "102",
>               "LbGroupId" : "",
>               "MemFree" : 0,
>               "MemUsed" : 0,
>               "NodeId" : "kalyan",
>               "NodeType" : "tpt1",
>               "State" : "online"
>             }
>           ]
>         }
>       },
>       {
>         "_index" : "kpi",
>         "_type" : "kpi",
>         "_id" : "AWIZCaVq_xnN0Y7Qo1A9",
>         "_score" : 1.0,
>         "_source" : {
>           "date" : "2018-03-12 12:38:05",
>           "kpi" : [
>             {
>               "CpuAverageLoad" : 3,
>               "HaGroupId" : "1001",
>               "LbGroupId" : "",
>               "MemFree" : 100,
>               "MemUsed" : 0,
>               "NodeId" : "kishan",
>               "NodeType" : "tpt",
>               "State" : "online",
>               "Static_limit" : 0
>             },
>             {
>               "CpuAverageLoad" : 0,
>               "HaGroupId" : "102",
>               "LbGroupId" : "",
>               "MemFree" : 0,
>               "MemUsed" : 0,
>               "NodeId" : "kalyan",
>               "NodeType" : "tpt1",
>               "State" : "online"
>             }
>           ]
>         }
>       },
>       {
>         "_index" : "kpi",
>         "_type" : "kpi",
>         "_id" : "AWIZCB7vnbh5r7Xr9nnz",
>         "_score" : 1.0,
>         "_source" : {
>           "date" : "2018-03-12 12:36:25",
>           "kpi" : [
>             {
>               "CpuAverageLoad" : 0,
>               "HaGroupId" : "1001",
>               "LbGroupId" : "",
>               "MemFree" : 100,
>               "MemUsed" : 0,
>               "NodeId" : "kishan",
>               "NodeType" : "tpt",
>               "State" : "online",
>               "Static_limit" : 0
>             },
>             {
>               "CpuAverageLoad" : 0,
>               "HaGroupId" : "102",
>               "LbGroupId" : "",
>               "MemFree" : 0,
>               "MemUsed" : 0,
>               "NodeId" : "kalyan",
>               "NodeType" : "tpt1",
>               "State" : "online"
>             }
>           ]
>         }
>       },
>       {
>         "_index" : "kpi",
>         "_type" : "kpi",
>         "_id" : "AWIZCkGnH8WRs8qwDKh6",
>         "_score" : 1.0,
>         "_source" : {
>           "date" : "2018-03-12 12:38:45",
>           "kpi" : [
>             {
>               "CpuAverageLoad" : 1,
>               "HaGroupId" : "1001",
>               "LbGroupId" : "",
>               "MemFree" : 100,
>               "MemUsed" : 0,
>               "NodeId" : "kishan",
>               "NodeType" : "tpt",
>               "State" : "online",
>               "Static_limit" : 0
>             },
>             {
>               "CpuAverageLoad" : 0,
>               "HaGroupId" : "102",
>               "LbGroupId" : "",
>               "MemFree" : 0,
>               "MemUsed" : 0,
>               "NodeId" : "kalyan",
>               "NodeType" : "tpt1",
>               "State" : "online"
>             }
>           ]
>         }
>       }
>     ]   } }

我尝试过以下查询

curl -XGET 'localhost:9200/_search?pretty' -H 'Content-Type: application/json' -d'
{
    "query": {
        "bool" : {
            "must" : {
                "script" : {
                    "script" : {
                        "source" : "kpi[CpuAverageLoad].value > params.param1",
                        "lang"   : "painless",
                        "params" : {
                            "param1" : 5
                        }
                    }
                }
            }
        }
    }
}'

使用的映射:

const RegisterIndex = "register"
const KpiIndex = "kpi"

// Register & Kpi Index doc type
const RegisterDocType = "register"
const KpiDocType = "kpi

const KpiMapping = `
{
    "mappings": {
        "kpi": {
            "properties": {
                "kpi": {
                    "type": "nested" 
                }
            }
        }
    }
}`

type KpiDocStruct struct {
    Date     string        `json:"date"`
    Counters []interface{} `json:"kpi"`
}

我无法构建一个porper查询,我们给出了特定节点类型的CpuAverageLoad的平均值我提到

0 个答案:

没有答案