foreach循环嵌套遍历map

foreach嵌套dsl脚本定义

<property name="dynamicInnerDsl">

<![CDATA[{ ## 最多返回1000条记录

size: #[size],

"query": {

"bool": {

"must": [

#set($needComma = false)

#foreach($condition in $conditions.entrySet())

#foreach($item in $condition.value.entrySet())

#if($needComma), #else #set($needComma = true) #end

{

"$condition.key": {

"$item.key": #[conditions[$condition.key][$item.key],serialJson=true]

}

}

#end

#end

]

}

}

}]]>

</property>

传递参数和解析上述dsl的java方法代码

@Test

public void dynamicInnerDsl(){

Map conditions = new HashMap<String,Map<String,Object>>();

Map<String,Object> term = new HashMap<String, Object>();

term.put("terma","tavalue");

term.put("termb","tbvalue");

term.put("termc","tcvalue");

conditions.put("term",term);

Map params = new HashMap();

params.put("conditions",conditions);

params.put("size",1000);

//加载配置文件中的dsl信息,解析dsl语句dynamicInnerDsl

ESUtil esUtil = ESUtil.getInstance("esmapper/dsl.xml");

String parseResult = ESTemplateHelper.evalTemplate(esUtil,"dynamicInnerDsl",params);

//打印解析结果

System.out.println(parseResult);

}

运行上述代码打印出来的实际dsl

{

"size": 1000,

"query": {

"bool": {

"must": [


{

"term": {

"termb": "tbvalue"

}

},

{

"term": {

"termc": "tcvalue"

}

},

{

"term": {

"terma": "tavalue"

}

}

]

}

}

}