@SuppressWarnings("nls")
    @Override
    public UsagePerClientBean getUsagePerClient(String organizationId, String apiId, String version,
            DateTime from, DateTime to) {
        UsagePerClientBean rval = new UsagePerClientBean();

        try {
            String query =
                    "{" +
                    "    \"query\": {" +
                    "        \"bool\": {" +
                    "            \"filter\": [{" +
                    "                \"term\": {" +
                    "                    \"apiOrgId\": \"{{apiOrgId}}\"" +
                    "                }" +
                    "            }, {" +
                    "                \"term\": {" +
                    "                    \"apiId\": \"{{apiId}}\"" +
                    "                }" +
                    "            }, {" +
                    "                \"term\": {" +
                    "                    \"apiVersion\": \"{{apiVersion}}\"" +
                    "                }" +
                    "            }, {" +
                    "                \"range\": {" +
                    "                    \"requestStart\": {" +
                    "                        \"gte\": \"{{from}}\"," +
                    "                        \"lte\": \"{{to}}\"" +
                    "                    }" +
                    "                }" +
                    "            }]" +
                    "        }" +
                    "    }," +
                    "    \"size\": 0," +
                    "    \"aggs\": {" +
                    "        \"usage_by_client\": {" +
                    "            \"terms\": {" +
                    "                \"field\": \"clientId\"" +
                    "            }" +
                    "        }" +
                    "    }" +
                    "}";

            Map<String, Object> params = new HashMap<>();
            params.put("from", MetricsAccessorHelper.formatDate(from));
            params.put("to", MetricsAccessorHelper.formatDate(to));
            params.put("apiOrgId", organizationId.replace('"', '_'));
            params.put("apiId", apiId.replace('"', '_'));
            params.put("apiVersion", version.replace('"', '_'));

            SearchResponse response = this.doSearchTemplateRequest(query, params);

            List<ParsedStringTerms.ParsedBucket> buckets = (List<ParsedStringTerms.ParsedBucket>) ((ParsedStringTerms) response.getAggregations().get("usage_by_client")).getBuckets();
            int counter = 0;
            for (ParsedStringTerms.ParsedBucket entry : buckets) {
                rval.getData().put(entry.getKeyAsString(), entry.getDocCount());
                counter++;
                if (counter > 5) {
                    break;
                }
            }

        } catch (IOException e) {
            LOGGER.error(e);
        }

        return rval;
    }
@see io.apiman.manager.api.core.IMetricsAccessor#getUsagePerClient(java.lang.String, java.lang.String, java.lang.String, org.joda.time.DateTime, org.joda.time.DateTime)
manager/api/es/src/main/java/io/apiman/manager/api/es/EsMetricsAccessor.java