// 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;
		Map hits = (Map) map.get("hits");
		List<Map<String,Object>> hitsList = (List) hits.get("hits");
		List results = Containers.apply(hitsList, hit -> hit.get("_source"));
		return results;		
	}
	
	/**
	 * {@inheritDoc}
	 * 
	 * Uses {@link #gson()} for the convertor.
	 */
	@Override
	public <X> List<X> getSearchResults(Class<? extends X> klass) {
		check();
		Map<String, Object> jobj = getJsonMap();
		List<Map> hits = (List<Map>) ((Map)jobj.get("hits")).get("hits");
		List<X> results = Containers.apply(hits, map -> gson().convert((Map)map.get("_source"), klass));
		return results;
	}

	
	@Override
	public Map getAggregations() {
		if ( ! isSuccess()) throw error;
		Map<String, Object> map = getParsedJson();
		Map hits = (Map) map.get("aggregations");
		return hits;
	}
	
	@Override
// 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;

		Map hits = (Map) map.get("hits");
		List<Map<String,Object>> hitsList = (List) hits.get("hits");
		List results = Containers.apply(hitsList, hit -> hit.get("_source"));
		return results;		
	}
	
	/**
	 * {@inheritDoc}
	 * 
	 * Uses {@link #gson()} for the convertor.
	 */
	@Override
	public <X> List<X> getSearchResults(Class<? extends X> klass) {
		check();
		Map<String, Object> jobj = getJsonMap();
		List<Map> hits = (List<Map>) ((Map)jobj.get("hits")).get("hits");
		List<X> results = Containers.apply(hits, map -> gson().convert((Map)map.get("_source"), klass));
		return results;
	}

	
	@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) {