public class EsQuery { private static int DEFAULT_SIZE =100; private final Map termFilter;private final Map rangeFilter;private final Map matchFilter;private int size;private String orderBy =null;private String order =null; // query 查询语法, 是否需要 filtered, filter 这两层// 5.x 版本不再需要这两层private boolean isNeedFilterLayer =true; private Integer from; private final Map mustNotTermFilter; private final Map shouldTermFilter;private Integer shouldMatchMinimum; private List includes;private List excludes; public EsQuery() {this.termFilter =new HashMap<>();this.rangeFilter =new HashMap();this.matchFilter =new HashMap();this.mustNotTermFilter =new HashMap<>();this.shouldTermFilter =new HashedMap();this.size = DEFAULT_SIZE;this.includes =new ArrayList<>();this.excludes =new ArrayList<>();} public EsQuery addTermFilter(String key, Object value) {this.termFilter.put(key, value);return this;} public EsQuery addMustNotTermFilter(String key, Object value) {this.mustNotTermFilter.put(key, value);return this;} public EsQuery addAllMustNotTermFilter(Map mustNot) {if (mustNot !=null && !mustNot.isEmpty()) {this.mustNotTermFilter.putAll(mustNot);}return this;} public EsQuery addShouldTermFilter(String key, Object value) {this.shouldTermFilter.put(key, value);return this;} public EsQuery addAllShouldTermFilter(Map should) {if (should !=null && !should.isEmpty()) {this.shouldTermFilter.putAll(should);}return this;} public EsQuery addRangeFilter(String key,long gte,long lte){this.rangeFilter.put(key,new Range(gte, lte));return this;} public EsQuery addMatchFilter(String key, Match value) {this.matchFilter.put(key, value);return this;} public EsQuery addIncludeFields(List includes) {this.includes.addAll(includes);return this;} public EsQuery addExcludeFields(List excludes) {this.excludes.addAll(excludes);return this;} @Overridepublic String toString() {return toJsonString();} public String toJsonString() {Map finalQuery =new HashMap<>();Map queryMap =new HashMap<>();Map filteredMap =new HashMap<>();Map filterMap =new HashMap<>();Map boolMap =new HashMap<>(); List mustList = obtainTermFilterList(this.termFilter); List mustNotList = obtainTermFilterList(this.mustNotTermFilter); List shouldList = obtainTermFilterList(this.shouldTermFilter); if(!this.rangeFilter.isEmpty()){for(Map.Entry e:this.rangeFilter.entrySet()){Map rangeMap =new HashMap<>();Map rangeEntityMap =new HashMap<>();rangeEntityMap.put(e.getKey(), e.getValue().toMap());rangeMap.put(Constant.range, rangeEntityMap);mustList.add(rangeMap);}} if(!this.matchFilter.isEmpty()){this.matchFilter.forEach((key, match) -> {Map matchEntityMap =new HashMap<>();Map matchMap =new HashMap<>();Map subMatchMap =new HashMap<>();matchEntityMap.put(Constant.query, match.getQuery());matchEntityMap.put(Constant.should_minum, match.getMinimumShouldMatch());matchMap.put(key, matchEntityMap);subMatchMap.put(Constant.match, matchMap);mustList.add(subMatchMap);});} boolMap.put(Constant.must, mustList);if (!mustNotList.isEmpty())boolMap.put(Constant.mustNot, mustNotList);if (!shouldList.isEmpty()) {// 有 minimum_should_match 不带过滤器boolMap.put(Constant.should, shouldList);boolMap.put(Constant.should_minum, shouldMatchMinimum);queryMap.put(Constant.bool, boolMap);}else {if (isNeedFilterLayer) {filterMap.put(Constant.bool, boolMap);filteredMap.put(Constant.filter, filterMap);queryMap.put(Constant.filtered, filteredMap);}else {queryMap.put(Constant.bool, boolMap);}}finalQuery.put(Constant.query, queryMap); Map orderMap =new HashMap<>();Map orderItem =new HashMap<>(); if(order !=null && orderBy !=null){orderItem.put(Constant.order,this.order);orderMap.put(this.orderBy, orderItem);finalQuery.put(Constant.sort, orderMap);} Map source =new HashMap<>();if (!includes.isEmpty()) {source.put(Constant.includes,this.includes);}if (!excludes.isEmpty()) {source.put(Constant.excludes,this.excludes);}if (!source.isEmpty()) {finalQuery.put(Constant.source, source);} finalQuery.put(Constant.size,this.size);if (from !=null) {finalQuery.put(Constant.from, from.intValue());}return JSON.toJSONString(finalQuery);} public List obtainTermFilterList(Map termFilter) {List termFilterList =new ArrayList<>();for (Map.Entry e: termFilter.entrySet()){Map termMap =new HashMap<>();Map itemMap =new HashMap<>();itemMap.put(e.getKey(), e.getValue());if(e.getValue()instanceof List){termMap.put(Constant.terms, itemMap);}else{termMap.put(Constant.term, itemMap);}termFilterList.add(termMap);}return termFilterList;} public String getOrderBy() {return orderBy;} public void setOrderBy(String orderBy) {this.orderBy = orderBy;} public String getOrder() {return order;} public void setOrder(String order) {this.order = order;} public int getSize() {return size;} public void setSize(int size) {this.size = size;} public Integer getFrom() {return from;} public void setFrom(Integer from) {this.from = from;} public Map getTermFilter() {return Collections.unmodifiableMap(termFilter);} public Map getRangeFilter() {return Collections.unmodifiableMap(rangeFilter);} public Map getMustNotTermFilter() {return Collections.unmodifiableMap(mustNotTermFilter);} public Map getShouldTermFilter() {return Collections.unmodifiableMap(shouldTermFilter);} public Map getMatchFilter() {return matchFilter;} public void setShouldMatchMinimum(Integer shouldMatchMinimum) {this.shouldMatchMinimum = shouldMatchMinimum;} public Integer getShouldMatchMinimum() {return shouldMatchMinimum;} public Map getRangeMap(String key) {return Collections.unmodifiableMap(rangeFilter.get(key).toMap());} public List getIncludes() {return Collections.unmodifiableList(includes);} public boolean isNeedFilterLayer() {return isNeedFilterLayer;} public void setNeedFilterLayer(boolean needFilterLayer) {isNeedFilterLayer = needFilterLayer;} @Overridepublic boolean equals(Object o) {// for you to write} @Overridepublic int hashCode() {// for you to write}