Search in sources :

Example 21 with Aggregations

use of org.elasticsearch.search.aggregations.Aggregations in project herd by FINRAOS.

the class ElasticSearchHelperTest method testGetAggregation.

@Test
public void testGetAggregation() {
    // Create a mock aggregation.
    Terms aggregation = mock(Terms.class);
    // Create mock aggregations.
    Aggregations aggregations = mock(Aggregations.class);
    when(aggregations.get(AGGREGATION_NAME)).thenReturn(aggregation);
    // Create a mock search response.
    SearchResponse searchResponse = mock(SearchResponse.class);
    when(searchResponse.getAggregations()).thenReturn(aggregations);
    // Call the method under test.
    Terms result = elasticsearchHelper.getAggregation(searchResponse, AGGREGATION_NAME);
    // Verify the external calls.
    verifyNoMoreInteractionsHelper();
    // Validate the result.
    assertEquals(aggregation, result);
}
Also used : Aggregations(org.elasticsearch.search.aggregations.Aggregations) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) StringTerms(org.elasticsearch.search.aggregations.bucket.terms.StringTerms) SearchResponse(org.elasticsearch.action.search.SearchResponse) Test(org.junit.Test) AbstractDaoTest(org.finra.herd.dao.AbstractDaoTest)

Example 22 with Aggregations

use of org.elasticsearch.search.aggregations.Aggregations in project herd by FINRAOS.

the class ElasticSearchHelperTest method testGetNestedTagTagIndexSearchResponseDtoSearchResponseParameter.

@Test
public void testGetNestedTagTagIndexSearchResponseDtoSearchResponseParameter() {
    SearchResponse searchResponse = mock(SearchResponse.class);
    Aggregations aggregations = mock(Aggregations.class);
    when(searchResponse.getAggregations()).thenReturn(aggregations);
    Nested nestedAggregation = mock(Nested.class);
    when(aggregations.get(TAG_FACET_AGGS)).thenReturn(nestedAggregation);
    Aggregations aggregationAggregations = mock(Aggregations.class);
    when(nestedAggregation.getAggregations()).thenReturn(aggregationAggregations);
    Terms subAggregation = mock(Terms.class);
    when(aggregationAggregations.get(TAGTYPE_CODE_AGGREGATION)).thenReturn(subAggregation);
    List<TagTypeIndexSearchResponseDto> result = elasticsearchHelper.getNestedTagTagIndexSearchResponseDto(searchResponse);
    assertThat("Result is null.", result, is(notNullValue()));
}
Also used : Aggregations(org.elasticsearch.search.aggregations.Aggregations) Nested(org.elasticsearch.search.aggregations.bucket.nested.Nested) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) StringTerms(org.elasticsearch.search.aggregations.bucket.terms.StringTerms) TagTypeIndexSearchResponseDto(org.finra.herd.model.dto.TagTypeIndexSearchResponseDto) SearchResponse(org.elasticsearch.action.search.SearchResponse) Test(org.junit.Test) AbstractDaoTest(org.finra.herd.dao.AbstractDaoTest)

Example 23 with Aggregations

use of org.elasticsearch.search.aggregations.Aggregations in project herd by FINRAOS.

the class ElasticSearchHelperTest method testGetTagTagIndexSearchResponseDto.

@Test
public void testGetTagTagIndexSearchResponseDto() {
    SearchResponse searchResponse = mock(SearchResponse.class);
    Terms terms = mock(Terms.class);
    Aggregations aggregations = mock(Aggregations.class);
    Terms.Bucket tagTypeCodeEntry = mock(Terms.Bucket.class);
    List<Terms.Bucket> tagTypeCodeEntryList = Collections.singletonList(tagTypeCodeEntry);
    when(searchResponse.getAggregations()).thenReturn(aggregations);
    when(aggregations.get(TAG_TYPE_FACET_AGGS)).thenReturn(terms);
    when(terms.getBuckets()).thenReturn(tagTypeCodeEntryList);
    when(tagTypeCodeEntry.getKeyAsString()).thenReturn(TAG_TYPE_CODE);
    when(tagTypeCodeEntry.getAggregations()).thenReturn(aggregations);
    Terms tagTypeDisplayNameAggs = mock(Terms.class);
    Terms.Bucket tagTypeDisplayNameEntry = mock(Terms.Bucket.class);
    List<Terms.Bucket> tagTypeDisplayNameEntryList = Collections.singletonList(tagTypeDisplayNameEntry);
    when(aggregations.get(TAGTYPE_NAME_AGGREGATION)).thenReturn(tagTypeDisplayNameAggs);
    when(tagTypeDisplayNameEntry.getAggregations()).thenReturn(aggregations);
    when(tagTypeDisplayNameAggs.getBuckets()).thenReturn(tagTypeDisplayNameEntryList);
    when(tagTypeDisplayNameEntry.getKeyAsString()).thenReturn(TAG_TYPE_DISPLAY_NAME);
    StringTerms tagCodeAggs = mock(StringTerms.class);
    StringTerms.Bucket tagCodeEntry = mock(StringTerms.Bucket.class);
    List<Terms.Bucket> tagCodeEntryList = Collections.singletonList(tagCodeEntry);
    when(aggregations.get(TAG_CODE_AGGREGATION)).thenReturn(tagCodeAggs);
    when(tagCodeAggs.getBuckets()).thenReturn(tagCodeEntryList);
    when(tagCodeEntry.getAggregations()).thenReturn(aggregations);
    when(tagCodeEntry.getKeyAsString()).thenReturn(TAG_CODE);
    when(tagCodeEntry.getDocCount()).thenReturn((long) TAG_CODE_COUNT);
    Terms tagNameAggs = mock(Terms.class);
    Terms.Bucket tagNameEntry = mock(Terms.Bucket.class);
    List<Terms.Bucket> tagNameEntryList = Collections.singletonList(tagNameEntry);
    when(tagNameEntry.getAggregations()).thenReturn(aggregations);
    when(aggregations.get(TAG_NAME_AGGREGATION)).thenReturn(tagNameAggs);
    when(tagNameAggs.getBuckets()).thenReturn(tagNameEntryList);
    when(tagNameEntry.getKeyAsString()).thenReturn(TAG_DISPLAY_NAME);
    List<TagTypeIndexSearchResponseDto> resultList = elasticsearchHelper.getTagTagIndexSearchResponseDto(searchResponse);
    List<TagTypeIndexSearchResponseDto> expectedList = new ArrayList<>();
    List<TagIndexSearchResponseDto> expectedTagList = new ArrayList<>();
    expectedTagList.add(new TagIndexSearchResponseDto(TAG_CODE, TAG_CODE_COUNT, TAG_DISPLAY_NAME));
    expectedList.add(new TagTypeIndexSearchResponseDto(TAG_TYPE_CODE, expectedTagList, TAG_TYPE_DISPLAY_NAME));
    assertEquals(expectedList, resultList);
}
Also used : Aggregations(org.elasticsearch.search.aggregations.Aggregations) TagIndexSearchResponseDto(org.finra.herd.model.dto.TagIndexSearchResponseDto) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) StringTerms(org.elasticsearch.search.aggregations.bucket.terms.StringTerms) ArrayList(java.util.ArrayList) SearchResponse(org.elasticsearch.action.search.SearchResponse) StringTerms(org.elasticsearch.search.aggregations.bucket.terms.StringTerms) TagTypeIndexSearchResponseDto(org.finra.herd.model.dto.TagTypeIndexSearchResponseDto) Test(org.junit.Test) AbstractDaoTest(org.finra.herd.dao.AbstractDaoTest)

Example 24 with Aggregations

use of org.elasticsearch.search.aggregations.Aggregations in project herd by FINRAOS.

the class ElasticSearchHelperTest method testGetNestedAggregationNestedAggregationIsNull.

@Test
public void testGetNestedAggregationNestedAggregationIsNull() {
    // Create mock search response aggregations.
    Aggregations searchResponseAggregations = mock(Aggregations.class);
    when(searchResponseAggregations.get(NESTED_AGGREGATION_NAME)).thenReturn(null);
    // Create a mock search response.
    SearchResponse searchResponse = mock(SearchResponse.class);
    when(searchResponse.getAggregations()).thenReturn(searchResponseAggregations);
    // Mock the external calls.
    when(jsonHelper.objectToJson(searchResponse)).thenReturn(SEARCH_RESPONSE_JSON_STRING);
    // Try to call the method under test.
    try {
        elasticsearchHelper.getNestedAggregation(searchResponse, NESTED_AGGREGATION_NAME, SUB_AGGREGATION_NAME);
        fail();
    } catch (IllegalStateException e) {
        assertEquals("Invalid search result.", e.getMessage());
    }
    // Verify the external calls.
    verify(jsonHelper).objectToJson(searchResponse);
    verifyNoMoreInteractionsHelper();
}
Also used : Aggregations(org.elasticsearch.search.aggregations.Aggregations) SearchResponse(org.elasticsearch.action.search.SearchResponse) Test(org.junit.Test) AbstractDaoTest(org.finra.herd.dao.AbstractDaoTest)

Example 25 with Aggregations

use of org.elasticsearch.search.aggregations.Aggregations in project herd by FINRAOS.

the class ElasticSearchHelperTest method testGetNestedAggregation.

@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);
}
Also used : Aggregations(org.elasticsearch.search.aggregations.Aggregations) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) StringTerms(org.elasticsearch.search.aggregations.bucket.terms.StringTerms) Nested(org.elasticsearch.search.aggregations.bucket.nested.Nested) SearchResponse(org.elasticsearch.action.search.SearchResponse) Test(org.junit.Test) AbstractDaoTest(org.finra.herd.dao.AbstractDaoTest)

Aggregations

Aggregations (org.elasticsearch.search.aggregations.Aggregations)26 SearchResponse (org.elasticsearch.action.search.SearchResponse)19 Test (org.junit.Test)14 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)11 ArrayList (java.util.ArrayList)10 AbstractDaoTest (org.finra.herd.dao.AbstractDaoTest)8 StringTerms (org.elasticsearch.search.aggregations.bucket.terms.StringTerms)7 Aggregations (org.graylog.shaded.elasticsearch7.org.elasticsearch.search.aggregations.Aggregations)7 Map (java.util.Map)6 HashMap (java.util.HashMap)5 Nested (org.elasticsearch.search.aggregations.bucket.nested.Nested)5 SearchType (org.graylog.plugins.views.search.SearchType)5 Date (java.util.Date)4 AggregationBuilder (org.elasticsearch.search.aggregations.AggregationBuilder)4 List (java.util.List)3 SearchRequest (org.elasticsearch.action.search.SearchRequest)3 Aggregation (org.elasticsearch.search.aggregations.Aggregation)3 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)3 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)3 PivotResult (org.graylog.plugins.views.search.searchtypes.pivot.PivotResult)3