Elasticsearch DSL写的很崩溃,自己定义了个工具类,
然后这样撸起来还挺方便的,习惯了restfulapi,nativeapi暂且用着不顺先这样用着
import com.vacp.api.common.DateUtils;
import com.vacp.api.common.UUIDUtils;
import org.apache.commons.lang3.StringUtils;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
public class DslQueryUtils {
public static final StringASC="asc";
public static final StringDESC="desc";
public static final String_SOURCE="{\"includes\": [ \"*\"],\"excludes\": [ \"track.*\" ]}";
public static final String_SOURCEAGGS="{\"excludes\": [ \"*\" ]}";
public static final String_SOURCEAGGS_COLLAPSEC="{\"excludes\": [ \"*\" ]}";
static public Stringsearch(String dsl,String scriptfield,String aggs,String source,String sort,String hightlight,Integer from,Integer size,boolean profile ){
StringBuilder sb =new StringBuilder("{");
if(StringUtils.isNotBlank(dsl)){
sb.append("\"query\":"+dsl+",");
}
if(StringUtils.isNotBlank(scriptfield)){
sb.append("\"script_fields\":"+scriptfield+",");
}
if(StringUtils.isNotBlank(source)){
sb.append("\"_source\":"+source+",");
}
if(StringUtils.isNotBlank(sort)){
sb.append("\"sort\":"+sort+",");
}
if(StringUtils.isNotBlank(hightlight)){
sb.append("\"hightlight\":"+hightlight+",");
}
if(from!=null){
sb.append("\"from\":"+from+",");
}
if(size!=null){
sb.append("\"size\":"+size+",");
}
if(aggs!=null){
sb.append("\"aggs\":"+aggs+",");
}
if(profile==true){
sb.append("\"profile\":"+profile+",");
}
return sb.substring(0,sb.length()-1)+"}";
}
static public Stringsearch(String dsl,String scriptfield,String aggs,String collapse,String source,String sort,String hightlight,Integer from,Integer size,boolean profile ){
StringBuilder sb =new StringBuilder("{");
if(StringUtils.isNotBlank(dsl)){
sb.append("\"query\":"+dsl+",");
}
if(StringUtils.isNotBlank(scriptfield)){
sb.append("\"script_fields\":"+scriptfield+",");
}
if(StringUtils.isNotBlank(source)){
sb.append("\"_source\":"+source+",");
}
if(StringUtils.isNotBlank(sort)){
sb.append("\"sort\":"+sort+",");
}
if(StringUtils.isNotBlank(hightlight)){
sb.append("\"hightlight\":"+hightlight+",");
}
if(from!=null){
sb.append("\"from\":"+from+",");
}
if(collapse!=null){
sb.append("\"collapse\":"+collapse+",");
}
if(size!=null){
sb.append("\"size\":"+size+",");
}
if(aggs!=null){
sb.append("\"aggs\":"+aggs+",");
}
if(profile==true){
sb.append("\"profile\":"+profile+",");
}
return sb.substring(0,sb.length()-1)+"}";
}
static public Stringsearch( String dsl){
return search(dsl,null,null,null,null,null,null,null,false);
}
static public Stringsearch(String dsl,int from,int size ){
return search(dsl,null,null,_SOURCE,null,null,from,size,false);
}
static public StringsearchSort(String dsl,String sort,int from,int size ){
return search(dsl,null,null,_SOURCE,sort,null,from,size,false);
}
static public Stringsearch(String dsl,int from,int size,String source ){
return search(dsl,null,source,null,null,null,from,size,false);
}
static public StringsearchAddScriptField(String dsl,String scriptField,int from,int size ){
return search(dsl,scriptField,null,_SOURCE,null,null,from,size,false);
}
static public StringsearchAddScriptField(String dsl,String sort,String scriptField,int from,int size ){
return search(dsl,scriptField,null,_SOURCE,sort,null,from,size,false);
}
static public StringsearchAggs(String dsl,String aggs,int from,int size ){
return search(dsl,null,aggs,_SOURCEAGGS,null,null,from,size,false);
}
static public StringsearchCollapse(String dsl,String collapse,int from,int size ){
return search(dsl,null,null,collapse,_SOURCEAGGS_COLLAPSEC,null,null,from,size,false);
}
/**
* https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-post-filter.html
* @param dsl
* @param postFilter
* @param aggs
* @param from
* @param size
* @return
*/
static public StringsearchPostFilter(String dsl,String postFilter,String aggs,int from,int size,String source){
return search(dsl,null,aggs,_SOURCE,null,null,from,size,false);
}
static public Stringsearch(String dsl,String source,int from,int size ){
if(StringUtils.isBlank(source)){
source=_SOURCE;
}
return search(dsl,null,null,source,null,null,from,size,false);
}
static public Stringsearch(String dsl,String scriptfield,String source,String sort,int from,int size ){
if(StringUtils.isBlank(dsl)) {
dsl ="{}";
}
if(StringUtils.isBlank(source)){
source=_SOURCE;
}
return"{\"query\":"+dsl+",\"_source\":"+source+"\"sort\":"+sort+",\"from\":"+from+",\"size\":"+size+"}";
}
/**
*https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-simple-query-string-query.html
* @param query 正则通配符 widldcards
* @param operator and or not
* @param fields
* @return
*/
static public StringqueryStringOpr(String query,String operator,String... fields){
if(StringUtils.isBlank(operator)) {
operator ="and";
}
StringBuilder sb =new StringBuilder();
sb.append("{\"query_string\":{\"query\":\""+query+"\",\"default_operator\":\""+operator+"\",");
int i;
if(fields==null||fields.length==0){
sb.append("\"default_field\":\"_all\",");
}else {
sb.append("\"fields\":[");
for (i =0; i < fields.length; i++) {
sb.append("\"" + fields[i] +"\",");
}
sb=new StringBuilder(sb.substring(0,sb.length()-1)+"],");
}
return sb.substring(0,sb.length()-1)+"}}";
}
static public StringqueryString(String query,String... fields){
if(StringUtils.isBlank(query)) {
return null;
}
return queryStringOpr(query,"and",fields);
}
static public StringqueryStringOr(String query,String... fields){
query = query.trim();
if(StringUtils.isBlank(query)) {
return null;
}
return queryStringOpr(query,"or",fields);
}
static public StringscriptFields(String...values){
StringBuilder sb =new StringBuilder("{");
for(int i =0 ;i
sb.append("\""+values[i]+"\":{");
sb.append("\"script\":\""+values[i+1]+"\"},");
}
return sb.substring(0,sb.length()-1)+"}";
}
static public Stringbool(String must){
return bool(must,null,null,null,null);
}
static public Stringbool(String must,String mustNot,String should){
return bool(must,mustNot,should,null,null);
}
static public Stringbool(String must,String mustNot,String should,int minimum_should_match){
return bool(must,mustNot,should,minimum_should_match,null);
}
static public Stringbool(String must,String mustNot,String should,String filter){
return bool(must,mustNot,should,null,filter);
}
static public Stringbool(String must,String mustNot,String should,Integerminimum_should_match,String filter){
StringBuilder sb =new StringBuilder("{\"bool\":{");
if(StringUtils.isNotBlank(must)){
sb.append("\"must\":"+must+",");
}
if(StringUtils.isNotBlank(mustNot)){
sb.append("\"must_not\":"+mustNot+",");
}
if(StringUtils.isNotBlank(should)){
sb.append("\"should\":"+should+",");
}
if(minimum_should_match!=null){
sb.append("\"minimum_should_match\":"+minimum_should_match+",");
}
if(StringUtils.isNotBlank(filter)){
sb.append("\"filter\":"+filter+",");
}
return sb.substring(0,sb.length()-1).toString()+"}}";
}
static public StringboolFilter(String filter){
if(StringUtils.isBlank(filter))
filter="{}";
return "{\"bool\":{\"filter\":"+filter+"}}";
}
/**
* and or not
* @param operator
* @param dsl
* @return
*/
static public Stringfilter(String operator,String dsl){
return "{\""+operator+"\":["+dsl+"]}";
}
static public StringmergeDsl(String... dsls){
StringBuilder sb =new StringBuilder("[");
for(String dsl:dsls){
if(StringUtils.isBlank(dsl)) {
continue;
}
sb.append(dsl);
sb.append(",");
}
return sb.substring(0,sb.length()-1)+"]";
}
static public Stringmatch(String field,String value){
return "{\"match\":{\""+field+"\":\""+value+"\"}}";
}
/**
* @param field
* @param query
* @param operator (and or)
* @param minnum
* @return
*/
static public Stringmatch(String field,String query,String operator,int minnum){
return "{\"match\":{\""+field+"\":{\"query\":\""+query+"\",\"operator\":\""+operator+"\",\"\",:"+minnum+"}}";
}
static public Stringmatch(HashMap map){
StringBuilder sb =new StringBuilder("[");
for(Map.Entry entry :map.entrySet()){
if(entry.getValue()instanceof String ) {
sb.append("{\"match\":{\"" + entry.getKey() +"\":\"" + entry.getValue() +"\"}},");
}
else {
sb.append("{\"match\":{\"" + entry.getKey() +"\":" + entry.getValue() +"\"}},");
}
}
return sb.substring(0,sb.length()-1)+"]";
}
static public Stringmatch(String field,Object... list){
StringBuilder sb =new StringBuilder("[");
for(Object str :list){
if(strinstanceof String ) {
sb.append("{\"match\":{\"" + field +"\":\"" + str +"\"}},");
}
else {
sb.append("{\"match\":{\"" + field +"\":" + str +"\"}},");
}
}
return sb.substring(0,sb.length()-1)+"]";
}
static public Stringterm(String type, Object value){
if(value==null||StringUtils.isBlank(String.valueOf(value))){
return null;
}
if(valueinstanceof String) {
return "{\"term\":{\"" + type +"\":\"" + value +"\"}}";
}
else{
return "{\"term\":{\"" + type +"\":" + value +"}}";
}
}
static public StringtermCoverPlate(String type, String value){
if(StringUtils.isBlank(value)){
return null;
}
StringBuilder dsl =new StringBuilder("[");
String[] types =value.split(",");
for(String coverType:types){
dsl.append("{\"term\":{\"" + type +"\":"+coverType+"}},");
}
return dsl.substring(0,dsl.length()-1)+"]";
}
static public Stringterm(HashMap map){
if(map.size()==0) {
return null;
}
StringBuilder sb =new StringBuilder("[");
for(Map.Entry entry :map.entrySet()){
sb.append("{\"term\":{\""+entry.getKey()+"\":\""+entry.getValue()+"\"}},");
}
return sb.substring(0,sb.length()-1)+"]";
}
static public Stringterm(String field,String ... values){
if(values==null||values.length==0) {
return null;
}
StringBuilder sb =new StringBuilder("[");
for(String val:values){
sb.append("{\"term\":{\""+field+"\":\""+val+"\"}},");
}
return sb.substring(0,sb.length()-1)+"]";
}
static public StringmuilMatch(String ctx,String... fields){
StringBuilder strBuilder =new StringBuilder("\"{\"multi_match\":{\"query\":\"\"+ctx+\"\",\"fields\":[");
for(String filed :fields){
strBuilder.append("\""+filed+"\",");
}
return strBuilder.substring(0,strBuilder.length()-1)+"]}}}";
}
static public StringmuilMatchFuzzy(String ctx,String... fields){
StringBuilder strBuilder =new StringBuilder("\"{\"multi_match\":{\"query\":\"\"+ctx+\"\",\"fields\":[");
for(String filed :fields){
strBuilder.append("\""+filed+"\",");
}
return strBuilder.substring(0,strBuilder.length()-1)+"],\"fuzziness\":\"AUTO\"}}}";
}
/**
*
* @param field
* @param value
* @return
*/
static public StringwildCardQuery(String field,String value){
value=value+"*";
return "{\"wildcard\":{\""+field+"\",\""+value+"\"}}";
}
static public Stringregexp(String field,String regexp){
return "{\"regexp\":{\"field\":{\""+field+"\",\""+regexp+"\"}}}";
}
/**
*
* @param field
* @param value
* @return
*/
static public Stringhighlight(String field,String value){
return "{\"fields\":\""+field+"\":{\""+value+"\"}}";
}
static public StringmuilMatchAll(String ctx){
return "{\"multi_match\":{\"query\":\""+ctx+"\",\"fields\":[\"_all\"]}}";
}
static public StringmatchAll(){
return "{\"match_all\":{}}";
}
static public Stringmust(String dsl){
return null;
}
public StringBuildermustNot(HashMap map){
return null;
}
static public Stringshould(HashMap map){
return null;
}
static public StringrangeMarkValue(HashMap> markMap){
if(markMap.size()==0) {
return null;
}
StringBuilder sb =new StringBuilder("[");
for(Map.Entry> entry :markMap.entrySet()){
String type =entry.getKey();
HashMap mark = entry.getValue();
String mk = String.valueOf(mark.get("mark"));
int value = Integer.valueOf(String.valueOf(mark.get("value")));
if("=".equals(mk)){
sb.append("{\"term\":{\""+type+"\":"+value+"}},");
}
else if(">".equals(mk)){
sb.append("{\"range\":{\""+type+"\":{\"gt\":"+value+"}}},");
}
else if(">=".equals(mk)){
sb.append("{\"range\":{\""+type+"\":{\"gte\":"+value+"}}},");
}
else if("<".equals(mk)){
sb.append("{\"range\":{\""+type+"\":{\"lt\":"+value+"}}},");
}
else if("<=".equals(mk)){
sb.append("{\"range\":{\""+type+"\":{\"lte" +
"\":"+value+"}}},");
}
}
return sb.substring(0,sb.length()-1)+"]";
}
static public Stringrange(String type,Object minValue,Object maxValue,String gte,String lte){
Object tempMin =null;
Object tempMax =null;
if(minValueinstanceof Integer){
tempMin = (Integer)minValue;
tempMax = (Integer)maxValue;
}
else if(minValueinstanceof Long){
tempMin = (Long)minValue;
tempMax = (Long)maxValue;
}
else if(minValueinstanceof String){
tempMin ="\""+minValue+"\"";
tempMax ="\""+maxValue+"\"";
}
else if(minValueinstanceof Double){
tempMin = (Double)minValue;
tempMax = (Double)maxValue;
}
else if(minValueinstanceof Float){
tempMin = (Float)minValue;
tempMax = (Float)maxValue;
}
else if(minValueinstanceof Date){
tempMin =((Date) minValue).getTime();
tempMax =((Date) maxValue).getTime();
}
StringBuilder sb =new StringBuilder();
sb.append("{\"range\":{\""+type+"\":{");
if(StringUtils.isNotBlank(gte)){
sb.append("\""+gte+"\":"+String.valueOf(tempMin)+",");
}
if(StringUtils.isNotBlank(lte)){
sb.append("\""+lte+"\":"+String.valueOf(tempMax)+",");
}
return sb.substring(0,sb.length()-1)+"}}}";
}
static public StringrangeGtAndLt(String type,Object minValue,Object maxValue){
return range(type,minValue,maxValue,"gt","lt");
}
static public StringrangeGteAndLte(String type,Object minValue,Object maxValue){
return range(type,minValue,maxValue,"gte","lte");
}
static public StringrangeGte(String type,Object minValue){
return range(type,minValue,null,"gte",null);
}
static public StringrangeLte(String type,Object maxValue){
return range(type,null,maxValue,null,"lte");
}
static public StringscriptQuery(String source ){
return "{\"script\":{\"script\":\""+source+"\"}}";
}
static public StringscriptQueryByTime(String startTime, String endTime ){
if(StringUtils.isBlank(startTime)||StringUtils.isBlank(endTime)){
return null;
}
int start = Integer.valueOf(startTime.replace(":",""));
int end = Integer.valueOf(endTime.replace(":",""));
String source ="int hour = doc['pass_time'].date.hourOfDay;int minute = doc['pass_time'].date.minuteOfHour;int flag=hour*100+minute; flag>="+start+"&&flag <="+end+" ";
return "{\"script\":{\"script\":\""+source+"\"}}";
}
static public StringscriptQueryByTime2(String startTime, String endTime ){
if(StringUtils.isBlank(startTime)||StringUtils.isBlank(endTime)){
return null;
}
if(startTime.length()==4){
startTime=startTime+"00";
}
if(endTime.length()==4){
endTime=endTime+"00";
}
int start = Integer.valueOf(startTime.replace(":",""));
int end = Integer.valueOf(endTime.replace(":",""));
return rangeGteAndLte("timenum",start,end);
}
/**
*code
* @param source
* @param paramJson
* @return
*/
static public StringscriptQuery(String source,String paramJson ){
return "{\"script\":{\"script\":{\"code\":\""+source+"\",\"lang\" :\"painless\",\"params\":"+paramJson+"}}}";
}
static public StringscriptQueryByScriptId(String id,String paramJson ){
return "{\"script\":{\"script\":{\"stored\":\""+id+"\",\"params\":"+paramJson+"}}}";
}
static public Stringmissing(String type){
return "{\"missing\":{\"field\":\""+type+"\"}}";
}
static public Stringexists(String type){
return "{\"exists\":{\"field\":\""+type+"\"}}";
}
static public StringpackageScript(String script){
return "{\"script\":{\"lang\":\"painless\",\"code\":\""+script+"\"}}";
}
/**
* 排序
* @param field
* @param sortType
* @return
*/
static public Stringsort(String field,String sortType){
if(StringUtils.isBlank(field)) {
return null;
}
return "{ \""+field+"\": { \"order\": \""+sortType+"\" }}";
}
static public Stringasc(String field){
return sort(field,ASC);
}
static public Stringdesc(String field){
return sort(field,DESC);
}
static public StringhasParent(String type,String dsl){
return "{\"has_parent\":{\"type\":\""+type+"\",\"query\":"+dsl+"}}";
}
static public StringhasChild(String type,String dsl,String dsl2,boolean showChild){
StringBuilder sb =new StringBuilder("{\"has_child\":{ ");
sb.append("\"type\":\"" + type +"\",\"query\":" + dsl +",");
if(showChild==true) {
sb.append("\"inner_hits\": {} }");
}
String sql = sb.substring(0,sb.length()-1)+"}";
if (StringUtils.isNotBlank(dsl2)){
sql = sql+",\"filter\":"+dsl2;
}
return sql+"}";
}
static public StringhasChild(String type,String dsl,boolean showChild){
return hasChild( type, dsl, null, showChild);
}
static public StringnestedQuery(String path,String dsl,boolean showNested){
return nestedQuery(path,dsl,null,showNested);
}
static public StringnestedQuery(String path,String dsl){
return nestedQuery(path,dsl,null,true);
}
static public StringnestedFilter(String path,String dsl){
return nestedQuery(path,null,dsl,true);
}
static public StringnestedQuery(String type,String dsl,String dsl2,boolean showChild){
StringBuilder sb =new StringBuilder("{\"nested\":{ ");
sb.append("\"path\":\"" + type +"\",\"query\":" + dsl +",");
if(showChild==true) {
sb.append("\"inner_hits\": {} }");
}
String sql = sb.substring(0,sb.length()-1)+"}";
if (StringUtils.isNotBlank(dsl2)){
sql = sql+",\"filter\":"+dsl2;
}
return sql+"}";
}
/**
* Elasticsearch Fields _source 返回值字段设置
* @param fields
* @return
*/
static public Stringsource(String... fields){
StringBuilder sb =new StringBuilder("[");
for(String field :fields){
sb.append("\""+field+"\",");
}
return sb.substring(0,sb.length()-1)+"]";
}
/**
* 度量聚合
* @param nickName
* @param aggsType
* @return
* https://blog.csdn.net/donghaixiaolongwang/article/details/58597058
* _search?search_type=count 参数:search_type=count 表示只返回aggregation部分的结果。
* avg max min sum cardinality(distinct)
* stats 最大、最小、和、平均值。一起求出来
* extended_stats 其他属性,包括最大最小,方差等等。
* geo_bounds (attr "wrap_longitude" : true ) 计算出所有的地理坐标将会落在一个矩形区域
* geo_centroid 计算出所有文档的大概的中心点
* percentiles 百分比统计。可
* percentile_ranks
*top_hits 当于sql 中 group by 后取出前n条。
* value_count 数量统计,看看这个字段一共有多少个不一样的数值。
*
*
*/
static public StringaggsMetrics(String nickName,String aggsType,String field){
return "{\"aggs\":{\""+nickName+"\":{\""+aggsType+"\":{\"field\":\""+field+"\"}}}}";
}
static public Stringinner_hits(String name,String sort,int from,int size){
return "{\"name\":\""+name+"\",\"sort\":"+sort+",\"from\""+from+"\",\"size\":"+size+"}";
}
/**
* 自定义聚合
* https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-scripted-metric-aggregation.html
* @return
*/
static public StringscriptMetrics(String nickName,String init,String map,String combine,String reduce){
return "{\""+nickName+"\":{" +
"\"init_script\":" +init+","+
"\"map_script\":" +map+","+
"\"combine_script\":" +combine+
"\"reduce_script\":" +reduce+
"}}";
}
/**
* @param values
* @return
*/
static public StringaggsMetrics(String ... values){
StringBuilder sb =new StringBuilder();
sb.append("{\"aggs\":{");
for(int i =0 ;i
String nickName =values[i];
String aggsType = values[i+1];
String field = values[i+2];
sb.append("\"" + nickName +"\":{\"" + aggsType +"\":{\"field\":\"" + field +"\"},");
}
return sb.substring(0,sb.length()-1)+"}}";
}
/**
*https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket.html
* @return
* adjacency_matrix 邻接矩阵,同事满足两个条件的数量
* Children Aggregation 父子关系 聚合
* Composite Aggregation
* sub Aggregation
* Nested Aggregation
*/
static public StringaggsBucket(){
return null;
}
static public StringaggsLine(){
return null;
}
/**
* 去除中括号,或者括号
* @param json
* @return
*/
static public StringwipeBracket(String json){
return StringUtils.isBlank(json)?json:json.substring(1,json.length()-1);
}
/**
*
* @param sub
* @return
*/
public static StringreduceChar(String sub){
return sub.replace("[","").replace("]","").replace(","," ").replace("\"","");
}
/**
*
* @param sub
* @return
*/
public static StringreplaceChar(String sub){
return sub.replace("\"","\'");
}
}