Elasticsearch聚合查询示例2

按不同条件统计数量Request:

curl -X POST \
  http://elasticsearch:9200/devslicejobs/task/_search \
  -H 'Accept: */*' \
  -H 'Accept-Encoding: gzip, deflate' \
  -H 'Authorization: Basic ZWxhc3RpYzpzbGljZWpvYnNfMTIz' \
  -H 'Cache-Control: no-cache' \
  -H 'Connection: keep-alive' \
  -H 'Content-Length: 565' \
  -H 'Content-Type: application/json' \
  -H 'Host: elasticsearch:9200' \
  -H 'cache-control: no-cache' \
  -d '{
    "size": 0,
    "query": {
        "bool": {
            "must": [
                {
                    "term": {
                        "batchid": 4813
                    }
                }
               ]

        }
    },
    "aggs": {
        "tasks": {
            "filters": {
                "filters": {
                    "pending": {"bool": {"must": [{"term": {"status": 1}}]}},
                    "assign": {"bool": {"must": [{"term": {"status": 2}}]}},
                    "total": {"bool": {}}
                }
            }
        }
    }
}'

Response:

{
    "took": 29,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 101,
        "max_score": 0.0,
        "hits": []
    },
    "aggregations": {
        "tasks": {
            "buckets": {
                "assign": {
                    "doc_count": 12
                },
                "pending": {
                    "doc_count": 1
                },
                "total": {
                    "doc_count": 101
                }
            }
        }
    }
}

相关文档:https://www.elastic.co/guide/en/elasticsearch/reference/5.1/search-aggregations-bucket-filters-aggregation.html

发表评论

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