Elasticsearch聚合查询示例1

Request:

curl -X POST \
  http://elasticsearch:9200/devorders/order/_search \
  -H 'Accept: */*' \
  -H 'Accept-Encoding: gzip, deflate' \
  -H 'Authorization: Basic ZWxhc3RpYzpzbGljZWpvYnNfMTIz' \
  -H 'Cache-Control: no-cache' \
  -H 'Connection: keep-alive' \
  -H 'Content-Length: 1333' \
  -H 'Content-Type: application/json' \
  -H 'Host: elasticsearch:9200' \
  -H 'cache-control: no-cache' \
  -d '{
    "size": 0,
    "sort": [
        {
            "orderid": {
                "order": "desc"
            }
        }
    ],
    "query": {
        "bool": {
            "must_not": [],
            "must": [
                {
                    "terms": {
                        "provinceid": [
                            "370"
                        ]
                    }
                },
                {
                    "term": {
                        "batchid": 4813
                    }
                },
                {
                    "terms": {
                        "status": [
                            1,
                            2,
                            3,
                            4,
                            40,
                            5
                        ]
                    }
                },
                {
                    "range": {
                        "origin_salary": {
                            "gte": 0
                        }
                    }
                }
            ]
        }
    },
    "aggs": {
        "sum_by_salary": {
            "sum": {
                "field": "salary"
            }
        },
        "sum_by_origin": {
            "sum": {
                "field": "origin_salary"
            }
        }
    }
}'

Response:

{
    "took": 49,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 10,
        "max_score": 0.0,
        "hits": []
    },
    "aggregations": {
        "sum_by_origin": {
            "value": 0.0
        },
        "sum_by_salary": {
            "value": 115.0
        }
    }
}

另附上一个script示例的Request:

{
    "size": 0,
    "aggs": {
        "sum_by_salary": {
            "sum": {
                "script": {
                    "lang": "painless",
                    "inline": "doc['hirenum'].value * doc['salary'].value"
                }
            }
        }
    }
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注