// Path: src/com/winterwell/es/client/query/ESQueryBuilder.java
// public class ESQueryBuilder implements IHasJson, Cloneable {
// 
// 	@Override
// 	public ESQueryBuilder clone() {
// 		ESQueryBuilder clone = new ESQueryBuilder(new ArrayMap(jobj));
// 		return clone;
// 	}
// 	
// 	@Override
// 	public String toString() {
// 		boolean prelock = lock; // HACK - don't accidentally lock by calling toJson2
// 		// NB: this prelock/lock code is not thread safe, but since its only sanity-checking code, that's OK.
// 		String s = "ESQueryBuilder"+toJSONString();
// 		lock = prelock;
// 		return s;
// 	}
// 	
// 	Map jobj;
// 	protected transient boolean lock;
// 	/**
// 	 * Many queries have one top level key, and the sub-object is where the settings go 
// 	 */
// 	protected Map props;
// 
// 	/**
// 	 * Direct access to the jobject map.
// 	 * @throws IllegalStateException see {@link #lockCheck()}
// 	 */
// 	public Map getUnderlyingMap() {
// 		lockCheck();
// 		return jobj;
// 	}
// 	
// 	/**
// 	 * 
// 	 * @param query Used directly! beware of side effects
// 	 */
// 	public ESQueryBuilder(Map query) {
// 		this.jobj = query;
// 		// Convenience hack
// 		if (query.size()==1) {
// 			this.props = (Map) Containers.first(jobj.values());	
// 		}
// 	}
// 	
// 	public ESQueryBuilder(QueryBuilder query) {
// 		this(ESUtils.jobj(query));
// 	}
// 	protected void lockCheck() throws IllegalStateException {
// 		if (lock) {
// 			throw new IllegalStateException("modified after toJson2() was used -- not allowed ('cos: risk of losing edits)");
// 		}
// 	}
// 	/**
// 	 * Construct an ESQueryBuilder from a QueryBuilder, ESQueryBuilder, or Map.
// 	 * 
// 	 * Note: the input object should not be modified afterwards!
// 	 *  
// 	 * @param query_mapOrQueryBuilder
// 	 * @return
// 	 */
// 	public static ESQueryBuilder make(Object query_mapOrQueryBuilder) {
// 		if (query_mapOrQueryBuilder instanceof ESQueryBuilder) {
// 			return (ESQueryBuilder) query_mapOrQueryBuilder;
// 		}
// 		if (query_mapOrQueryBuilder instanceof QueryBuilder) {
// 			return new ESQueryBuilder((QueryBuilder)query_mapOrQueryBuilder);
// 		}
// 		return new ESQueryBuilder((Map)query_mapOrQueryBuilder);
// 	}
// 
// 	@Override
// 	public Map<String,Object> toJson2() throws UnsupportedOperationException {
// 		lock = true;
// 		return jobj;
// 	}
// }


import com.winterwell.utils.time.Dt;
import com.winterwell.utils.time.TUnit;
import java.util.Map;
import com.winterwell.es.client.query.ESQueryBuilder;
/**
 * 
 */
package com.winterwell.es.client.agg;


/**
 * Builder methods for making {@link Aggregation}s
 * @author daniel
 *
 */
public class Aggregations {

	/**
	 * Default to one day interval
	 */
	public static Aggregation dateHistogram(String name, String field) {
		return dateHistogram(name, field, TUnit.DAY.dt);
	}
	
// Path: src/com/winterwell/es/client/query/ESQueryBuilder.java
// public class ESQueryBuilder implements IHasJson, Cloneable {
// 
// 	@Override
// 	public ESQueryBuilder clone() {
// 		ESQueryBuilder clone = new ESQueryBuilder(new ArrayMap(jobj));
// 		return clone;
// 	}
// 	
// 	@Override
// 	public String toString() {
// 		boolean prelock = lock; // HACK - don't accidentally lock by calling toJson2
// 		// NB: this prelock/lock code is not thread safe, but since its only sanity-checking code, that's OK.
// 		String s = "ESQueryBuilder"+toJSONString();
// 		lock = prelock;
// 		return s;
// 	}
// 	
// 	Map jobj;
// 	protected transient boolean lock;
// 	/**
// 	 * Many queries have one top level key, and the sub-object is where the settings go 
// 	 */
// 	protected Map props;
// 
// 	/**
// 	 * Direct access to the jobject map.
// 	 * @throws IllegalStateException see {@link #lockCheck()}
// 	 */
// 	public Map getUnderlyingMap() {
// 		lockCheck();
// 		return jobj;
// 	}
// 	
// 	/**
// 	 * 
// 	 * @param query Used directly! beware of side effects
// 	 */
// 	public ESQueryBuilder(Map query) {
// 		this.jobj = query;
// 		// Convenience hack
// 		if (query.size()==1) {
// 			this.props = (Map) Containers.first(jobj.values());	
// 		}
// 	}
// 	
// 	public ESQueryBuilder(QueryBuilder query) {
// 		this(ESUtils.jobj(query));
// 	}
// 	protected void lockCheck() throws IllegalStateException {
// 		if (lock) {
// 			throw new IllegalStateException("modified after toJson2() was used -- not allowed ('cos: risk of losing edits)");
// 		}
// 	}
// 	/**
// 	 * Construct an ESQueryBuilder from a QueryBuilder, ESQueryBuilder, or Map.
// 	 * 
// 	 * Note: the input object should not be modified afterwards!
// 	 *  
// 	 * @param query_mapOrQueryBuilder
// 	 * @return
// 	 */
// 	public static ESQueryBuilder make(Object query_mapOrQueryBuilder) {
// 		if (query_mapOrQueryBuilder instanceof ESQueryBuilder) {
// 			return (ESQueryBuilder) query_mapOrQueryBuilder;
// 		}
// 		if (query_mapOrQueryBuilder instanceof QueryBuilder) {
// 			return new ESQueryBuilder((QueryBuilder)query_mapOrQueryBuilder);
// 		}
// 		return new ESQueryBuilder((Map)query_mapOrQueryBuilder);
// 	}
// 
// 	@Override
// 	public Map<String,Object> toJson2() throws UnsupportedOperationException {
// 		lock = true;
// 		return jobj;
// 	}
// }


// Path: src/com/winterwell/es/client/agg/Aggregations.java
import com.winterwell.utils.time.Dt;
import com.winterwell.utils.time.TUnit;
import java.util.Map;
import com.winterwell.es.client.query.ESQueryBuilder;

/**
 * 
 */
package com.winterwell.es.client.agg;


/**
 * Builder methods for making {@link Aggregation}s
 * @author daniel
 *
 */
public class Aggregations {

	/**
	 * Default to one day interval
	 */
	public static Aggregation dateHistogram(String name, String field) {
		return dateHistogram(name, field, TUnit.DAY.dt);
	}
	
	public static Aggregation filtered(String name, ESQueryBuilder filter, Aggregation agg) {