Elasticsearch 聚合分析

2019-03-31  本文已影响0人  歌哥居士

准备数据

PUT test_search_index
{
  "settings": {
    "number_of_shards": 5,
    "number_of_replicas": 1
  }
}


POST test_search_index/doc/_bulk
{"index":{"_id":"1"}}
{"username":"alfred way","job":"java engineer","age":18,"birth":"1992-01-02","isMarried":false}
{"index":{"_id":"2"}}
{"username":"alfred","job":"java senior engineer and java specialist","age":28,"birth":"1980-05-07","isMarried":true}
{"index":{"_id":"3"}}
{"username":"lee","job":"java and ruby engineer","age":22,"birth":"1985-08-07","isMarried":false}
{"index":{"_id":"4"}}
{"username":"alfred junior way","job":"ruby engineer","age":23,"birth":"1989-08-07","isMarried":false}

GET test_search_index/doc/_search

Metric


# min
GET test_search_index/doc/_search
{
  "size": 0, 
  "aggs": {
    "min_age": {
      "min": {
        "field": "age"
      }
    }
  }
}

# max
GET test_search_index/doc/_search
{
  "size": 0,
  "aggs": {
    "max_age": {
      "max": {
        "field": "age"
      }
    }
  }
}

# avg
GET test_search_index/doc/_search
{
  "size": 0,
  "aggs": {
    "avg_age": {
      "avg": {
        "field": "age"
      }
    }
  }
}

# sum
GET test_search_index/doc/_search
{
  "size": 0, 
  "aggs": {
    "sum_age": {
      "sum": {
        "field": "age"
      }
    }
  }
}

# 组合多个
GET test_search_index/doc/_search
{
  "size": 0,
  "aggs": {
    "min_age": {
      "min": {
        "field": "age"
      }
    },
    "max_age": {
      "max": {
        "field": "age"
      }
    },
    "avg_age": {
      "avg": {
        "field": "age"
      }
    },
    "sum_age": {
      "sum": {
        "field": "age"
      }
    }
  }
}

# Cardinality: 基数,不同数值的个数,distinct count
GET test_search_index/doc/_search
{
  "size": 0,
  "aggs": {
    "count_of_jobs": {
      "cardinality": {
        "field": "job.keyword"
      }
    }
  }
}

# stats
GET test_search_index/doc/_search
{
  "size": 0,
  "aggs": {
    "stats_age": {
      "stats": {
        "field": "age"
      }
    }
  }
}


# extended_stats
GET test_search_index/doc/_search
{
  "size": 0,
  "aggs": {
    "extended_stats_age": {
      "extended_stats": {
        "field": "age"
      }
    }
  }
}

# percentile
GET test_search_index/doc/_search
{
  "size": 0,
  "aggs": {
    "per_age": {
      "percentiles": {
        "field": "age"
      }
    }
  }
}

# percentile_ranks
GET test_search_index/doc/_search
{
  "size": 0,
  "aggs": {
    "per_age": {
      "percentile_ranks": {
        "field": "age",
        "values": [
          22,
          28
          ]
      }
    }
  }
}
上一篇下一篇

猜你喜欢

热点阅读