use of 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.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.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);
}
use of org.elasticsearch.search.aggregations.Aggregations in project molgenis by molgenis.
the class ElasticsearchService method aggregate.
@Override
public AggregateResult aggregate(final EntityType entityType, AggregateQuery aggregateQuery) {
List<AggregationBuilder> aggregationList = contentGenerators.createAggregations(aggregateQuery.getAttributeX(), aggregateQuery.getAttributeY(), aggregateQuery.getAttributeDistinct());
QueryBuilder query = contentGenerators.createQuery(aggregateQuery.getQuery(), entityType);
Index index = contentGenerators.createIndex(entityType);
Aggregations aggregations = clientFacade.aggregate(aggregationList, query, index);
return new AggregateResponseParser().parseAggregateResponse(aggregateQuery.getAttributeX(), aggregateQuery.getAttributeY(), aggregateQuery.getAttributeDistinct(), aggregations, dataService);
}
use of org.elasticsearch.search.aggregations.Aggregations in project play2-elasticsearch by cleverage.
the class IndexQuery method toSearchResults.
private IndexResults<T> toSearchResults(SearchResponse searchResponse) {
// Get Total Records Found
long count = searchResponse.getHits().totalHits();
// Get Aggregations
Aggregations aggregationsResponse = searchResponse.getAggregations();
// Get List results
List<T> results = new ArrayList<T>();
// Loop on each one
for (SearchHit h : searchResponse.getHits()) {
// Get Data Map
Map<String, Object> map = h.sourceAsMap();
// Create a new Indexable Object for the return
T objectIndexable = IndexUtils.getInstanceIndex(clazz);
T t = (T) objectIndexable.fromIndex(map);
t.id = h.getId();
t.searchHit = h;
results.add(t);
}
if (Logger.isDebugEnabled()) {
Logger.debug("ElasticSearch : Results -> " + results.toString());
}
// pagination
long pageSize = 10;
if (size > -1) {
pageSize = size;
}
long pageCurrent = 1;
if (from > 0) {
pageCurrent = ((int) (from / pageSize)) + 1;
}
long pageNb;
if (pageSize == 0) {
pageNb = 1;
} else {
pageNb = (long) Math.ceil(new BigDecimal(count).divide(new BigDecimal(pageSize), 2, RoundingMode.HALF_UP).doubleValue());
}
// Return Results
return new IndexResults<T>(count, pageSize, pageCurrent, pageNb, results, aggregationsResponse);
}
Aggregations