ElasticsearchHelper { public Terms getNestedAggregation(SearchResponse searchResponse, String nestedAggregationName, String subAggregationName) { Aggregations searchResponseAggregations = getAggregationsFromSearchResponse(searchResponse); Nested nestedAggregation = searchResponseAggregations.get(nestedAggregationName); if (nestedAggregation == null) { LOGGER.error("Failed to retrieve \"{}\" nested aggregation from the search response. searchResponse={}", nestedAggregationName, jsonHelper.objectToJson(searchResponse)); throw new IllegalStateException("Invalid search result."); } Aggregations nestedAggregationAggregations = getAggregationsFromNestedAggregation(nestedAggregation, searchResponse); Terms subAggregation = nestedAggregationAggregations.get(subAggregationName); if (subAggregation == null) { LOGGER.error("Failed to retrieve \"{}\" sub-aggregation from \"{}\" nested aggregation. searchResponse={} nestedAggregation={}", subAggregationName, nestedAggregationName, jsonHelper.objectToJson(searchResponse), jsonHelper.objectToJson(nestedAggregation)); throw new IllegalStateException("Invalid search result."); } return subAggregation; }  SearchRequestBuilder addFacetFieldAggregations(Set<String> facetFieldsList, SearchRequestBuilder searchRequestBuilder); BoolQueryBuilder addIndexSearchFilterBooleanClause(List<IndexSearchFilter> indexSearchFilters, String bdefActiveIndex, String tagActiveIndex); List<ResultTypeIndexSearchResponseDto> getResultTypeIndexSearchResponseDto(SearchResponse searchResponse); List<TagTypeIndexSearchResponseDto> getTagTagIndexSearchResponseDto(SearchResponse searchResponse); List<TagTypeIndexSearchResponseDto> getNestedTagTagIndexSearchResponseDto(SearchResponse searchResponse); List<Facet> getFacetsResponse(ElasticsearchResponseDto elasticsearchResponseDto, final String bdefActiveIndex, final String tagActiveIndex); Terms getAggregation(SearchResponse searchResponse, String aggregationName); Terms getNestedAggregation(SearchResponse searchResponse, String nestedAggregationName, String subAggregationName); String getSearchIndexType(final String indexName, final String bdefActiveIndex, final String tagActiveIndex); static final String DISPLAY_NAME_SOURCE; static final String NESTED_BDEFTAGS_PATH; static final String TAGTYPE_CODE_FIELD_TAG_INDEX; static final String TAGTYPE_NAME_FIELD_TAG_INDEX; static final String TAG_CODE_FIELD_TAG_INDEX; static final String TAG_NAME_FIELD_TAG_INDEX; static final String BDEF_TAGTYPE_CODE_FIELD; static final String BDEF_TAGTYPE_NAME_FIELD; static final String BDEF_TAG_CODE_FIELD; static final String BDEF_TAG_NAME_FIELD; static final String TAG_TAG_CODE_FIELD; static final String TAG_TAGTYPE_CODE_FIELD; static final String TAG_FACET_AGGS; static final String TAGTYPE_CODE_AGGREGATION; static final String TAGTYPE_NAME_AGGREGATION; static final String TAG_CODE_AGGREGATION; static final String TAG_NAME_AGGREGATION; static final String TAG_FACET; static final String RESULT_TYPE_FACET; static final String TAG_TYPE_FACET_AGGS; static final String RESULT_TYPE_AGGS; static final String RESULT_TYPE_FIELD; }
@Test public void testGetNestedAggregation() { Terms subAggregation = mock(Terms.class); Aggregations nestedAggregations = mock(Aggregations.class); when(nestedAggregations.get(SUB_AGGREGATION_NAME)).thenReturn(subAggregation); Nested nestedAggregation = mock(Nested.class); when(nestedAggregation.getAggregations()).thenReturn(nestedAggregations); Aggregations searchResponseAggregations = mock(Aggregations.class); when(searchResponseAggregations.get(NESTED_AGGREGATION_NAME)).thenReturn(nestedAggregation); SearchResponse searchResponse = mock(SearchResponse.class); when(searchResponse.getAggregations()).thenReturn(searchResponseAggregations); Terms result = elasticsearchHelper.getNestedAggregation(searchResponse, NESTED_AGGREGATION_NAME, SUB_AGGREGATION_NAME); verifyNoMoreInteractionsHelper(); assertEquals(subAggregation, result); }
@Test public void testGetNestedAggregationNestedAggregationIsNull() { Aggregations searchResponseAggregations = mock(Aggregations.class); when(searchResponseAggregations.get(NESTED_AGGREGATION_NAME)).thenReturn(null); SearchResponse searchResponse = mock(SearchResponse.class); when(searchResponse.getAggregations()).thenReturn(searchResponseAggregations); when(jsonHelper.objectToJson(searchResponse)).thenReturn(SEARCH_RESPONSE_JSON_STRING); try { elasticsearchHelper.getNestedAggregation(searchResponse, NESTED_AGGREGATION_NAME, SUB_AGGREGATION_NAME); fail(); } catch (IllegalStateException e) { assertEquals("Invalid search result.", e.getMessage()); } verify(jsonHelper).objectToJson(searchResponse); verifyNoMoreInteractionsHelper(); }
@Test public void testGetNestedAggregationSubAggregationIsNull() { Aggregations nestedAggregations = mock(Aggregations.class); when(nestedAggregations.get(SUB_AGGREGATION_NAME)).thenReturn(null); Nested nestedAggregation = mock(Nested.class); when(nestedAggregation.getAggregations()).thenReturn(nestedAggregations); Aggregations searchResponseAggregations = mock(Aggregations.class); when(searchResponseAggregations.get(NESTED_AGGREGATION_NAME)).thenReturn(nestedAggregation); SearchResponse searchResponse = mock(SearchResponse.class); when(searchResponse.getAggregations()).thenReturn(searchResponseAggregations); when(jsonHelper.objectToJson(searchResponse)).thenReturn(SEARCH_RESPONSE_JSON_STRING); when(jsonHelper.objectToJson(nestedAggregation)).thenReturn(NESTED_AGGREGATION_JSON_STRING); try { elasticsearchHelper.getNestedAggregation(searchResponse, NESTED_AGGREGATION_NAME, SUB_AGGREGATION_NAME); fail(); } catch (IllegalStateException e) { assertEquals("Invalid search result.", e.getMessage()); } verify(jsonHelper).objectToJson(searchResponse); verify(jsonHelper).objectToJson(nestedAggregation); verifyNoMoreInteractionsHelper(); }