Elasticsearch聚合针对嵌套类型的查询返回空值,但会针对非嵌套类型返回数字

时间:2017-10-04 21:17:40

标签: elasticsearch grafana

我有以下查询 - 由grafana生成,当我针对嵌套“buckets”类型的索引运行时,它返回空值。

但是当我将“buckets”字段作为非嵌套类型运行时,当我针对相同的索引运行时,相同的查询返回非零数字

以下是查询:

{
"size": 0,
"query": {
    "bool": {
        "filter": [{
                "range": {
                    "timestamp": {
                        "from": "1504818600000",
                        "to": "1504818780000",
                        "include_lower": true,
                        "include_upper": true,
                        "format": "epoch_millis",
                        "boost": 1.0
                    }
                }
            },
            {
                "query_string": {
                    "query": "app_name:ssss AND buckets.name:dddr",
                    "fields": [],
                    "use_dis_max": true,
                    "tie_breaker": 0.0,
                    "default_operator": "or",
                    "auto_generate_phrase_queries": false,
                    "max_determinized_states": 10000,
                    "enable_position_increments": true,
                    "fuzziness": "AUTO",
                    "fuzzy_prefix_length": 0,
                    "fuzzy_max_expansions": 50,
                    "phrase_slop": 0,
                    "analyze_wildcard": true,
                    "escape": false,
                    "split_on_whitespace": true,
                    "boost": 1.0
                }
            }
        ],
        "disable_coord": false,
        "adjust_pure_negative": true,
        "boost": 1.0
    }
},
"aggregations": {
    "2": {
        "date_histogram": {
            "field": "timestamp",
            "format": "epoch_millis",
            "interval": "1m",
            "offset": 0,
            "order": {
                "_key": "asc"
            },
            "keyed": false,
            "min_doc_count": 0,
            "extended_bounds": {
                "min": "1504818600000",
                "max": "1504818780000"
            }
        },
        "aggregations": {
            "1": {
                "avg": {
                    "field": "buckets.basicStats.memUsed"
                }
            }
        }
    }
}
}

针对非嵌套类型运行:

{
"took" : 4,
"timed_out" : false,
"_shards" : {
 "total" : 5,
 "successful" : 5,
 "skipped" : 0,
 "failed" : 0
 },
 "hits" : {
 "total" : 3,
 "max_score" : 0.0,
 "hits" : [ ]
},
"aggregations" : {
"2" : {
  "buckets" : [
    {
      "key_as_string" : "1504818600000",
      "key" : 1504818600000,
      "doc_count" : 1,
      "1" : {
        "value" : 1.03766064E8
      }
    },
    {
      "key_as_string" : "1504818660000",
      "key" : 1504818660000,
      "doc_count" : 1,
      "1" : {
        "value" : 1.0376628E8
      }
    },
    {
      "key_as_string" : "1504818720000",
      "key" : 1504818720000,
      "doc_count" : 1,
      "1" : {
        "value" : 1.0376628E8
      }
    },
    {
      "key_as_string" : "1504818780000",
      "key" : 1504818780000,
      "doc_count" : 0,
      "1" : {
        "value" : null
      }
    }
  ]
}
}
}

但是当针对嵌套类型运行时:

 "buckets" : [
    {
      "key_as_string" : "1504818600000",
      "key" : 1504818600000,
      "doc_count" : 0,
      "1" : {
        "value" : null
      }
    },
    {
      "key_as_string" : "1504818660000",
      "key" : 1504818660000,
      "doc_count" : 0,
      "1" : {
        "value" : null
      }
    },
    {
      "key_as_string" : "1504818720000",
      "key" : 1504818720000,
      "doc_count" : 0,
      "1" : {
        "value" : null
      }
    },
    {
      "key_as_string" : "1504818780000",
      "key" : 1504818780000,
      "doc_count" : 0,
      "1" : {
        "value" : null
      }
    }
  ]

有没有办法让我更改查询以适用于嵌套类型?

以下是映射

{
"buckets": {
    "mappings": {
        "buckets": {
            "properties": {
                "app_name": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "buckets": {
                    "type": "nested",
                    "properties": {
                        "authType": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "autoCompactionSettings": {
                            "type": "boolean"
                        },
                        "basicStats": {
                            "properties": {
                                "dataUsed": {
                                    "type": "long"
                                },
                                "diskFetches": {
                                    "type": "long"
                                },
                                "diskUsed": {
                                    "type": "long"
                                },
                                "itemCount": {
                                    "type": "long"
                                },
                                "memUsed": {
                                    "type": "long"
                                },
                                "opsPerSec": {
                                    "type": "long"
                                },
                                "quotaPercentUsed": {
                                    "type": "float"
                                }
                            }
                        },
                        "bucketCapabilities": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "bucketCapabilitiesVer": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "bucketType": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "controllers": {
                            "properties": {
                                "compactAll": {
                                    "type": "text",
                                    "fields": {
                                        "keyword": {
                                            "type": "keyword",
                                            "ignore_above": 256
                                        }
                                    }
                                },
                                "compactDB": {
                                    "type": "text",
                                    "fields": {
                                        "keyword": {
                                            "type": "keyword",
                                            "ignore_above": 256
                                        }
                                    }
                                },
                                "flush": {
                                    "type": "text",
                                    "fields": {
                                        "keyword": {
                                            "type": "keyword",
                                            "ignore_above": 256
                                        }
                                    }
                                },
                                "purgeDeletes": {
                                    "type": "text",
                                    "fields": {
                                        "keyword": {
                                            "type": "keyword",
                                            "ignore_above": 256
                                        }
                                    }
                                },
                                "startRecovery": {
                                    "type": "text",
                                    "fields": {
                                        "keyword": {
                                            "type": "keyword",
                                            "ignore_above": 256
                                        }
                                    }
                                }
                            }
                        },
                        "ddocs": {
                            "properties": {
                                "uri": {
                                    "type": "text",
                                    "fields": {
                                        "keyword": {
                                            "type": "keyword",
                                            "ignore_above": 256
                                        }
                                    }
                                }
                            }
                        },
                        "evictionPolicy": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "localRandomKeyUri": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "name": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "nodeLocator": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "nodes": {
                            "type": "nested",
                            "properties": {
                                "clusterCompatibility": {
                                    "type": "long"
                                },
                                "clusterMembership": {
                                    "type": "text",
                                    "fields": {
                                        "keyword": {
                                            "type": "keyword",
                                            "ignore_above": 256
                                        }
                                    }
                                },
                                "couchApiBase": {
                                    "type": "text",
                                    "fields": {
                                        "keyword": {
                                            "type": "keyword",
                                            "ignore_above": 256
                                        }
                                    }
                                },
                                "couchApiBaseHTTPS": {
                                    "type": "text",
                                    "fields": {
                                        "keyword": {
                                            "type": "keyword",
                                            "ignore_above": 256
                                        }
                                    }
                                },
                                "hostname": {
                                    "type": "text",
                                    "fields": {
                                        "keyword": {
                                            "type": "keyword",
                                            "ignore_above": 256
                                        }
                                    }
                                },
                                "interestingStats": {
                                    "properties": {
                                        "cmd_get": {
                                            "type": "long"
                                        },
                                        "couch_docs_actual_disk_size": {
                                            "type": "long"
                                        },
                                        "couch_docs_data_size": {
                                            "type": "long"
                                        },
                                        "couch_spatial_data_size": {
                                            "type": "long"
                                        },
                                        "couch_spatial_disk_size": {
                                            "type": "long"
                                        },
                                        "couch_views_actual_disk_size": {
                                            "type": "long"
                                        },
                                        "couch_views_data_size": {
                                            "type": "long"
                                        },
                                        "curr_items": {
                                            "type": "long"
                                        },
                                        "curr_items_tot": {
                                            "type": "long"
                                        },
                                        "ep_bg_fetched": {
                                            "type": "long"
                                        },
                                        "get_hits": {
                                            "type": "long"
                                        },
                                        "mem_used": {
                                            "type": "long"
                                        },
                                        "ops": {
                                            "type": "long"
                                        },
                                        "vb_replica_curr_items": {
                                            "type": "long"
                                        }
                                    }
                                },
                                "mcdMemoryAllocated": {
                                    "type": "long"
                                },
                                "mcdMemoryReserved": {
                                    "type": "long"
                                },
                                "memoryFree": {
                                    "type": "long"
                                },
                                "memoryTotal": {
                                    "type": "long"
                                },
                                "os": {
                                    "type": "text",
                                    "fields": {
                                        "keyword": {
                                            "type": "keyword",
                                            "ignore_above": 256
                                        }
                                    }
                                },
                                "otpNode": {
                                    "type": "text",
                                    "fields": {
                                        "keyword": {
                                            "type": "keyword",
                                            "ignore_above": 256
                                        }
                                    }
                                },
                                "ports": {
                                    "properties": {
                                        "direct": {
                                            "type": "long"
                                        },
                                        "httpsCAPI": {
                                            "type": "long"
                                        },
                                        "httpsMgmt": {
                                            "type": "long"
                                        },
                                        "proxy": {
                                            "type": "long"
                                        },
                                        "sslProxy": {
                                            "type": "long"
                                        }
                                    }
                                },
                                "recoveryType": {
                                    "type": "text",
                                    "fields": {
                                        "keyword": {
                                            "type": "keyword",
                                            "ignore_above": 256
                                        }
                                    }
                                },
                                "replication": {
                                    "type": "long"
                                },
                                "services": {
                                    "type": "text",
                                    "fields": {
                                        "keyword": {
                                            "type": "keyword",
                                            "ignore_above": 256
                                        }
                                    }
                                },
                                "status": {
                                    "type": "text",
                                    "fields": {
                                        "keyword": {
                                            "type": "keyword",
                                            "ignore_above": 256
                                        }
                                    }
                                },
                                "systemStats": {
                                    "properties": {
                                        "cpu_utilization_rate": {
                                            "type": "float"
                                        },
                                        "mem_free": {
                                            "type": "long"
                                        },
                                        "mem_total": {
                                            "type": "long"
                                        },
                                        "swap_total": {
                                            "type": "long"
                                        },
                                        "swap_used": {
                                            "type": "long"
                                        }
                                    }
                                },
                                "thisNode": {
                                    "type": "boolean"
                                },
                                "uptime": {
                                    "type": "text",
                                    "fields": {
                                        "keyword": {
                                            "type": "keyword",
                                            "ignore_above": 256
                                        }
                                    }
                                },
                                "version": {
                                    "type": "text",
                                    "fields": {
                                        "keyword": {
                                            "type": "keyword",
                                            "ignore_above": 256
                                        }
                                    }
                                }
                            }
                        },
                        "proxyPort": {
                            "type": "long"
                        },
                        "quota": {
                            "properties": {
                                "ram": {
                                    "type": "long"
                                },
                                "rawRAM": {
                                    "type": "long"
                                }
                            }
                        },
                        "replicaIndex": {
                            "type": "boolean"
                        },
                        "replicaNumber": {
                            "type": "long"
                        },
                        "saslPassword": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "stats": {
                            "properties": {
                                "directoryURI": {
                                    "type": "text",
                                    "fields": {
                                        "keyword": {
                                            "type": "keyword",
                                            "ignore_above": 256
                                        }
                                    }
                                },
                                "nodeStatsListURI": {
                                    "type": "text",
                                    "fields": {
                                        "keyword": {
                                            "type": "keyword",
                                            "ignore_above": 256
                                        }
                                    }
                                },
                                "uri": {
                                    "type": "text",
                                    "fields": {
                                        "keyword": {
                                            "type": "keyword",
                                            "ignore_above": 256
                                        }
                                    }
                                }
                            }
                        },
                        "streamingUri": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "threadsNumber": {
                            "type": "long"
                        },
                        "timeSynchronization": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "uri": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "uuid": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "vBucketServerMap": {
                            "properties": {
                                "hashAlgorithm": {
                                    "type": "text",
                                    "fields": {
                                        "keyword": {
                                            "type": "keyword",
                                            "ignore_above": 256
                                        }
                                    }
                                },
                                "numReplicas": {
                                    "type": "long"
                                },
                                "serverList": {
                                    "type": "text",
                                    "fields": {
                                        "keyword": {
                                            "type": "keyword",
                                            "ignore_above": 256
                                        }
                                    }
                                }
                            }
                        }
                    }
                },
                "timestamp": {
                    "type": "date"
                }
            }
        }
    }
}
}

示例文档

    "hits": [{
        "_index": "buckets",
        "_type": "buckets",
        "_id": "59b1b8c427f20beff758a6ac",
        "_score": 2.361674,
        "_source": {
            "app_name": "sssss",
            "buckets": [{
                "authType": "sasl",
                "autoCompactionSettings": false,
                "basicStats": {
                    "dataUsed": 123467470,
                    "diskFetches": 0,
                    "diskUsed": 153858414,
                    "itemCount": 21,
                    "memUsed": 87807640,
                    "opsPerSec": 0,
                    "quotaPercentUsed": 2.725907911856969
                },
                "bucketCapabilities": ["cbhello", "touch", "couchapi", "cccp", "xdcrCheckpointing", "nodesExt", "dcp"],
                "bucketCapabilitiesVer": "",
                "bucketType": "membase",
                "controllers": {
                    "compactAll": "/pools/default/buckets/config/controller/compactBucket",
                    "compactDB": "/pools/default/buckets/default/controller/compactDatabases",
                    "purgeDeletes": "/pools/default/buckets/config/controller/unsafePurgeBucket",
                    "startRecovery": "/pools/default/buckets/config/controller/startRecovery"
                },
                "ddocs": {
                    "uri": "/pools/default/buckets/config/ddocs"
                },
                "evictionPolicy": "valueOnly",
                "localRandomKeyUri": "/pools/default/buckets/config/localRandomKey",
                "name": "config",
                "nodeLocator": "vbucket",
                "nodes": [{
                    "clusterCompatibility": 262149,
                    "clusterMembership": "active",
                    "couchApiBase": "http://lpdcbc01c.phx.aexp.com:8092/config%2B2fb1ce31a0421a3440047ad2a9e99bab",
                    "couchApiBaseHTTPS": "https://lpdcbc01c.phx.aexp.com:18092/config%2B2fb1ce31a0421a3440047ad2a9e99bab",
                    "hostname": "lpdcbc01c.phx.aexp.com:8091",
                    "interestingStats": {
                        "cmd_get": 0,
                        "couch_docs_actual_disk_size": 849296160,
                        "couch_docs_data_size": 727180093,
                        "couch_spatial_data_size": 0,
                        "couch_spatial_disk_size": 0,
                        "couch_views_actual_disk_size": 7610,
                        "couch_views_data_size": 7610,
                        "curr_items": 2022,
                        "curr_items_tot": 4069,
                        "ep_bg_fetched": 0,
                        "get_hits": 0,
                        "mem_used": 134052320,
                        "ops": 0,
                        "vb_replica_curr_items": 2047
                    },
                    "mcdMemoryAllocated": 6347,
                    "mcdMemoryReserved": 6347,
                    "memoryFree": 5827776512,
                    "memoryTotal": 8319832064,
                    "os": "x86_64-unknown-linux-gnu",
                    "otpNode": "ns_1@lpdcbc01c.phx.aexp.com",
                    "ports": {
                        "direct": 11210,
                        "httpsCAPI": 18092,
                        "httpsMgmt": 18091,
                        "proxy": 11211,
                        "sslProxy": 11214
                    },
                    "recoveryType": "none",
                    "replication": 1,
                    "services": ["index", "kv", "n1ql"],
                    "status": "healthy",
                    "systemStats": {
                        "cpu_utilization_rate": 9.004739336492891,
                        "mem_free": 5827776512,
                        "mem_total": 8319832064,
                        "swap_total": 4294963200,
                        "swap_used": 0
                    },
                    "uptime": "685303",
                    "version": "4.5.1-2844-enterprise"
                }, {

1 个答案:

答案 0 :(得分:0)

在对嵌套类型进行聚合时,您应该使用嵌套聚合。 Link

使用以下内容替换您的聚合,看看这是否适合您。

"aggregations": {
    "nested": {
      "nested": {
        "path": "buckets"
      },
      "aggs": {
        "1": {
          "avg": {
            "field": "buckets.basicStats.memUsed"
          }
        }
      }
    }
  }