ElasticSearch 学习笔记 - 10.指标聚合

1、基础数据

GET /my-index/_search

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 1,
    "hits": [
      {
        "_index": "my-index",
        "_type": "person",
        "_id": "5",
        "_score": 1,
        "_source": {
          "name": "kobe",
          "age": 28,
          "salary": 10000
        }
      },
      {
        "_index": "my-index",
        "_type": "person",
        "_id": "6",
        "_score": 1,
        "_source": {
          "name": "hadron",
          "age": 19,
          "salary": 5000
        }
      }
    ]
  }
}

2、max

GET /my-index/_search
{
  "size": 1,
  "aggs": {
    "max_age": {
      "max": {
        "field": "age"
      }
    }
  }
}

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 1,
    "hits": [
      {
        "_index": "my-index",
        "_type": "person",
        "_id": "5",
        "_score": 1,
        "_source": {
          "name": "kobe",
          "age": 28,
          "salary": 10000
        }
      }
    ]
  },
  "aggregations": {
    "max_age": {
      "value": 28
    }
  }
}

3、min

GET /my-index/_search
{
  "size": 2,
  "aggs": {
    "min_age": {
      "min": {
        "field": "age"
      }
    }
  }
}

4、avg

GET /my-index/_search
{
  "size": 2,
  "aggs": {
    "avg_age": {
      "avg": {
        "field": "age"
      }
    }
  }
}

5、sum

GET /my-index/_search
{
  "size": 20,
  "aggs": {
    "sum_salary": {
      "sum": {
        "field": "salary"
      }
    }
  }
}

6、 stat

GET my-index/_search
{
  "size": 0, 
  "aggs": {
    "stats_salary": {
      "stats": {"field": "salary"}
    }
  }
}

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "stats_salary": {
      "count": 2,
      "min": 5000,
      "max": 10000,
      "avg": 7500,
      "sum": 15000
    }
  }
}

7、高级统计

GET my-index/_search
{
  "size": 0, 
  "aggs": {
    "stats_salary": {
      "extended_stats": {"field": "salary"}
    }
  }
}

{
  "took": 0,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "stats_salary": {
      "count": 2,
      "min": 5000,
      "max": 10000,
      "avg": 7500,
      "sum": 15000,
      "sum_of_squares": 125000000,
      "variance": 6250000,
      "std_deviation": 2500,
      "std_deviation_bounds": {
        "upper": 12500,
        "lower": 2500
      }
    }
  }
}

8、文档数量统计

GET my-index/_search
{
  "size": 0, 
  "aggs": {
    "doc_count": {
      "value_count": {"field": "salary"}
    }
  }
}

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "doc_count": {
      "value": 2
    }
  }
}

9、百分位统计

GET my-index/_search
{
  "size": 0, 
  "aggs": {
    "persion_salary": {
      "percentiles": {"field": "salary"}
    }
  }
}

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "persion_salary": {
      "values": {
        "1.0": 5000,
        "5.0": 5000,
        "25.0": 5000,
        "50.0": 7500,
        "75.0": 10000,
        "95.0": 10000,
        "99.0": 10000
      }
    }
  }
}

相关推荐