弹性搜索的嵌套映射中的平均字段

时间:2018-03-14 10:10:06

标签: elasticsearch elasticsearch-5

我无法获得cpuaveload的平均值。

映射:

{
  "kpi" : {
    "mappings" : {
      "kpi" : {
        "properties" : {
          "date" : {
            "type" : "date"
          },
          "kpi" : {
            "type" : "nested",
            "properties" : {
              "CpuAverageLoad" : {
                "type" : "long"
              },
              "HaGroupId" : {
                "type" : "text",
                "fields" : {
                  "keyword" : {
                    "type" : "keyword",
                    "ignore_above" : 256
                  }
                }
              },
              "LbGroupId" : {
                "type" : "text",
                "fields" : {
                  "keyword" : {
                    "type" : "keyword",
                    "ignore_above" : 256
                  }
                }
              },
              "MemFree" : {
                "type" : "long"
              },
              "MemUsed" : {
                "type" : "long"
              },
              "NodeId" : {
                "type" : "text",
                "fields" : {
                  "keyword" : {
                    "type" : "keyword",
                    "ignore_above" : 256
                  }
                }
              },
              "NodeType" : {
                "type" : "text",
                "fields" : {
                  "keyword" : {
                    "type" : "keyword",
                    "ignore_above" : 256
                  }
                }
              },
              "State" : {
                "type" : "text",
                "fields" : {
                  "keyword" : {
                    "type" : "keyword",
                    "ignore_above" : 256
                  }
                }
              },
              "Static_limit" : {
                "type" : "long"
              }
            }
          }
        }
      }
    }
  }
}

查询

curl -XPOST 'localhost:9200/kpi/_search?size=0&pretty' -H 'Content-Type: application/json' -d'
{
    "from": 0,   "size": 10,
    "sort":[
        {"date" : {"order" : "desc"}}
    ],
    "stored_fields": [],
    "query": {
      "nested": {
      "path": "kpi",
      "query": {
        "match": {"kpi.NodeType" : "cs"}
      },
      "inner_hits": {
      },
      "aggs" : {
        "kpi" : {
          "nested" : {
            "path" : "kpi"
          },
        "aggs" : {
          "avg_price" : { "avg" : { "field" : "kpi.CpuAverageLoad" } }
        }
       }
     }      
    }
  }
}'

当我执行时,我得到以下错误:

  

" [嵌套]查询不支持[aggs]"   type:" parsing_exception",   原因:" [嵌套]查询不支持[aggs]",   line:16,col:16

我需要获得CpuAveLoad

的平均值

1 个答案:

答案 0 :(得分:0)

问题是,您在aggs中加入了queryinner_hits也定义在错误的位置。这应该有效:

{
   "from": 0,   "size": 10,
   "sort":[{
      "date" : {"order" : "desc"}
   }],
   "stored_fields": [],
   "query": {
      "nested" : {
         "path" : "kpi",
         "query" : {
            "match" : { "kpi.NodeType" : "type_a" }
         },
         "inner_hits" : {
         }
      }
    },


  "aggs": {   
     "kpi": {
        "nested": {
           "path": "kpi"
         },
         "aggs" : {
             "average-agg" : {
                 "avg" : { "field" : "kpi.CpuAverageLoad" }
              }

         }
     }   
   }
}

使用elasticsearch 5-6测试该解决方案。如果它对您有用,请告诉我。