use of org.finra.herd.model.dto.ElasticsearchResponseDto in project herd by FINRAOS.
the class ElasticSearchHelperTest method testGetFacetsResponseIncludingTag.
@Test
public void testGetFacetsResponseIncludingTag() {
ElasticsearchResponseDto elasticsearchResponseDto = new ElasticsearchResponseDto();
List<TagTypeIndexSearchResponseDto> nestTagTypeIndexSearchResponseDtos = new ArrayList<>();
TagTypeIndexSearchResponseDto tagType1 = new TagTypeIndexSearchResponseDto(TAG_TYPE_CODE, null, TAG_TYPE_DISPLAY_NAME);
TagTypeIndexSearchResponseDto tagType2 = new TagTypeIndexSearchResponseDto(TAG_TYPE_CODE_2, Collections.singletonList(new TagIndexSearchResponseDto(TAG_CODE, 1, TAG_CODE_DISPLAY_NAME)), TAG_TYPE_DISPLAY_NAME_2);
nestTagTypeIndexSearchResponseDtos.add(tagType1);
nestTagTypeIndexSearchResponseDtos.add(tagType2);
elasticsearchResponseDto.setNestTagTypeIndexSearchResponseDtos(nestTagTypeIndexSearchResponseDtos);
TagTypeIndexSearchResponseDto tagType3 = new TagTypeIndexSearchResponseDto(TAG_TYPE_CODE_2, Collections.singletonList(new TagIndexSearchResponseDto(TAG_CODE, 1, TAG_CODE_DISPLAY_NAME)), TAG_TYPE_DISPLAY_NAME_2);
List<TagTypeIndexSearchResponseDto> tagTypeIndexSearchResponseDtos = new ArrayList<>();
tagTypeIndexSearchResponseDtos.add(tagType3);
elasticsearchResponseDto.setTagTypeIndexSearchResponseDtos(tagTypeIndexSearchResponseDtos);
List<Facet> facets = elasticsearchHelper.getFacetsResponse(elasticsearchResponseDto, BUSINESS_OBJECT_DEFINITION_SEARCH_INDEX_NAME, TAG_SEARCH_INDEX_NAME);
List<Facet> expectedFacets = new ArrayList<>();
expectedFacets.add(new Facet(TAG_TYPE_DISPLAY_NAME, null, FacetTypeEnum.TAG_TYPE.value(), TAG_TYPE_CODE, new ArrayList<>()));
List<Facet> tagFacet = new ArrayList<>();
tagFacet.add(new Facet(TAG_CODE_DISPLAY_NAME, 1L, FacetTypeEnum.TAG.value(), TAG_CODE, null));
expectedFacets.add(new Facet(TAG_TYPE_DISPLAY_NAME_2, null, FacetTypeEnum.TAG_TYPE.value(), TAG_TYPE_CODE_2, tagFacet));
assertEquals(expectedFacets, facets);
}
use of org.finra.herd.model.dto.ElasticsearchResponseDto in project herd by FINRAOS.
the class BusinessObjectDefinitionIndexSearchDaoTest method testFindAllBusinessObjectDefinitionsFunction.
@Test
public void testFindAllBusinessObjectDefinitionsFunction() throws Exception {
SearchResult searchResult = mock(SearchResult.class);
JestResult jestResult = mock(JestResult.class);
Terms.Bucket tagTypeCodeBucket = mock(Terms.Bucket.class);
List<Terms.Bucket> tagTypeCodeBucketList = new ArrayList<>();
tagTypeCodeBucketList.add(tagTypeCodeBucket);
// Mock the call to external methods
when(jestClientHelper.searchExecute(any(Search.class))).thenReturn(searchResult);
List<TagTypeIndexSearchResponseDto> tagTypeIndexSearchResponseDtoList = new ArrayList<>();
when(elasticsearchHelper.getNestedTagTagIndexSearchResponseDto(any(SearchResult.class))).thenReturn(tagTypeIndexSearchResponseDtoList);
List<BusinessObjectDefinitionIndexSearchResponseDto> businessObjectDefinitionIndexSearchResponseDtoList = Arrays.asList(new BusinessObjectDefinitionIndexSearchResponseDto(new DataProvider("data provider"), "description 1", "display name", "bdefname", new Namespace("namespace")));
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("_scroll_id", "100");
when(searchResult.getSourceAsObjectList(BusinessObjectDefinitionIndexSearchResponseDto.class)).thenReturn(businessObjectDefinitionIndexSearchResponseDtoList);
when(jestClientHelper.searchScrollExecute(any())).thenReturn(jestResult);
when(searchResult.getJsonObject()).thenReturn(jsonObject);
// Call the method under test.
ElasticsearchResponseDto result = businessObjectDefinitionIndexSearchDao.findAllBusinessObjectDefinitions("INDEX_NAME", "DOCUMENT_TYPE", new HashSet<>());
// Verify the external calls.
verify(jestClientHelper).searchExecute(any());
// Validate the results.
assertEquals(new ElasticsearchResponseDto(businessObjectDefinitionIndexSearchResponseDtoList, null, null, null), result);
}
use of org.finra.herd.model.dto.ElasticsearchResponseDto in project herd by FINRAOS.
the class BusinessObjectDefinitionIndexSearchDaoTest method testSearchBusinessObjectDefinitionByTagsFunctionExclusionAndTag.
@Test
public void testSearchBusinessObjectDefinitionByTagsFunctionExclusionAndTag() throws Exception {
SearchResult searchResult = mock(SearchResult.class);
JestResult jestResult = mock(JestResult.class);
Terms.Bucket tagTypeCodeBucket = mock(Terms.Bucket.class);
List<Terms.Bucket> tagTypeCodeBucketList = new ArrayList<>();
tagTypeCodeBucketList.add(tagTypeCodeBucket);
// Mock the call to external methods
when(jestClientHelper.searchExecute(any(Search.class))).thenReturn(searchResult);
List<TagTypeIndexSearchResponseDto> tagTypeIndexSearchResponseDtoList = new ArrayList<>();
when(elasticsearchHelper.getNestedTagTagIndexSearchResponseDto(any(SearchResult.class))).thenReturn(tagTypeIndexSearchResponseDtoList);
List<BusinessObjectDefinitionIndexSearchResponseDto> businessObjectDefinitionIndexSearchResponseDtoList = Arrays.asList(new BusinessObjectDefinitionIndexSearchResponseDto(new DataProvider("data provider"), "description 1", "display name", "bdefname", new Namespace("namespace")));
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("_scroll_id", "100");
when(searchResult.getSourceAsObjectList(BusinessObjectDefinitionIndexSearchResponseDto.class)).thenReturn(businessObjectDefinitionIndexSearchResponseDtoList);
when(jestClientHelper.searchScrollExecute(any())).thenReturn(jestResult);
when(searchResult.getJsonObject()).thenReturn(jsonObject);
// Get test tag entity
TagEntity tagEntity = new TagEntity();
tagEntity.setTagCode("TAG_CODE");
TagTypeEntity tagTypeEntity = new TagTypeEntity();
tagTypeEntity.setCode("TAG_TYPE_CODE");
tagTypeEntity.setDisplayName("DISPLAY_NAME");
tagTypeEntity.setOrderNumber(1);
tagEntity.setTagType(tagTypeEntity);
List<TagEntity> tagEntities = new ArrayList<>();
tagEntities.add(tagEntity);
// List<Map<SearchFilterType, List<TagEntity>>>
Map<SearchFilterType, List<TagEntity>> searchFilterTypeListMap = new HashMap<>();
searchFilterTypeListMap.put(SearchFilterType.EXCLUSION_SEARCH_FILTER, tagEntities);
List<Map<SearchFilterType, List<TagEntity>>> tagEnLists = Collections.singletonList(searchFilterTypeListMap);
// Mock the external calls.
when(elasticsearchHelper.getNestedTagTagIndexSearchResponseDto(searchResult)).thenReturn(tagTypeIndexSearchResponseDtoList);
// Call the method under test.
ElasticsearchResponseDto result = businessObjectDefinitionIndexSearchDao.searchBusinessObjectDefinitionsByTags("INDEX_NAME", "DOCUMENT_TYPE", tagEnLists, new HashSet<>(Arrays.asList("tag")));
// Verify the external calls.
verify(jestClientHelper, times(2)).searchExecute(any());
verify(jestClientHelper).searchScrollExecute(any());
// Validate the results.
assertEquals(new ElasticsearchResponseDto(businessObjectDefinitionIndexSearchResponseDtoList, null, null, null), result);
}
use of org.finra.herd.model.dto.ElasticsearchResponseDto in project herd by FINRAOS.
the class BusinessObjectDefinitionIndexSearchDaoImpl method searchBusinessObjectDefinitionsByTags.
@Override
public ElasticsearchResponseDto searchBusinessObjectDefinitionsByTags(String indexName, String documentType, List<Map<SearchFilterType, List<TagEntity>>> nestedTagEntityMaps, Set<String> facetFieldsList) {
ElasticsearchResponseDto elasticsearchResponseDto = new ElasticsearchResponseDto();
List<List<TagEntity>> nestedInclusionTagEntityLists = new ArrayList<>();
List<List<TagEntity>> nestedExclusionTagEntityLists = new ArrayList<>();
for (Map<SearchFilterType, List<TagEntity>> tagEntityMap : nestedTagEntityMaps) {
if (tagEntityMap.containsKey(SearchFilterType.INCLUSION_SEARCH_FILTER)) {
nestedInclusionTagEntityLists.add(tagEntityMap.get(SearchFilterType.INCLUSION_SEARCH_FILTER));
} else if (tagEntityMap.containsKey(SearchFilterType.EXCLUSION_SEARCH_FILTER)) {
nestedExclusionTagEntityLists.add(tagEntityMap.get(SearchFilterType.EXCLUSION_SEARCH_FILTER));
}
}
LOGGER.info("Searching Elasticsearch business object definition documents from index, indexName={} and documentType={}, by tagEntityList={}", indexName, documentType, tagEntityListToString(flattenTagEntitiesList(nestedInclusionTagEntityLists)));
LOGGER.info("Excluding the following tagEntityList={}", indexName, documentType, tagEntityListToString(flattenTagEntitiesList(nestedExclusionTagEntityLists)));
BoolQueryBuilder compoundSearchFiltersQueryBuilder = new BoolQueryBuilder();
// If there are only exclusion tag entities then, get everything else, but the exclusion tags.
if (CollectionUtils.isEmpty(flattenTagEntitiesList(nestedInclusionTagEntityLists))) {
WildcardQueryBuilder wildcardQueryBuilder = QueryBuilders.wildcardQuery(NAME_FIELD, "*");
compoundSearchFiltersQueryBuilder.must(wildcardQueryBuilder);
}
// Inclusion
for (List<TagEntity> tagEntities : nestedInclusionTagEntityLists) {
BoolQueryBuilder searchFilterQueryBuilder = new BoolQueryBuilder();
for (TagEntity tagEntity : tagEntities) {
// Add constant-score term queries for tagType-code and tag-code from the tag-key.
ConstantScoreQueryBuilder searchKeyQueryBuilder = QueryBuilders.constantScoreQuery(QueryBuilders.boolQuery().must(QueryBuilders.termQuery(TAGTYPE_CODE_FIELD, tagEntity.getTagType().getCode())).must(QueryBuilders.termQuery(TAG_CODE_FIELD, tagEntity.getTagCode())));
// Individual tag-keys are OR-ed
searchFilterQueryBuilder.should(searchKeyQueryBuilder);
}
// Individual search-filters are AND-ed
compoundSearchFiltersQueryBuilder.must(searchFilterQueryBuilder);
}
// Exclusion
for (List<TagEntity> tagEntities : nestedExclusionTagEntityLists) {
for (TagEntity tagEntity : tagEntities) {
// Add constant-score term queries for tagType-code and tag-code from the tag-key.
QueryBuilder searchKeyQueryBuilder = QueryBuilders.boolQuery().must(QueryBuilders.termQuery(TAGTYPE_CODE_FIELD, tagEntity.getTagType().getCode())).must(QueryBuilders.termQuery(TAG_CODE_FIELD, tagEntity.getTagCode()));
// Exclusion: individual tag-keys are added as a must not query
compoundSearchFiltersQueryBuilder.mustNot(searchKeyQueryBuilder);
}
}
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
// Fetch only the required fields
searchSourceBuilder.fetchSource(new String[] { DATA_PROVIDER_NAME_SOURCE, DESCRIPTION_SOURCE, DISPLAY_NAME_SOURCE, NAME_SOURCE, NAMESPACE_CODE_SOURCE }, null);
searchSourceBuilder.query(compoundSearchFiltersQueryBuilder);
// Create a search request and set the scroll time and scroll size
SearchRequestBuilder searchRequestBuilder = new SearchRequestBuilder(new ElasticsearchClientImpl(), SearchAction.INSTANCE);
searchRequestBuilder.setIndices(indexName);
// Construct scroll query
searchRequestBuilder.setTypes(documentType).setScroll(new TimeValue(ELASTIC_SEARCH_SCROLL_KEEP_ALIVE_TIME)).setSize(ELASTIC_SEARCH_SCROLL_PAGE_SIZE).setSource(searchSourceBuilder).addSort(SortBuilders.fieldSort(BUSINESS_OBJECT_DEFINITION_SORT_FIELD).order(SortOrder.ASC)).addSort(SortBuilders.fieldSort(NAMESPACE_CODE_SORT_FIELD).order(SortOrder.ASC));
// Add aggregation builder if facet fields are present
if (CollectionUtils.isNotEmpty(facetFieldsList)) {
addFacetFieldAggregations(facetFieldsList, elasticsearchResponseDto, searchRequestBuilder, indexName);
}
// Log the actual search query
LOGGER.info("bdefIndexSearchQuery={}", searchRequestBuilder.toString());
elasticsearchResponseDto.setBusinessObjectDefinitionIndexSearchResponseDtos(scrollSearchResultsIntoBusinessObjectDefinitionDto(searchRequestBuilder, indexName));
return elasticsearchResponseDto;
}
use of org.finra.herd.model.dto.ElasticsearchResponseDto in project herd by FINRAOS.
the class IndexSearchDaoImpl method extractFacets.
/**
* 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