@Override
    @SuppressWarnings("nls")
    public ClientUsagePerApiBean getClientUsagePerApi(String organizationId, String clientId,
            String version, DateTime from, DateTime to) {
        ClientUsagePerApiBean rval = new ClientUsagePerApiBean();

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

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

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

            ParsedStringTerms aggregation = response.getAggregations().get("usage_by_api");
            if(aggregation != null) {
                List<ParsedStringTerms.ParsedBucket> buckets = (List<ParsedStringTerms.ParsedBucket>) aggregation.getBuckets();
                for (ParsedStringTerms.ParsedBucket entry : buckets) {
                    rval.getData().put(entry.getKeyAsString(), entry.getDocCount());
                }
            }
        } catch (IOException e) {
            LOGGER.error(e);
        }
        return rval;
    }
@see io.apiman.manager.api.core.IMetricsAccessor#getClientUsagePerApi(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