Search in sources :

Example 11 with ElasticsearchResponseDto

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);
}
Also used : TagIndexSearchResponseDto(org.finra.herd.model.dto.TagIndexSearchResponseDto) TagTypeIndexSearchResponseDto(org.finra.herd.model.dto.TagTypeIndexSearchResponseDto) ArrayList(java.util.ArrayList) ElasticsearchResponseDto(org.finra.herd.model.dto.ElasticsearchResponseDto) Facet(org.finra.herd.model.api.xml.Facet) Test(org.junit.Test) AbstractDaoTest(org.finra.herd.dao.AbstractDaoTest)

Example 12 with ElasticsearchResponseDto

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);
}
Also used : Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) SearchResult(io.searchbox.core.SearchResult) Namespace(org.finra.herd.model.dto.Namespace) DataProvider(org.finra.herd.model.dto.DataProvider) Search(io.searchbox.core.Search) BusinessObjectDefinitionIndexSearchResponseDto(org.finra.herd.model.dto.BusinessObjectDefinitionIndexSearchResponseDto) TagTypeIndexSearchResponseDto(org.finra.herd.model.dto.TagTypeIndexSearchResponseDto) ElasticsearchResponseDto(org.finra.herd.model.dto.ElasticsearchResponseDto) JestResult(io.searchbox.client.JestResult) Test(org.junit.Test)

Example 13 with ElasticsearchResponseDto

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);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) TagTypeEntity(org.finra.herd.model.jpa.TagTypeEntity) JsonObject(com.google.gson.JsonObject) DataProvider(org.finra.herd.model.dto.DataProvider) TagEntity(org.finra.herd.model.jpa.TagEntity) Search(io.searchbox.core.Search) TagTypeIndexSearchResponseDto(org.finra.herd.model.dto.TagTypeIndexSearchResponseDto) ArrayList(java.util.ArrayList) List(java.util.List) JestResult(io.searchbox.client.JestResult) SearchFilterType(org.finra.herd.model.dto.SearchFilterType) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) SearchResult(io.searchbox.core.SearchResult) Namespace(org.finra.herd.model.dto.Namespace) BusinessObjectDefinitionIndexSearchResponseDto(org.finra.herd.model.dto.BusinessObjectDefinitionIndexSearchResponseDto) ElasticsearchResponseDto(org.finra.herd.model.dto.ElasticsearchResponseDto) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 14 with ElasticsearchResponseDto

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;
}
Also used : SearchFilterType(org.finra.herd.model.dto.SearchFilterType) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) ArrayList(java.util.ArrayList) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) WildcardQueryBuilder(org.elasticsearch.index.query.WildcardQueryBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) ConstantScoreQueryBuilder(org.elasticsearch.index.query.ConstantScoreQueryBuilder) ElasticsearchClientImpl(org.finra.herd.dao.helper.ElasticsearchClientImpl) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) WildcardQueryBuilder(org.elasticsearch.index.query.WildcardQueryBuilder) ConstantScoreQueryBuilder(org.elasticsearch.index.query.ConstantScoreQueryBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) TagEntity(org.finra.herd.model.jpa.TagEntity) ArrayList(java.util.ArrayList) List(java.util.List) ElasticsearchResponseDto(org.finra.herd.model.dto.ElasticsearchResponseDto) TimeValue(org.elasticsearch.common.unit.TimeValue)

Example 15 with 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);
}
Also used : ElasticsearchResponseDto(org.finra.herd.model.dto.ElasticsearchResponseDto)

Aggregations

ElasticsearchResponseDto (org.finra.herd.model.dto.ElasticsearchResponseDto)20 ArrayList (java.util.ArrayList)17 Test (org.junit.Test)15 TagEntity (org.finra.herd.model.jpa.TagEntity)11 BusinessObjectDefinitionIndexSearchResponseDto (org.finra.herd.model.dto.BusinessObjectDefinitionIndexSearchResponseDto)10 BusinessObjectDefinitionIndexSearchResponse (org.finra.herd.model.api.xml.BusinessObjectDefinitionIndexSearchResponse)9 BusinessObjectDefinitionSearchFilter (org.finra.herd.model.api.xml.BusinessObjectDefinitionSearchFilter)9 DataProvider (org.finra.herd.model.dto.DataProvider)9 Namespace (org.finra.herd.model.dto.Namespace)9 TagTypeIndexSearchResponseDto (org.finra.herd.model.dto.TagTypeIndexSearchResponseDto)9 BusinessObjectDefinitionIndexSearchRequest (org.finra.herd.model.api.xml.BusinessObjectDefinitionIndexSearchRequest)8 BusinessObjectDefinitionSearchKey (org.finra.herd.model.api.xml.BusinessObjectDefinitionSearchKey)8 TagKey (org.finra.herd.model.api.xml.TagKey)7 Facet (org.finra.herd.model.api.xml.Facet)6 TagIndexSearchResponseDto (org.finra.herd.model.dto.TagIndexSearchResponseDto)6 AbstractDaoTest (org.finra.herd.dao.AbstractDaoTest)5 List (java.util.List)4 SearchFilterType (org.finra.herd.model.dto.SearchFilterType)4 JsonObject (com.google.gson.JsonObject)3 JestResult (io.searchbox.client.JestResult)3