// Path: src/com/winterwell/es/client/agg/AggregationResults.java
// public class AggregationResults {
// 
// 	private String name;
// 	private Map results;
// 
// 	public AggregationResults(String aggName, Map rs) {
// 		this.name = aggName;
// 		this.results = rs;
// 	}
// 
// 	@Override
// 	public String toString() {
// 		return "AggregationResults[ "+name+": "+results+"]";
// 	}
// }
// 
// Path: src/com/winterwell/es/fail/ESBulkException.java
// public class ESBulkException extends ESException {
// 	
// 	private List<Exception> errors;
// 
// 	public List<Exception> getErrors() {
// 		return errors;
// 	}
// 	
// 	public ESBulkException(List<Exception> exs) {
// 		super(exs.size()+" errors", exs.size() > 1? exs.get(0) : null);
// 		this.errors = exs;
// 	}
// 
// 	private static final long serialVersionUID = 1L;
// 
// }
// 
// Path: src/com/winterwell/es/fail/ESException.java
// public class ESException extends WrappedException implements IElasticException {
// 
// 	public transient ESHttpRequest request;
// 
// 	public ESException(String msg, Throwable ex) {
// 		super(msg, ex);
// 	}
// 
// 	public ESException(String msg) {
// 		super(msg, null);
// 	}
// 	
// 
// 	private static final long serialVersionUID = 1L;
// 
// }


import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.winterwell.es.client.agg.AggregationResults;
import com.winterwell.es.fail.ESBulkException;
import com.winterwell.es.fail.ESException;
import com.winterwell.gson.Gson;
import com.winterwell.gson.GsonBuilder;
import com.winterwell.utils.Dep;
import com.winterwell.utils.Printer;
import com.winterwell.utils.StrUtils;
import com.winterwell.utils.Utils;
import com.winterwell.utils.containers.Containers;
import com.winterwell.utils.log.Log;
import com.winterwell.utils.web.IHasJson;
	@Override
	public Map getAggregations() {
		if ( ! isSuccess()) throw error;
		Map<String, Object> map = getParsedJson();
		Map hits = (Map) map.get("aggregations");
		return hits;
	}
	
	@Override
	public AggregationResults getAggregationResults(String aggName) {
		Map rs = (Map) getAggregations().get(aggName);
		return new AggregationResults(aggName, rs);
	}

	/**
	 * @return Never null - can throw exceptions
	 */
	@Override
	public long getTotal() throws IllegalArgumentException {
		if ( ! isSuccess()) throw error;
		Map<String, Object> map = getParsedJson();
		Map hits = (Map) map.get("hits");
		if (hits == null){
			throw new IllegalArgumentException("hits field cannot be null");
		}
		// c.f. https://www.elastic.co/guide/en/elasticsearch/reference/7.x/search-request-track-total-hits.html
		Map hitTotal = (Map) hits.get("total");
		if (hitTotal == null){
			throw new IllegalArgumentException("hitTotal field cannot be null");
		}
// Path: src/com/winterwell/es/client/agg/AggregationResults.java
// public class AggregationResults {
// 
// 	private String name;
// 	private Map results;
// 
// 	public AggregationResults(String aggName, Map rs) {
// 		this.name = aggName;
// 		this.results = rs;
// 	}
// 
// 	@Override
// 	public String toString() {
// 		return "AggregationResults[ "+name+": "+results+"]";
// 	}
// }
// 
// Path: src/com/winterwell/es/fail/ESBulkException.java
// public class ESBulkException extends ESException {
// 	
// 	private List<Exception> errors;
// 
// 	public List<Exception> getErrors() {
// 		return errors;
// 	}
// 	
// 	public ESBulkException(List<Exception> exs) {
// 		super(exs.size()+" errors", exs.size() > 1? exs.get(0) : null);
// 		this.errors = exs;
// 	}
// 
// 	private static final long serialVersionUID = 1L;
// 
// }
// 
// Path: src/com/winterwell/es/fail/ESException.java
// public class ESException extends WrappedException implements IElasticException {
// 
// 	public transient ESHttpRequest request;
// 
// 	public ESException(String msg, Throwable ex) {
// 		super(msg, ex);
// 	}
// 
// 	public ESException(String msg) {
// 		super(msg, null);
// 	}
// 	
// 
// 	private static final long serialVersionUID = 1L;
// 
// }


// Path: src/com/winterwell/es/client/ESHttpResponse.java
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.winterwell.es.client.agg.AggregationResults;
import com.winterwell.es.fail.ESBulkException;
import com.winterwell.es.fail.ESException;
import com.winterwell.gson.Gson;
import com.winterwell.gson.GsonBuilder;
import com.winterwell.utils.Dep;
import com.winterwell.utils.Printer;
import com.winterwell.utils.StrUtils;
import com.winterwell.utils.Utils;
import com.winterwell.utils.containers.Containers;
import com.winterwell.utils.log.Log;
import com.winterwell.utils.web.IHasJson;

	@Override
	public Map getAggregations() {
		if ( ! isSuccess()) throw error;
		Map<String, Object> map = getParsedJson();
		Map hits = (Map) map.get("aggregations");
		return hits;
	}
	
	@Override
	public AggregationResults getAggregationResults(String aggName) {
		Map rs = (Map) getAggregations().get(aggName);
		return new AggregationResults(aggName, rs);
	}

	/**
	 * @return Never null - can throw exceptions
	 */
	@Override
	public long getTotal() throws IllegalArgumentException {
		if ( ! isSuccess()) throw error;
		Map<String, Object> map = getParsedJson();
		Map hits = (Map) map.get("hits");
		if (hits == null){
			throw new IllegalArgumentException("hits field cannot be null");
		}
		// c.f. https://www.elastic.co/guide/en/elasticsearch/reference/7.x/search-request-track-total-hits.html
		Map hitTotal = (Map) hits.get("total");
		if (hitTotal == null){
			throw new IllegalArgumentException("hitTotal field cannot be null");
		}
		Number hitTotalValue = (Number) hitTotal.get("value");