org.elasticsearch.search.aggregations.bucket.nested.Nested Java Examples

The following examples show how to use org.elasticsearch.search.aggregations.bucket.nested.Nested. 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: AnalyticsServiceElasticsearch.java    From hawkular-apm with Apache License 2.0 6 votes vote down vote up
private static TimeseriesStatistics toTimeseriesStatistics(Bucket bucket) {
    Stats stat = bucket.getAggregations().get("stats");

    long faultCount = bucket.getAggregations()
            .<Nested>get("nested").getAggregations()
            .<Filter>get("faults").getDocCount();

    TimeseriesStatistics s = new TimeseriesStatistics();
    s.setTimestamp(bucket.getKeyAsDate().getMillis());
    s.setAverage((long)stat.getAvg());
    s.setMin((long)stat.getMin());
    s.setMax((long)stat.getMax());
    s.setCount(stat.getCount());
    s.setFaultCount(faultCount);
    return s;
}
 
Example #2
Source File: ElasticsearchHelper.java    From herd with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a representation of a set of aggregations from the nested aggregation. This method also validates that the retrieved object is not null.
 *
 * @param nestedAggregation the nested aggregation
 * @param searchResponse the response of the search request
 *
 * @return the aggregations
 */
protected Aggregations getAggregationsFromNestedAggregation(Nested nestedAggregation, SearchResponse searchResponse)
{
    // Retrieve the aggregations.
    Aggregations aggregations = nestedAggregation.getAggregations();

    // Fail if the retrieved object is null.
    if (aggregations == null)
    {
        // Log the error along with the nested aggregation contents.
        LOGGER.error("Failed to retrieve aggregations from the nested aggregation. searchResponse={} nestedAggregation={}",
            jsonHelper.objectToJson(searchResponse), jsonHelper.objectToJson(nestedAggregation));

        // Throw an exception.
        throw new IllegalStateException("Invalid search result.");
    }

    return aggregations;
}
 
Example #3
Source File: ElasticSearchHelperTest.java    From herd with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetNestedTagTagIndexSearchResponseDtoSearchResponseParameter()
{
    SearchResponse searchResponse = mock(SearchResponse.class);
    Aggregations aggregations = mock(Aggregations.class);

    when(searchResponse.getAggregations()).thenReturn(aggregations);

    Nested nestedAggregation = mock(Nested.class);
    when(aggregations.get(TAG_FACET_AGGS)).thenReturn(nestedAggregation);

    Aggregations aggregationAggregations = mock(Aggregations.class);
    when(nestedAggregation.getAggregations()).thenReturn(aggregationAggregations);

    Terms subAggregation = mock(Terms.class);
    when(aggregationAggregations.get(TAGTYPE_CODE_AGGREGATION)).thenReturn(subAggregation);

    List<TagTypeIndexSearchResponseDto> result = elasticsearchHelper.getNestedTagTagIndexSearchResponseDto(searchResponse);
    assertThat("Result is null.", result, is(notNullValue()));
}
 
Example #4
Source File: ElasticsearchHelper.java    From herd with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the sub-aggregation that is associated with the specified nested aggregation. This method also validates that the retrieved sub-aggregation
 * exists.
 *
 * @param searchResponse the response of the search request
 * @param nestedAggregationName the name of the nested aggregation
 * @param subAggregationName the name of the sub-aggregation
 *
 * @return the aggregation
 */
public Terms getNestedAggregation(SearchResponse searchResponse, String nestedAggregationName, String subAggregationName)
{
    // Retrieve the aggregations from the search response.
    Aggregations searchResponseAggregations = getAggregationsFromSearchResponse(searchResponse);

    // Retrieve the nested aggregation.
    Nested nestedAggregation = searchResponseAggregations.get(nestedAggregationName);

    // Fail if the retrieved nested aggregation is null.
    if (nestedAggregation == null)
    {
        // Log the error along with the search response contents.
        LOGGER.error("Failed to retrieve \"{}\" nested aggregation from the search response. searchResponse={}", nestedAggregationName,
            jsonHelper.objectToJson(searchResponse));

        // Throw an exception.
        throw new IllegalStateException("Invalid search result.");
    }

    // Retrieve the aggregations from the nested aggregation.
    Aggregations nestedAggregationAggregations = getAggregationsFromNestedAggregation(nestedAggregation, searchResponse);

    // Retrieve the sub-aggregation.
    Terms subAggregation = nestedAggregationAggregations.get(subAggregationName);

    // Fail if retrieved sub-aggregation is null.
    if (subAggregation == null)
    {
        // Log the error along with the search response contents.
        LOGGER.error("Failed to retrieve \"{}\" sub-aggregation from \"{}\" nested aggregation. searchResponse={} nestedAggregation={}", subAggregationName,
            nestedAggregationName, jsonHelper.objectToJson(searchResponse), jsonHelper.objectToJson(nestedAggregation));

        // Throw an exception.
        throw new IllegalStateException("Invalid search result.");
    }

    return subAggregation;
}
 
Example #5
Source File: ElasticSearchHelperTest.java    From herd with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetNestedAggregation()
{
    // Create a mock sub-aggregation.
    Terms subAggregation = mock(Terms.class);

    // Create mock nested aggregations.
    Aggregations nestedAggregations = mock(Aggregations.class);
    when(nestedAggregations.get(SUB_AGGREGATION_NAME)).thenReturn(subAggregation);

    // Create a mock nested aggregation.
    Nested nestedAggregation = mock(Nested.class);
    when(nestedAggregation.getAggregations()).thenReturn(nestedAggregations);

    // Create mock search response aggregations.
    Aggregations searchResponseAggregations = mock(Aggregations.class);
    when(searchResponseAggregations.get(NESTED_AGGREGATION_NAME)).thenReturn(nestedAggregation);

    // Create a mock search response.
    SearchResponse searchResponse = mock(SearchResponse.class);
    when(searchResponse.getAggregations()).thenReturn(searchResponseAggregations);

    // Call the method under test.
    Terms result = elasticsearchHelper.getNestedAggregation(searchResponse, NESTED_AGGREGATION_NAME, SUB_AGGREGATION_NAME);

    // Verify the external calls.
    verifyNoMoreInteractionsHelper();

    // Validate the result.
    assertEquals(subAggregation, result);
}
 
Example #6
Source File: ElasticSearchHelperTest.java    From herd with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetNestedAggregationSubAggregationIsNull()
{
    // Create mock nested aggregations.
    Aggregations nestedAggregations = mock(Aggregations.class);
    when(nestedAggregations.get(SUB_AGGREGATION_NAME)).thenReturn(null);

    // Create a mock nested aggregation.
    Nested nestedAggregation = mock(Nested.class);
    when(nestedAggregation.getAggregations()).thenReturn(nestedAggregations);

    // Create mock search response aggregations.
    Aggregations searchResponseAggregations = mock(Aggregations.class);
    when(searchResponseAggregations.get(NESTED_AGGREGATION_NAME)).thenReturn(nestedAggregation);

    // Create a mock search response.
    SearchResponse searchResponse = mock(SearchResponse.class);
    when(searchResponse.getAggregations()).thenReturn(searchResponseAggregations);

    // Mock the external calls.
    when(jsonHelper.objectToJson(searchResponse)).thenReturn(SEARCH_RESPONSE_JSON_STRING);
    when(jsonHelper.objectToJson(nestedAggregation)).thenReturn(NESTED_AGGREGATION_JSON_STRING);

    // Try to call the method under test.
    try
    {
        elasticsearchHelper.getNestedAggregation(searchResponse, NESTED_AGGREGATION_NAME, SUB_AGGREGATION_NAME);
        fail();
    }
    catch (IllegalStateException e)
    {
        assertEquals("Invalid search result.", e.getMessage());
    }

    // Verify the external calls.
    verify(jsonHelper).objectToJson(searchResponse);
    verify(jsonHelper).objectToJson(nestedAggregation);
    verifyNoMoreInteractionsHelper();
}
 
Example #7
Source File: ElasticSearchHelperTest.java    From herd with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetAggregationsFromNestedAggregationAggregationsSetIsNull()
{
    // Create a mock search response.
    SearchResponse searchResponse = mock(SearchResponse.class);

    // Create a mock nested aggregation.
    Nested nestedAggregation = mock(Nested.class);
    when(nestedAggregation.getAggregations()).thenReturn(null);

    // Mock the external calls.
    when(jsonHelper.objectToJson(searchResponse)).thenReturn(SEARCH_RESPONSE_JSON_STRING);
    when(jsonHelper.objectToJson(nestedAggregation)).thenReturn(NESTED_AGGREGATION_JSON_STRING);

    // Try to call the method under test.
    try
    {
        elasticsearchHelper.getAggregationsFromNestedAggregation(nestedAggregation, searchResponse);
    }
    catch (IllegalStateException e)
    {
        assertEquals("Invalid search result.", e.getMessage());
    }

    // Verify the external calls.
    verify(jsonHelper).objectToJson(searchResponse);
    verify(jsonHelper).objectToJson(nestedAggregation);
    verifyNoMoreInteractionsHelper();
}
 
Example #8
Source File: AggregateResponseParser.java    From molgenis with GNU Lesser General Public License v3.0 5 votes vote down vote up
private Aggregations removeNesting(Aggregations aggs) {
  if (Iterables.size(aggs) != 1) {
    throw new RuntimeException("Invalid number of aggregations");
  }
  Aggregation agg = aggs.iterator().next();
  if (!(agg instanceof Nested)) {
    throw new RuntimeException("Aggregation is not a nested aggregation");
  }
  return ((Nested) agg).getAggregations();
}
 
Example #9
Source File: JdbcResponseExtractor.java    From elasticsearch-sql with MIT License 4 votes vote down vote up
private void parseNestedAggregation(Aggregation aggregation, Map<String, Object> aggMap) {
    Nested nested = (Nested) aggregation;
    if (nested.getAggregations() != null && nested.getAggregations().asList().size() > 0) {
        parseAggregations(nested.getAggregations(), aggMap, null);
    }
}
 
Example #10
Source File: SimpleSearchQueryBuilder.java    From onetwo with Apache License 2.0 4 votes vote down vote up
public Nested getNestedAggregation(String name) {
	this.checkAggs();
	Nested nested = aggregations.get(name);
	return nested;
}