Search in sources :

Example 11 with Aggregations

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

the class ElasticSearchHelperTest method testGetAggregationAggregationIsNull.

@Test
public void testGetAggregationAggregationIsNull() {
    // Create mock aggregations.
    Aggregations aggregations = mock(Aggregations.class);
    when(aggregations.get(AGGREGATION_NAME)).thenReturn(null);
    // Create a mock search response.
    SearchResponse searchResponse = mock(SearchResponse.class);
    when(searchResponse.getAggregations()).thenReturn(aggregations);
    // Mock the external calls.
    when(jsonHelper.objectToJson(searchResponse)).thenReturn(SEARCH_RESPONSE_JSON_STRING);
    // Try to call the method under test.
    try {
        elasticsearchHelper.getAggregation(searchResponse, 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 12 with Aggregations

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

the class ElasticSearchHelperTest method testGetResultTypeIndexSearchResponseDto.

@Test
public void testGetResultTypeIndexSearchResponseDto() {
    SearchResponse searchResponse = mock(SearchResponse.class);
    Terms terms = mock(Terms.class);
    Aggregations aggregations = mock(Aggregations.class);
    Terms.Bucket bucket = mock(Terms.Bucket.class);
    List<Terms.Bucket> buckets = Collections.singletonList(bucket);
    when(searchResponse.getAggregations()).thenReturn(aggregations);
    when(aggregations.get(RESULT_TYPE_AGGS)).thenReturn(terms);
    when(terms.getBuckets()).thenReturn(buckets);
    when(bucket.getKeyAsString()).thenReturn(TAG_CODE);
    when(bucket.getDocCount()).thenReturn(TAG_COUNT);
    List<ResultTypeIndexSearchResponseDto> expectedList = new ArrayList<>();
    expectedList.add(new ResultTypeIndexSearchResponseDto(TAG_CODE, TAG_COUNT, TAG_CODE));
    List<ResultTypeIndexSearchResponseDto> resultList = elasticsearchHelper.getResultTypeIndexSearchResponseDto(searchResponse);
    assertEquals(expectedList, resultList);
}
Also used : Aggregations(org.elasticsearch.search.aggregations.Aggregations) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) StringTerms(org.elasticsearch.search.aggregations.bucket.terms.StringTerms) ArrayList(java.util.ArrayList) ResultTypeIndexSearchResponseDto(org.finra.herd.model.dto.ResultTypeIndexSearchResponseDto) SearchResponse(org.elasticsearch.action.search.SearchResponse) Test(org.junit.Test) AbstractDaoTest(org.finra.herd.dao.AbstractDaoTest)

Example 13 with Aggregations

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

the class ElasticSearchHelperTest method testGetNestedAggregationSubAggregationIsNull.

@Test
public void testGetNestedAggregationSubAggregationIsNull() {
    // Create mock nested aggregations.
    Aggregations nestedAggregations = mock(Aggregations.class);
    when(nestedAggregations.get(SUB_AGGREGATION_NAME)).thenReturn(null);
    // 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);
    // 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.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);
    verify(jsonHelper).objectToJson(nestedAggregation);
    verifyNoMoreInteractionsHelper();
}
Also used : Aggregations(org.elasticsearch.search.aggregations.Aggregations) Nested(org.elasticsearch.search.aggregations.bucket.nested.Nested) SearchResponse(org.elasticsearch.action.search.SearchResponse) Test(org.junit.Test) AbstractDaoTest(org.finra.herd.dao.AbstractDaoTest)

Example 14 with Aggregations

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

the class ElasticsearchHelper method getNestedAggregation.

/**
 * Returns the sub-aggregation that is associated with the specified nested aggregation. This method also validates that the retrieved sub-aggregation
 * exists.
 *
 * @param searchResponse the response of the search request
 * @param nestedAggregationName the name of the nested aggregation
 * @param subAggregationName the name of the sub-aggregation
 *
 * @return the aggregation
 */
public Terms getNestedAggregation(SearchResponse searchResponse, String nestedAggregationName, String subAggregationName) {
    // Retrieve the aggregations from the search response.
    Aggregations searchResponseAggregations = getAggregationsFromSearchResponse(searchResponse);
    // Retrieve the nested aggregation.
    Nested nestedAggregation = searchResponseAggregations.get(nestedAggregationName);
    // Fail if the retrieved nested aggregation is null.
    if (nestedAggregation == null) {
        // Log the error along with the search response contents.
        LOGGER.error("Failed to retrieve \"{}\" nested aggregation from the search response. searchResponse={}", nestedAggregationName, jsonHelper.objectToJson(searchResponse));
        // Throw an exception.
        throw new IllegalStateException("Invalid search result.");
    }
    // Retrieve the aggregations from the nested aggregation.
    Aggregations nestedAggregationAggregations = getAggregationsFromNestedAggregation(nestedAggregation, searchResponse);
    // Retrieve the sub-aggregation.
    Terms subAggregation = nestedAggregationAggregations.get(subAggregationName);
    // Fail if retrieved sub-aggregation is null.
    if (subAggregation == null) {
        // Log the error along with the search response contents.
        LOGGER.error("Failed to retrieve \"{}\" sub-aggregation from \"{}\" nested aggregation. searchResponse={} nestedAggregation={}", subAggregationName, nestedAggregationName, jsonHelper.objectToJson(searchResponse), jsonHelper.objectToJson(nestedAggregation));
        // Throw an exception.
        throw new IllegalStateException("Invalid search result.");
    }
    return subAggregation;
}
Also used : Aggregations(org.elasticsearch.search.aggregations.Aggregations) Nested(org.elasticsearch.search.aggregations.bucket.nested.Nested) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms)

Example 15 with Aggregations

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.aggregations.Aggregations in project core-ng-project by neowu.

the class ElasticSearchTypeImpl method searchResponse.

private SearchResponse<T> searchResponse(org.elasticsearch.action.search.SearchResponse response) {
    SearchHit[] hits = response.getHits().getHits();
    List<T> items = new ArrayList<>(hits.length);
    for (SearchHit hit : hits) {
        items.add(reader.fromJSON(BytesReference.toBytes(hit.getSourceRef())));
    }
    Aggregations aggregationResponse = response.getAggregations();
    Map<String, Aggregation> aggregations = aggregationResponse == null ? Maps.newHashMap() : aggregationResponse.asMap();
    return new SearchResponse<>(items, response.getHits().getTotalHits(), aggregations);
}
Also used : Aggregation(org.elasticsearch.search.aggregations.Aggregation) SearchHit(org.elasticsearch.search.SearchHit) Aggregations(org.elasticsearch.search.aggregations.Aggregations) ArrayList(java.util.ArrayList) SearchResponse(core.framework.search.SearchResponse)

Aggregations

Aggregations (org.elasticsearch.search.aggregations.Aggregations)26 SearchResponse (org.elasticsearch.action.search.SearchResponse)19 Test (org.junit.Test)14 ArrayList (java.util.ArrayList)11 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)11 AbstractDaoTest (org.finra.herd.dao.AbstractDaoTest)8 Map (java.util.Map)7 StringTerms (org.elasticsearch.search.aggregations.bucket.terms.StringTerms)7 Aggregations (org.graylog.shaded.elasticsearch7.org.elasticsearch.search.aggregations.Aggregations)7 SearchType (org.graylog.plugins.views.search.SearchType)6 HashMap (java.util.HashMap)5 Nested (org.elasticsearch.search.aggregations.bucket.nested.Nested)5 Date (java.util.Date)4 List (java.util.List)4 AggregationBuilder (org.elasticsearch.search.aggregations.AggregationBuilder)4 PivotResult (org.graylog.plugins.views.search.searchtypes.pivot.PivotResult)4 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