Search in sources :

Example 11 with Aggregations

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

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;
}
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 13 with Aggregations

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

Example 14 with 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);
}
Also used : AggregationBuilder(org.elasticsearch.search.aggregations.AggregationBuilder) Aggregations(org.elasticsearch.search.aggregations.Aggregations) QueryBuilder(org.elasticsearch.index.query.QueryBuilder)

Example 15 with Aggregations

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);
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) Aggregations(org.elasticsearch.search.aggregations.Aggregations) ArrayList(java.util.ArrayList) BigDecimal(java.math.BigDecimal)

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