@Test public void testAddFacetFieldAggregations() { Set<String> facetSet = new HashSet<>(); //it is ok to pass a null search request builder, as empty facet set bypass the processing elasticsearchHelper.addFacetFieldAggregations(facetSet, null); }
/** * get tag index search response dto * * @param searchResponse elastic search response * * @return list of tag type index search response dto */ public List<TagTypeIndexSearchResponseDto> getTagTagIndexSearchResponseDto(SearchResponse searchResponse) { Terms aggregation = getAggregation(searchResponse, TAG_TYPE_FACET_AGGS); return getTagTypeIndexSearchResponseDtosFromTerms(aggregation); }
/** * Returns the aggregation that is associated with the specified name. This method also validates that the retrieved aggregation exists. * * @param searchResponse the response of the search request * @param aggregationName the name of the aggregation * * @return the aggregation */ public Terms getAggregation(SearchResponse searchResponse, String aggregationName) { // Retrieve the aggregations from the search response. Aggregations aggregations = getAggregationsFromSearchResponse(searchResponse); // Retrieve the specified aggregation. Terms aggregation = aggregations.get(aggregationName); // Fail if retrieved aggregation is null. if (aggregation == null) { // Log the error along with the search response contents. LOGGER.error("Failed to retrieve \"{}\" aggregation from the search response. searchResponse={}", aggregationName, jsonHelper.objectToJson(searchResponse)); // Throw an exception. throw new IllegalStateException("Invalid search result."); } return aggregation; }
/** * create tag tag index response dto * * @param searchResponse search response * * @return tag type index search response dto list */ public List<TagTypeIndexSearchResponseDto> getNestedTagTagIndexSearchResponseDto(SearchResponse searchResponse) { Terms tagTypeCodeAgg = getNestedAggregation(searchResponse, TAG_FACET_AGGS, TAGTYPE_CODE_AGGREGATION); return getTagTypeIndexSearchResponseDtosFromTerms(tagTypeCodeAgg); }
/** * Extracts facet information from a {@link SearchResult} object * * @param request The specified {@link IndexSearchRequest} * @param searchResult A given {@link SearchResult} to extract the facet information from * @param bdefActiveIndex the name of the active index for business object definitions * @param tagActiveIndex the name os the active index for tags * * @return A list of {@link Facet} objects */ private List<Facet> extractFacets(IndexSearchRequest request, SearchResult searchResult, final String bdefActiveIndex, final String tagActiveIndex) { ElasticsearchResponseDto elasticsearchResponseDto = new ElasticsearchResponseDto(); if (request.getFacetFields().contains(ElasticsearchHelper.TAG_FACET)) { elasticsearchResponseDto.setNestTagTypeIndexSearchResponseDtos(elasticsearchHelper.getNestedTagTagIndexSearchResponseDto(searchResult)); elasticsearchResponseDto.setTagTypeIndexSearchResponseDtos(elasticsearchHelper.getTagTagIndexSearchResponseDto(searchResult)); } if (request.getFacetFields().contains(ElasticsearchHelper.RESULT_TYPE_FACET)) { elasticsearchResponseDto.setResultTypeIndexSearchResponseDtos(elasticsearchHelper.getResultTypeIndexSearchResponseDto(searchResult)); } return elasticsearchHelper.getFacetsResponse(elasticsearchResponseDto, bdefActiveIndex, tagActiveIndex); }
Collections.singletonList(new ResultTypeIndexSearchResponseDto("type", 1, null)); when(elasticsearchHelper.getNestedTagTagIndexSearchResponseDto(searchResponse)).thenReturn(tagTypeIndexSearchResponseDtos); when(elasticsearchHelper.getResultTypeIndexSearchResponseDto(searchResponse)).thenReturn(resultTypeIndexSearchResponseDto); when(elasticsearchHelper .getFacetsResponse(any(ElasticsearchResponseDto.class), eq(BUSINESS_OBJECT_DEFINITION_SEARCH_INDEX_NAME), eq(TAG_SEARCH_INDEX_NAME))) .thenCallRealMethod(); when(elasticsearchHelper.addIndexSearchFilterBooleanClause(any(), any(), any())).thenReturn(boolQueryBuilder); when(elasticsearchHelper.addFacetFieldAggregations(any(), any(SearchRequestBuilder.class))).thenReturn(searchRequestBuilder); .addIndexSearchFilterBooleanClause(indexSearchRequest.getIndexSearchFilters(), BUSINESS_OBJECT_DEFINITION_SEARCH_INDEX_NAME, TAG_SEARCH_INDEX_NAME); verify(elasticsearchHelper).addFacetFieldAggregations(any(), any(SearchRequestBuilder.class)); verify(elasticsearchHelper).getNestedTagTagIndexSearchResponseDto(searchResult); verify(elasticsearchHelper).getTagTagIndexSearchResponseDto(searchResult); verify(elasticsearchHelper).getResultTypeIndexSearchResponseDto(searchResult); .getFacetsResponse(any(ElasticsearchResponseDto.class), eq(BUSINESS_OBJECT_DEFINITION_SEARCH_INDEX_NAME), eq(TAG_SEARCH_INDEX_NAME));
Aggregations searchResponseAggregations = getAggregationsFromSearchResponse(searchResponse); Aggregations nestedAggregationAggregations = getAggregationsFromNestedAggregation(nestedAggregation, searchResponse);
.filter(elasticsearchHelper.addIndexSearchFilterBooleanClause(indexSearchRequest.getIndexSearchFilters(), bdefActiveIndex, tagActiveIndex)); searchRequestBuilder = elasticsearchHelper.addFacetFieldAggregations(new HashSet<>(indexSearchRequest.getFacetFields()), searchRequestBuilder);
@Test public void testGetFacetsResponseWithEmptyResponseDto() { ElasticsearchResponseDto elasticsearchResponseDto = new ElasticsearchResponseDto(); List<Facet> facets = elasticsearchHelper.getFacetsResponse(elasticsearchResponseDto, BUSINESS_OBJECT_DEFINITION_SEARCH_INDEX_NAME, TAG_SEARCH_INDEX_NAME); assertThat("Facet size is not equal to 0.", facets.size(), equalTo(0)); }
tagTypeFacets.add(createTagTypeFacet(tagTypeIndexSearchResponseDto)); facets.add(createTagTypeFacet(tagTypeIndexDto)); String facetId = getSearchIndexType(resultTypeIndexSearchResponseDto.getResultTypeDisplayName(), bdefActiveIndex, tagActiveIndex); Facet resultTypeFacet = new Facet(facetId, resultTypeIndexSearchResponseDto.getCount(), FacetTypeEnum.RESULT_TYPE.value(), facetId, null); resultTypeFacets.add(resultTypeFacet);
/** * Creates result type facet response dto * * @param searchResponse search response * * @return result type facet response dto list */ public List<ResultTypeIndexSearchResponseDto> getResultTypeIndexSearchResponseDto(SearchResponse searchResponse) { List<ResultTypeIndexSearchResponseDto> list = new ArrayList<>(); Terms aggregation = getAggregation(searchResponse, RESULT_TYPE_AGGS); for (Terms.Bucket resultTypeEntry : aggregation.getBuckets()) { ResultTypeIndexSearchResponseDto dto = new ResultTypeIndexSearchResponseDto(); dto.setResultTypeCode(resultTypeEntry.getKeyAsString()); dto.setResultTypeDisplayName(resultTypeEntry.getKeyAsString()); dto.setCount(resultTypeEntry.getDocCount()); list.add(dto); } return list; }
@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); }
@Test public void testGetNestedTagTagIndexSearchResponseDtoSearchResultParameter() { SearchResult searchResult = mock(SearchResult.class); MetricAggregation metricAggregation = mock(MetricAggregation.class); SumAggregation tagFacetAggregation = mock(SumAggregation.class); TermsAggregation tagTypeCodesAggregation = mock(TermsAggregation.class); when(searchResult.getAggregations()).thenReturn(metricAggregation); when(metricAggregation.getSumAggregation(TAG_FACET_AGGS)).thenReturn(tagFacetAggregation); when(tagFacetAggregation.getTermsAggregation(TAGTYPE_CODE_AGGREGATION)).thenReturn(tagTypeCodesAggregation); List<TagTypeIndexSearchResponseDto> result = elasticsearchHelper.getNestedTagTagIndexSearchResponseDto(searchResult); assertThat("Result is null.", result, is(notNullValue())); }
@Test public void testGetResultTypeIndexSearchResponseDtoSearchResult() { SearchResult searchResult = mock(SearchResult.class); MetricAggregation metricAggregation = mock(MetricAggregation.class); TermsAggregation termsAggregation = mock(TermsAggregation.class); List<TermsAggregation.Entry> buckets = new ArrayList<>(); buckets.add(new TermsAggregation("TermAggregation", new JsonObject()).new Entry(new JsonObject(), "key", 1L)); when(searchResult.getAggregations()).thenReturn(metricAggregation); when(metricAggregation.getTermsAggregation(RESULT_TYPE_AGGS)).thenReturn(termsAggregation); when(termsAggregation.getBuckets()).thenReturn(buckets); List<ResultTypeIndexSearchResponseDto> result = elasticsearchHelper.getResultTypeIndexSearchResponseDto(searchResult); assertThat("Result is null.", result, is(notNullValue())); }
BoolQueryBuilder result = elasticsearchHelper.addIndexSearchFilterBooleanClause(indexSearchFilters, "bdefIndex", "tagIndex"); assertThat("Result is null.", result, is(notNullValue()));
@Test public void testGetSearchIndexType() { assertEquals(SearchIndexTypeEntity.SearchIndexTypes.BUS_OBJCT_DFNTN.name(), elasticsearchHelper .getSearchIndexType(BUSINESS_OBJECT_DEFINITION_SEARCH_INDEX_NAME, BUSINESS_OBJECT_DEFINITION_SEARCH_INDEX_NAME, TAG_SEARCH_INDEX_NAME)); assertEquals(SearchIndexTypeEntity.SearchIndexTypes.TAG.name(), elasticsearchHelper.getSearchIndexType(TAG_SEARCH_INDEX_NAME, BUSINESS_OBJECT_DEFINITION_SEARCH_INDEX_NAME, TAG_SEARCH_INDEX_NAME)); try { elasticsearchHelper.getSearchIndexType(SEARCH_INDEX_NAME, BUSINESS_OBJECT_DEFINITION_SEARCH_INDEX_NAME, TAG_SEARCH_INDEX_NAME); } catch (IllegalStateException e) { assertEquals(String.format("Search result index name \"%s\" does not match any of the active search indexes. bdefActiveIndex=%s tagActiveIndex=%s", SEARCH_INDEX_NAME, BUSINESS_OBJECT_DEFINITION_SEARCH_INDEX_NAME, TAG_SEARCH_INDEX_NAME), e.getMessage()); } }
@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(); }
/** * Extracts facet information from a {@link SearchResult} object * * @param request The specified {@link IndexSearchRequest} * @param searchResult A given {@link SearchResult} to extract the facet information from * @param bdefActiveIndex the name of the active index for business object definitions * @param tagActiveIndex the name os the active index for tags * * @return A list of {@link Facet} objects */ private List<Facet> extractFacets(IndexSearchRequest request, SearchResult searchResult, final String bdefActiveIndex, final String tagActiveIndex) { ElasticsearchResponseDto elasticsearchResponseDto = new ElasticsearchResponseDto(); if (request.getFacetFields().contains(ElasticsearchHelper.TAG_FACET)) { elasticsearchResponseDto.setNestTagTypeIndexSearchResponseDtos(elasticsearchHelper.getNestedTagTagIndexSearchResponseDto(searchResult)); elasticsearchResponseDto.setTagTypeIndexSearchResponseDtos(elasticsearchHelper.getTagTagIndexSearchResponseDto(searchResult)); } if (request.getFacetFields().contains(ElasticsearchHelper.RESULT_TYPE_FACET)) { elasticsearchResponseDto.setResultTypeIndexSearchResponseDtos(elasticsearchHelper.getResultTypeIndexSearchResponseDto(searchResult)); } return elasticsearchHelper.getFacetsResponse(elasticsearchResponseDto, bdefActiveIndex, tagActiveIndex); }
Aggregations searchResponseAggregations = getAggregationsFromSearchResponse(searchResponse); Aggregations nestedAggregationAggregations = getAggregationsFromNestedAggregation(nestedAggregation, searchResponse);
.filter(elasticsearchHelper.addIndexSearchFilterBooleanClause(indexSearchRequest.getIndexSearchFilters(), bdefActiveIndex, tagActiveIndex)); searchRequestBuilder = elasticsearchHelper.addFacetFieldAggregations(new HashSet<>(indexSearchRequest.getFacetFields()), searchRequestBuilder);