class ElasticsearchHelper {

    public List<Facet> getFacetsResponse(ElasticsearchResponseDto elasticsearchResponseDto, final String bdefActiveIndex, final String tagActiveIndex)
    {
        List<Facet> facets = new ArrayList<>();

        List<Facet> tagTypeFacets;
        if (elasticsearchResponseDto.getNestTagTypeIndexSearchResponseDtos() != null)
        {
            tagTypeFacets = new ArrayList<>();
            //construct a list of facet information
            for (TagTypeIndexSearchResponseDto tagTypeIndexSearchResponseDto : elasticsearchResponseDto.getNestTagTypeIndexSearchResponseDtos())
            {
                tagTypeFacets.add(createTagTypeFacet(tagTypeIndexSearchResponseDto));
            }

            facets.addAll(tagTypeFacets);
        }

        if (elasticsearchResponseDto.getTagTypeIndexSearchResponseDtos() != null)
        {
            for (TagTypeIndexSearchResponseDto tagTypeIndexDto : elasticsearchResponseDto.getTagTypeIndexSearchResponseDtos())
            {
                boolean foundMatchingTagType = false;
                for (Facet tagFacet : facets)
                {
                    if (tagFacet.getFacetId().equals(tagTypeIndexDto.getCode()))
                    {
                        foundMatchingTagType = true;
                        boolean foundMatchingTagCode = false;
                        for (TagIndexSearchResponseDto tagIndexDto : tagTypeIndexDto.getTagIndexSearchResponseDtos())
                        {
                            for (Facet nestedTagIndexDto : tagFacet.getFacets())
                            {
                                if (tagIndexDto.getTagCode().equals(nestedTagIndexDto.getFacetId()))
                                {
                                    foundMatchingTagCode = true;
                                }
                            }
                            if (!foundMatchingTagCode)
                            {
                                tagFacet.getFacets().add(
                                    new Facet(tagIndexDto.getTagDisplayName(), tagIndexDto.getCount(), FacetTypeEnum.TAG.value(), tagIndexDto.getTagCode(),
                                        null));
                            }
                        }
                    }
                }
                if (!foundMatchingTagType)
                {
                    facets.add(createTagTypeFacet(tagTypeIndexDto));
                }
            }
        }

        if (elasticsearchResponseDto.getResultTypeIndexSearchResponseDtos() != null)
        {
            List<Facet> resultTypeFacets = new ArrayList<>();
            //construct a list of facet information
            for (ResultTypeIndexSearchResponseDto resultTypeIndexSearchResponseDto : elasticsearchResponseDto.getResultTypeIndexSearchResponseDtos())
            {
                String facetId = getSearchIndexType(resultTypeIndexSearchResponseDto.getResultTypeDisplayName(), bdefActiveIndex, tagActiveIndex);
                Facet resultTypeFacet = new Facet(facetId, resultTypeIndexSearchResponseDto.getCount(), FacetTypeEnum.RESULT_TYPE.value(), facetId, null);
                resultTypeFacets.add(resultTypeFacet);
            }
            facets.addAll(resultTypeFacets);
        }

        return facets;
    }

}