返回map

    "scripted_terms": { 

      "scripted_metric": { 

        "init_script": "_agg[\"prd\"] = []", 

        "map_script": "if(doc[\"cat2_id\"].value) {_agg.prd.add(doc[\"cat2_id\"].value.toString())}", 

        "combine_script": "combined = [:]; for (tmp in _agg.prd) { if(!combined[tmp]) { combined[tmp] = 1 } }; return combined",

        "reduce_script": "reduced = [:]; for (a in _aggs) {  for (entry in a) { word = entry.key; if (!reduced[word] ) { reduced[word] = entry.value; }  } }; return reduced"

      } 

    }



返回array

"scripted_terms": { 

      "scripted_metric": { 

        "init_script": "_agg[\"prd\"] = []", 

        "map_script": "if(doc[\"cat2_id\"].value) {_agg.prd.add(doc[\"cat2_id\"].value.toString())}", 

        "combine_script": "combined = [:]; for (tmp in _agg.prd) { if(!combined[tmp]) { combined[tmp] = 1 } }; return combined",

        "reduce_script": "reduced = []; for (a in _aggs) { for (entry in a) {  reduced.add(entry.key);  } }; return reduced"

      } 

    }


统计求和

"agg1" : {

      "scripted_metric" : {

        "init_script" : {

          "inline" : "_agg[\"prd\"] = []"

        },

        "map_script" : {

          "inline" : "if(doc[\"cat2_id\"].value) {_agg.prd.add(doc[\"cat2_id\"].value.toString())}"

        },

        "combine_script" : {

          "inline" : "combined = [:]; for (tmp in _agg.prd) { if(!combined[tmp]) { combined[tmp] = 1 } else { combined[tmp]=combined[tmp]+1 } }; return combined"

        },

        "reduce_script" : {

          "inline" : "reduced = [:]; for (a in _aggs) {  for (entry in a) { word = entry.key; if (!reduced[word] ) { reduced[word] = entry.value; } else { reduced[word]=reduced[word]+entry.value}  } }; return reduced"

        }

      }

    }