Java Code Examples for io.searchbox.core.SearchResult#getAggregations()

The following examples show how to use io.searchbox.core.SearchResult#getAggregations() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ElasticsearchHelper.java    From herd with Apache License 2.0 6 votes vote down vote up
/**
 * Creates result type facet response dto
 *
 * @param searchResult search result
 *
 * @return result type facet response dto list
 */
public List<ResultTypeIndexSearchResponseDto> getResultTypeIndexSearchResponseDto(SearchResult searchResult)
{
    MetricAggregation metricAggregation = searchResult.getAggregations();
    TermsAggregation resultTypeAggregation = metricAggregation.getTermsAggregation(RESULT_TYPE_AGGS);

    List<TermsAggregation.Entry> buckets = resultTypeAggregation.getBuckets();

    List<ResultTypeIndexSearchResponseDto> resultTypeIndexSearchResponseDtos = new ArrayList<>();

    for (TermsAggregation.Entry entry : buckets)
    {
        ResultTypeIndexSearchResponseDto dto = new ResultTypeIndexSearchResponseDto();
        dto.setResultTypeCode(entry.getKeyAsString());
        dto.setResultTypeDisplayName(entry.getKeyAsString());
        dto.setCount(entry.getCount());
        resultTypeIndexSearchResponseDtos.add(dto);
    }

    return resultTypeIndexSearchResponseDtos;
}
 
Example 2
Source File: ElasticsearchHelper.java    From herd with Apache License 2.0 5 votes vote down vote up
/**
 * create tag tag index response dto
 *
 * @param searchResult search result
 *
 * @return tag type index search response dto list
 */
public List<TagTypeIndexSearchResponseDto> getNestedTagTagIndexSearchResponseDto(SearchResult searchResult)
{
    MetricAggregation metricAggregation = searchResult.getAggregations();
    MetricAggregation tagFacetAggregation = metricAggregation.getSumAggregation(TAG_FACET_AGGS);
    TermsAggregation tagTypeCodesAggregation = tagFacetAggregation.getTermsAggregation(TAGTYPE_CODE_AGGREGATION);
    return getTagTypeIndexSearchResponseDtosFromTermsAggregation(tagTypeCodesAggregation);
}
 
Example 3
Source File: ESRegistry.java    From apiman with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("nls")
public void listOrgs(IAsyncResultHandler<List<String>> handler) {
    try {
        String query =
                "{\n" +
                "    \"aggs\" : {\n" +
                "        \"all_orgs\" : {\n" +
                "            \"terms\" : { \"field\" : \"organizationId\" }\n" + // i.e. only records containing an orgId field.
                "        }\n" +
                "    }\n" +
                "}";
        Search search = new Search.Builder(query)
                .addIndex(getIndexName())
                .setParameter(Parameters.SIZE, 0)
                .build();
        SearchResult response = getClient().execute(search);
        // Aggregations section
        MetricAggregation aggregation = response.getAggregations();
        // Look at the terms subsection
        TermsAggregation terms = aggregation.getTermsAggregation("all_orgs");
        // Grab only the name of each aggregation (we don't care about count
        List<String> results = terms.getBuckets().stream()
                .map(TermsAggregation.Entry::getKey)
                .collect(Collectors.toList());
        handler.handle(AsyncResultImpl.create(results));
    } catch (IOException e) {
        handler.handle(AsyncResultImpl.create(e));
    }
}
 
Example 4
Source File: ESMetricsAccessor.java    From apiman with Apache License 2.0 4 votes vote down vote up
/**
 * @see io.apiman.manager.api.core.IMetricsAccessor#getUsage(java.lang.String, java.lang.String, java.lang.String, io.apiman.manager.api.beans.metrics.HistogramIntervalType, org.joda.time.DateTime, org.joda.time.DateTime)
 */
@SuppressWarnings("nls")
@Override
public UsageHistogramBean getUsage(String organizationId, String apiId, String version,
        HistogramIntervalType interval, DateTime from, DateTime to) {
    UsageHistogramBean rval = new UsageHistogramBean();
    Map<String, UsageDataPoint> index = generateHistogramSkeleton(rval, from, to, interval, UsageDataPoint.class);

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

        Map<String, String> params = new HashMap<>();
        params.put("from", formatDate(from));
        params.put("to", formatDate(to));
        params.put("apiOrgId", organizationId.replace('"', '_'));
        params.put("apiId", apiId.replace('"', '_'));
        params.put("apiVersion", version.replace('"', '_'));
        params.put("interval", interval.name());
        StrSubstitutor ss = new StrSubstitutor(params);
        query = ss.replace(query);

        Search search = new Search.Builder(query).addIndex(INDEX_NAME).addType("request").build();
        SearchResult response = getEsClient().execute(search);
        MetricAggregation aggregations = response.getAggregations();
        DateHistogramAggregation aggregation = aggregations.getDateHistogramAggregation("histogram");
        if (aggregation != null) {
            List<DateHistogram> buckets = aggregation.getBuckets();
            for (DateHistogram entry : buckets) {
                String keyAsString = entry.getTimeAsString();
                if (index.containsKey(keyAsString)) {
                    index.get(keyAsString).setCount(entry.getCount());
                }
            }
        }
    } catch (IOException e) {
        log.error(e);
    }

    return rval;
}
 
Example 5
Source File: ESMetricsAccessor.java    From apiman with Apache License 2.0 4 votes vote down vote up
/**
 * @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)
 */
@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, String> params = new HashMap<>();
        params.put("from", formatDate(from));
        params.put("to", formatDate(to));
        params.put("apiOrgId", organizationId.replace('"', '_'));
        params.put("apiId", apiId.replace('"', '_'));
        params.put("apiVersion", version.replace('"', '_'));
        StrSubstitutor ss = new StrSubstitutor(params);
        query = ss.replace(query);

        Search search = new Search.Builder(query).addIndex(INDEX_NAME).addType("request").build();
        SearchResult response = getEsClient().execute(search);
        MetricAggregation aggregations = response.getAggregations();
        ApimanTermsAggregation aggregation = aggregations.getAggregation("usage_by_client", ApimanTermsAggregation.class); //$NON-NLS-1$
        if (aggregation != null) {
            List<ApimanTermsAggregation.Entry> buckets = aggregation.getBuckets();
            int counter = 0;
            for (ApimanTermsAggregation.Entry entry : buckets) {
                rval.getData().put(entry.getKey(), entry.getCount());
                counter++;
                if (counter > 5) {
                    break;
                }
            }
        }
    } catch (IOException e) {
        log.error(e);
    }

    return rval;
}
 
Example 6
Source File: ESMetricsAccessor.java    From apiman with Apache License 2.0 4 votes vote down vote up
/**
 * @see io.apiman.manager.api.core.IMetricsAccessor#getUsagePerPlan(java.lang.String, java.lang.String, java.lang.String, org.joda.time.DateTime, org.joda.time.DateTime)
 */
@SuppressWarnings("nls")
@Override
public UsagePerPlanBean getUsagePerPlan(String organizationId, String apiId, String version,
        DateTime from, DateTime to) {
    UsagePerPlanBean rval = new UsagePerPlanBean();

    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_plan\": {" +
                "            \"terms\": {" +
                "                \"field\": \"planId\"" +
                "            }" +
                "        }" +
                "    }" +
                "}";

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

        Search search = new Search.Builder(query).addIndex(INDEX_NAME).addType("request").build();
        SearchResult response = getEsClient().execute(search);
        MetricAggregation aggregations = response.getAggregations();
        ApimanTermsAggregation aggregation = aggregations.getAggregation("usage_by_plan", ApimanTermsAggregation.class); //$NON-NLS-1$
        if (aggregation != null) {
            List<ApimanTermsAggregation.Entry> buckets = aggregation.getBuckets();
            for (ApimanTermsAggregation.Entry entry : buckets) {
                rval.getData().put(entry.getKey(), entry.getCount());
            }
        }
    } catch (IOException e) {
        log.error(e);
    }

    return rval;
}
 
Example 7
Source File: ESMetricsAccessor.java    From apiman with Apache License 2.0 4 votes vote down vote up
/**
 * @see io.apiman.manager.api.core.IMetricsAccessor#getResponseStats(java.lang.String, java.lang.String, java.lang.String, io.apiman.manager.api.beans.metrics.HistogramIntervalType, org.joda.time.DateTime, org.joda.time.DateTime)
 */
@SuppressWarnings("nls")
@Override
public ResponseStatsHistogramBean getResponseStats(String organizationId, String apiId,
        String version, HistogramIntervalType interval, DateTime from, DateTime to) {
    ResponseStatsHistogramBean rval = new ResponseStatsHistogramBean();
    Map<String, ResponseStatsDataPoint> index = generateHistogramSkeleton(rval, from, to, interval, ResponseStatsDataPoint.class);

    try {
        String query =
                "{" +
                "    \"query\": {" +
                "        \"bool\": {" +
                "            \"filter\": [{" +
                "                \"term\": {" +
                "                    \"apiOrgId\": \"${apiOrgId}\"" +
                "                }" +
                "            }, {" +
                "                \"term\": {" +
                "                    \"apiId\": \"${apiId}\"" +
                "                }" +
                "            }, {" +
                "                \"term\": {" +
                "                    \"apiVersion\": \"${apiVersion}\"" +
                "                }" +
                "            }, {" +
                "                \"range\": {" +
                "                    \"requestStart\": {" +
                "                        \"gte\": \"${from}\"," +
                "                        \"lte\": \"${to}\"" +
                "                    }" +
                "                }" +
                "            }]" +
                "        }" +
                "    }," +
                "    \"size\": 0," +
                "    \"aggs\": {" +
                "        \"histogram\": {" +
                "            \"date_histogram\": {" +
                "                \"field\": \"requestStart\"," +
                "                \"interval\": \"${interval}\"" +
                "            }," +
                "            \"aggs\": {" +
                "                \"total_failures\": {" +
                "                    \"filter\": {" +
                "                        \"term\": {" +
                "                            \"failure\": true" +
                "                        }" +
                "                    }" +
                "                }," +
                "                \"total_errors\": {" +
                "                    \"filter\": {" +
                "                        \"term\": {" +
                "                            \"error\": true" +
                "                        }" +
                "                    }" +
                "                }" +
                "            }" +
                "        }" +
                "    }" +
                "}";

        Map<String, String> params = new HashMap<>();
        params.put("from", formatDate(from));
        params.put("to", formatDate(to));
        params.put("apiOrgId", organizationId.replace('"', '_'));
        params.put("apiId", apiId.replace('"', '_'));
        params.put("apiVersion", version.replace('"', '_'));
        params.put("interval", interval.name());
        StrSubstitutor ss = new StrSubstitutor(params);
        query = ss.replace(query);

        Search search = new Search.Builder(query).addIndex(INDEX_NAME).addType("request").build();
        SearchResult response = getEsClient().execute(search);
        MetricAggregation aggregations = response.getAggregations();
        DateHistogramAggregation aggregation = aggregations.getDateHistogramAggregation("histogram");
        if (aggregation != null) {
            List<DateHistogram> buckets = aggregation.getBuckets();
            for (DateHistogram entry : buckets) {
                String keyAsString = entry.getTimeAsString();
                if (index.containsKey(keyAsString)) {
                    FilterAggregation totalFailuresAgg = entry.getFilterAggregation("total_failures");
                    FilterAggregation totalErrorsAgg = entry.getFilterAggregation("total_errors");
                    long failures = totalFailuresAgg.getCount();
                    long errors = totalErrorsAgg.getCount();
                    ResponseStatsDataPoint point = index.get(keyAsString);
                    point.setTotal(entry.getCount());
                    point.setFailures(failures);
                    point.setErrors(errors);
                }
            }
        }
    } catch (IOException e) {
        log.error(e);
    }

    return rval;
}
 
Example 8
Source File: ESMetricsAccessor.java    From apiman with Apache License 2.0 4 votes vote down vote up
/**
 * @see io.apiman.manager.api.core.IMetricsAccessor#getResponseStatsPerClient(java.lang.String, java.lang.String, java.lang.String, org.joda.time.DateTime, org.joda.time.DateTime)
 */
@Override
@SuppressWarnings("nls")
public ResponseStatsPerClientBean getResponseStatsPerClient(String organizationId, String apiId,
        String version, DateTime from, DateTime to) {
    ResponseStatsPerClientBean rval = new ResponseStatsPerClientBean();

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

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

        Search search = new Search.Builder(query).addIndex(INDEX_NAME).addType("request").build();
        SearchResult response = getEsClient().execute(search);
        MetricAggregation aggregations = response.getAggregations();
        ApimanTermsAggregation aggregation = aggregations.getAggregation("by_client", ApimanTermsAggregation.class); //$NON-NLS-1$
        if (aggregation != null) {
            List<ApimanTermsAggregation.Entry> buckets = aggregation.getBuckets();
            int counter = 0;
            for (ApimanTermsAggregation.Entry entry : buckets) {
                rval.addDataPoint(entry.getKey(), entry.getCount(), entry.getFilterAggregation("total_failures").getCount(),
                        entry.getFilterAggregation("total_errors").getCount());
                counter++;
                if (counter > 10) {
                    break;
                }
            }
        }
    } catch (IOException e) {
        log.error(e);
    }

    return rval;
}
 
Example 9
Source File: ESMetricsAccessor.java    From apiman with Apache License 2.0 4 votes vote down vote up
/**
 * @see io.apiman.manager.api.core.IMetricsAccessor#getResponseStatsPerPlan(java.lang.String, java.lang.String, java.lang.String, org.joda.time.DateTime, org.joda.time.DateTime)
 */
@Override
@SuppressWarnings("nls")
public ResponseStatsPerPlanBean getResponseStatsPerPlan(String organizationId, String apiId,
        String version, DateTime from, DateTime to) {
    ResponseStatsPerPlanBean rval = new ResponseStatsPerPlanBean();

    try {
        String query =
                "{" +
                "    \"query\": {" +
                "        \"bool\": {" +
                "            \"filter\": [{" +
                "                \"term\": {" +
                "                    \"apiOrgId\": \"${apiOrgId}\"" +
                "                }" +
                "            }, {" +
                "                \"term\": {" +
                "                    \"apiId\": \"${apiId}\"" +
                "                }" +
                "            }, {" +
                "                \"term\": {" +
                "                    \"apiVersion\": \"${apiVersion}\"" +
                "                }" +
                "            }, {" +
                "                \"range\": {" +
                "                    \"requestStart\": {" +
                "                        \"gte\": \"${from}\"," +
                "                        \"lte\": \"${to}\"" +
                "                    }" +
                "                }" +
                "            }]" +
                "        }" +
                "    }," +
                "    \"size\": 0," +
                "    \"aggs\": {" +
                "        \"by_plan\": {" +
                "            \"terms\": {" +
                "                \"field\": \"planId\"" +
                "            }," +
                "            \"aggs\": {" +
                "                \"total_failures\": {" +
                "                    \"filter\": {" +
                "                        \"term\": {" +
                "                            \"failure\": true" +
                "                        }" +
                "                    }" +
                "                }," +
                "                \"total_errors\": {" +
                "                    \"filter\": {" +
                "                        \"term\": {" +
                "                            \"error\": true" +
                "                        }" +
                "                    }" +
                "                }" +
                "            }" +
                "        }" +
                "    }" +
                "}";

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

        Search search = new Search.Builder(query).addIndex(INDEX_NAME).addType("request").build();
        SearchResult response = getEsClient().execute(search);
        MetricAggregation aggregations = response.getAggregations();
        ApimanTermsAggregation aggregation = aggregations.getAggregation("by_plan", ApimanTermsAggregation.class); //$NON-NLS-1$
        if (aggregation != null) {
            List<ApimanTermsAggregation.Entry> buckets = aggregation.getBuckets();
            int counter = 0;
            for (ApimanTermsAggregation.Entry entry : buckets) {
                rval.addDataPoint(entry.getKey(), entry.getCount(), entry.getFilterAggregation("total_failures").getCount(),
                        entry.getFilterAggregation("total_errors").getCount());
                counter++;
                if (counter > 10) {
                    break;
                }
            }
        }
    } catch (IOException e) {
        log.error(e);
    }

    return rval;
}
 
Example 10
Source File: ESMetricsAccessor.java    From apiman with Apache License 2.0 4 votes vote down vote up
/**
 * @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)
 */
@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, String> params = new HashMap<>();
        params.put("from", formatDate(from));
        params.put("to", formatDate(to));
        params.put("clientOrgId", organizationId.replace('"', '_'));
        params.put("clientId", clientId.replace('"', '_'));
        params.put("clientVersion", version.replace('"', '_'));
        StrSubstitutor ss = new StrSubstitutor(params);
        query = ss.replace(query);

        Search search = new Search.Builder(query).addIndex(INDEX_NAME).addType("request").build();
        SearchResult response = getEsClient().execute(search);
        MetricAggregation aggregations = response.getAggregations();
        ApimanTermsAggregation aggregation = aggregations.getAggregation("usage_by_api", ApimanTermsAggregation.class); //$NON-NLS-1$
        if (aggregation != null) {
            List<ApimanTermsAggregation.Entry> buckets = aggregation.getBuckets();
            for (ApimanTermsAggregation.Entry entry : buckets) {
                rval.getData().put(entry.getKey(), entry.getCount());
            }
        }
    } catch (IOException e) {
        log.error(e);
    }

    return rval;

}
 
Example 11
Source File: ESRegistry.java    From apiman with Apache License 2.0 4 votes vote down vote up
@Override
@SuppressWarnings("nls")
public void listClients(String organizationId, int page, int pageSize, IAsyncResultHandler<List<String>> handler) {
    try {
        String query = "{" +
                "      \"query\": {" +
                "        \"bool\": {" +
                "          \"filter\": [" +
                "            {" +
                "              \"exists\": {" +
                "                \"field\": \"clientId\" " + // clientId
                "              }" +
                "            }," +
                "            {" +
                "              \"term\": {" +
                "                \"organizationId\": ? " +  // organizationId
                "              }" +
                "            }" +
                "          ]" +
                "        }" +
                "    }," +
                "    \"aggs\": {" +
                "        \"clients\": {" +
                "            \"terms\": {" +
                "                \"field\": \"clientId\" " + // only return aggregated clientId field
                "            }" +
                "        }" +
                "    }" +
                "}";
        String escaped = ESUtils.queryWithEscapedArgs(query, organizationId);
        Search search = new Search.Builder(escaped)
                .addIndex(getIndexName())
                .addType("client")
                .setParameter(Parameters.SIZE, 0)
                .build();
        SearchResult response = getClient().execute(search);
        // Aggregations section
        MetricAggregation aggregation = response.getAggregations();
        // Look at the terms subsection
        TermsAggregation terms = aggregation.getTermsAggregation("clients");
        // Grab only the name of each aggregation (we don't care about count [for now]).
        List<String> results = terms.getBuckets().stream()
                .map(TermsAggregation.Entry::getKey)
                .collect(Collectors.toList());
        handler.handle(AsyncResultImpl.create(results));
    } catch (IOException e) {
        handler.handle(AsyncResultImpl.create(e));
    }
}
 
Example 12
Source File: ESRegistry.java    From apiman with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("nls")
@Override
public void listApis(String organizationId, int page, int pageSize, IAsyncResultHandler<List<String>> handler) {
    try {
        String query = "{" +
                "      \"query\": {" +
                "        \"bool\": {" +
                "          \"filter\": [" +
                "            {" +
                "              \"exists\": {" +
                "                \"field\": \"apiId\" " + // must have field apiId
                "              }" +
                "            }," +
                "            {" +
                "              \"term\": {" +
                "                \"organizationId\": ? " +  // organizationId
                "              }" +
                "            }" +
                "          ]" +
                "        }" +
                "    }," +
                "    \"aggs\": {" +
                "        \"apis\": {" +
                "            \"terms\": {" +
                "                \"field\": \"apiId\" " + // only return aggregated apiId field
                "            }" +
                "        }" +
                "    }" +
                "}";
        String escaped = ESUtils.queryWithEscapedArgs(query, organizationId);
        Search search = new Search.Builder(escaped)
                .addIndex(getIndexName())
                .addType("api")
                .setParameter(Parameters.SIZE, 0)
                .build();
        SearchResult response = getClient().execute(search);
        // Aggregations section
        MetricAggregation aggregation = response.getAggregations();
        // Look at the terms subsection
        TermsAggregation terms = aggregation.getTermsAggregation("apis");
        // Grab only the name of each aggregation (we don't care about count [for now]).
        List<String> results = terms.getBuckets().stream()
                .map(TermsAggregation.Entry::getKey)
                .collect(Collectors.toList());
        handler.handle(AsyncResultImpl.create(results));
    } catch (IOException e) {
        handler.handle(AsyncResultImpl.create(e));
    }
}
 
Example 13
Source File: ESRegistry.java    From apiman with Apache License 2.0 4 votes vote down vote up
@Override
@SuppressWarnings("nls")
public void listClientVersions(String organizationId, String clientId, int page, int pageSize, IAsyncResultHandler<List<String>> handler) {
    try {
        String query = "{" +
                "  \"query\": {" +
                "    \"bool\": {" +
                "      \"filter\": [" +
                "        {" +
                "          \"term\": {" +
                "            \"organizationId\": ?" +  // organizationId
                "          }" +
                "        }," +
                "        {" +
                "          \"term\": {" +
                "            \"clientId\": ?" + // clientId
                "          }" +
                "        }" +
                "      ]" +
                "    }" +
                "  }," +
                "    \"aggs\": {" +
                "      \"client_versions\": {" +
                "        \"terms\": {" +
                "          \"field\": \"version\"" + // only return version fields of clients
                "        }" +
                "      }" +
                "    }" +
                "}";

        String escaped = ESUtils.queryWithEscapedArgs(query, organizationId, clientId);
        Search search = new Search.Builder(escaped)
                .addIndex(getIndexName())
                .addType("client")
                .setParameter(Parameters.SIZE, 0) // size zero to return only aggregate data (not raw query results)
                .build();
        SearchResult response = getClient().execute(search);
        // Aggregations section
        MetricAggregation aggregation = response.getAggregations();
        // Look at the terms subsection
        TermsAggregation terms = aggregation.getTermsAggregation("client_versions");
        // Grab only the name of each aggregation
        List<String> results = terms.getBuckets().stream()
                .map(TermsAggregation.Entry::getKey)
                .collect(Collectors.toList());
        handler.handle(AsyncResultImpl.create(results));
    } catch (IOException e) {
        handler.handle(AsyncResultImpl.create(e));
    }
}
 
Example 14
Source File: ESRegistry.java    From apiman with Apache License 2.0 4 votes vote down vote up
@Override
@SuppressWarnings("nls")
public void listApiVersions(String organizationId, String apiId, int page, int pageSize,
                            IAsyncResultHandler<List<String>> handler) {
    try {
        String query = "{" +
                "  \"query\": {" +
                "    \"bool\": {" +
                "      \"filter\": [" +
                "        {" +
                "          \"term\": {" +
                "            \"organizationId\": ?" + // organizationId
                "          }" +
                "        }," +
                "        {" +
                "          \"term\": {" +
                "            \"apiId\": ?" + // apiId
                "          }" +
                "        }" +
                "      ]" +
                "    }" +
                "  }," +
                "    \"aggs\": {" +
                "      \"api_versions\": {" +
                "        \"terms\": {" +
                "          \"field\": \"version\"" + // only return version fields of APIs
                "        }" +
                "      }" +
                "    }" +
                "}";
        String escaped = ESUtils.queryWithEscapedArgs(query, organizationId, apiId);
        Search search = new Search.Builder(escaped)
                .addIndex(getIndexName())
                .addType("api")
                .setParameter(Parameters.SIZE, 0)
                .build();
        SearchResult response = getClient().execute(search);
        // Aggregations section
        MetricAggregation aggregation = response.getAggregations();
        // Look at the terms subsection
        TermsAggregation terms = aggregation.getTermsAggregation("api_versions");
        // Grab only the name of each aggregation
        List<String> results = terms.getBuckets().stream()
                .map(TermsAggregation.Entry::getKey)
                .collect(Collectors.toList());
        handler.handle(AsyncResultImpl.create(results));
    } catch (IOException e) {
        handler.handle(AsyncResultImpl.create(e));
    }
}
 
Example 15
Source File: ElasticsearchHelper.java    From herd with Apache License 2.0 3 votes vote down vote up
/**
 * get tag index search response dto
 *
 * @param searchResult elastic search result
 *
 * @return list of tag type index search response dto
 */
public List<TagTypeIndexSearchResponseDto> getTagTagIndexSearchResponseDto(SearchResult searchResult)
{
    MetricAggregation metricAggregation = searchResult.getAggregations();
    TermsAggregation tagTypeFacetAggregation = metricAggregation.getTermsAggregation(TAG_TYPE_FACET_AGGS);
    return getTagTypeIndexSearchResponseDtosFromTermsAggregation(tagTypeFacetAggregation);
}