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();
}
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);
}
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();
}
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;
}
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);
}
Aggregations