(16) ElasticSearch aggregate query

Data Preparation: Section 15 Data

(1) sum (sum), aggs fixed wording, price_of_sum name is taken.

GET /lib7/items/_search
{
    "size":0,
    "aggs":{
        "price_of_sum":{
            "sum":{
                "field":"price"
            }
        }
    }
}

Output:

{
  "took": 9,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 5,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "price_of_sum": {
      "value": 145
    }
  }
}

(2) for the minimum (min)

GET /lib7/items/_search
{
    "size":0,
    "aggs":{
        "price_of_min":{
            "min":{
                "field":"price"
            }
        }
    }
}

Output:

{
  "took": 51,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 5,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "price_of_min": {
      "value": 25
    }
  }
}

(3) selecting the maximum value (max)

GET /lib7/items/_search
{
    "size":0,
    "aggs":{
        "price_of_max":{
            "max":{
                "field":"price"
            }
        }
    }
}

Output:

{
  "took": 14,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 5,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "price_of_max": {
      "value": 50
    }
  }
}

(4) averaging (avg)

GET /lib7/items/_search
{
    "size":0,
    "aggs":{
        "price_of_avg":{
            "avg":{
                "field":"price"
            }
        }
    }
}

Output:

{
  "took": 14,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 5,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "price_of_avg": {
      "value": 36.25
    }
  }
}

The number (5) Find the cardinality (cardinality), mutually different

GET /lib7/items/_search
{
    "size":0,
    "aggs":{
        "price_of_cardi":{
            "cardinality":{
                "field":"price"
            }
        }
    }
}

Execution results are as follows:

{
  "took": 38,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 5,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "price_of_cardi": {
      "value": 4
    }
  }
}

(6) a packet (Terms)

GET /lib7/items/_search
{
    "size":0,
    "aggs":{
        "price_group_by":{
            "terms":{
                "field":"price"
            }
        }
    }
}

Output:

{
  "took": 45,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 5,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "price_group_by": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": 25,
          "doc_count": 1
        },
        {
          "key": 30,
          "doc_count": 1
        },
        {
          "key": 40,
          "doc_count": 1
        },
        {
          "key": 50,
          "doc_count": 1
        }
      ]
    }
  }
}

(7) For those interested users by age group singing, and the average age of each group in reverse order, from the data section 12

GET /lib3/user/_search
{
    "size":0,
    "query":{
        "match":{
            "interests":"changge"
        }
    },
    "aggs":{
        "age_of_group":{
            "terms":{
                "field":"age",
                "order":{
                    "age_of_avg":"desc"
                }
            },
            "aggs":{
                "age_of_avg":{
                    "avg":{
                        "field":"age"
                    }
                }
            }
        }
    }
}

Output:

{
  "took": 19,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "age_of_group": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": 29,
          "doc_count": 1,
          "age_of_avg": {
            "value": 29
          }
        },
        {
          "key": 23,
          "doc_count": 1,
          "age_of_avg": {
            "value": 23
          }
        },
        {
          "key": 20,
          "doc_count": 1,
          "age_of_avg": {
            "value": 20
          }
        }
      ]
    }
  }
}

 

Guess you like

Origin www.cnblogs.com/javasl/p/11487687.html