Search in sources :

Example 1 with ProfileShardResult

use of org.opensearch.search.profile.ProfileShardResult in project OpenSearch by opensearch-project.

the class SearchResponseMerger method getMergedResponse.

/**
 * Returns the merged response. To be called once all responses have been added through {@link #add(SearchResponse)}
 * so that all responses are merged into a single one.
 */
SearchResponse getMergedResponse(SearchResponse.Clusters clusters) {
    // we end up calling merge without anything to merge, we just return an empty search response
    if (searchResponses.size() == 0) {
        return SearchResponse.empty(searchTimeProvider::buildTookInMillis, clusters);
    }
    int totalShards = 0;
    int skippedShards = 0;
    int successfulShards = 0;
    // the current reduce phase counts as one
    int numReducePhases = 1;
    List<ShardSearchFailure> failures = new ArrayList<>();
    Map<String, ProfileShardResult> profileResults = new HashMap<>();
    List<InternalAggregations> aggs = new ArrayList<>();
    Map<ShardIdAndClusterAlias, Integer> shards = new TreeMap<>();
    List<TopDocs> topDocsList = new ArrayList<>(searchResponses.size());
    Map<String, List<Suggest.Suggestion>> groupedSuggestions = new HashMap<>();
    Boolean trackTotalHits = null;
    SearchPhaseController.TopDocsStats topDocsStats = new SearchPhaseController.TopDocsStats(trackTotalHitsUpTo);
    for (SearchResponse searchResponse : searchResponses) {
        totalShards += searchResponse.getTotalShards();
        skippedShards += searchResponse.getSkippedShards();
        successfulShards += searchResponse.getSuccessfulShards();
        numReducePhases += searchResponse.getNumReducePhases();
        Collections.addAll(failures, searchResponse.getShardFailures());
        profileResults.putAll(searchResponse.getProfileResults());
        if (searchResponse.getAggregations() != null) {
            InternalAggregations internalAggs = (InternalAggregations) searchResponse.getAggregations();
            aggs.add(internalAggs);
        }
        Suggest suggest = searchResponse.getSuggest();
        if (suggest != null) {
            for (Suggest.Suggestion<? extends Suggest.Suggestion.Entry<? extends Suggest.Suggestion.Entry.Option>> entries : suggest) {
                List<Suggest.Suggestion> suggestionList = groupedSuggestions.computeIfAbsent(entries.getName(), s -> new ArrayList<>());
                suggestionList.add(entries);
            }
            List<CompletionSuggestion> completionSuggestions = suggest.filter(CompletionSuggestion.class);
            for (CompletionSuggestion completionSuggestion : completionSuggestions) {
                for (CompletionSuggestion.Entry options : completionSuggestion) {
                    for (CompletionSuggestion.Entry.Option option : options) {
                        SearchShardTarget shard = option.getHit().getShard();
                        ShardIdAndClusterAlias shardId = new ShardIdAndClusterAlias(shard.getShardId(), shard.getClusterAlias());
                        shards.putIfAbsent(shardId, null);
                    }
                }
            }
        }
        SearchHits searchHits = searchResponse.getHits();
        final TotalHits totalHits;
        if (searchHits.getTotalHits() == null) {
            // in case we didn't track total hits, we get null from each cluster, but we need to set 0 eq to the TopDocs
            totalHits = new TotalHits(0, TotalHits.Relation.EQUAL_TO);
            assert trackTotalHits == null || trackTotalHits == false;
            trackTotalHits = false;
        } else {
            totalHits = searchHits.getTotalHits();
            assert trackTotalHits == null || trackTotalHits;
            trackTotalHits = true;
        }
        TopDocs topDocs = searchHitsToTopDocs(searchHits, totalHits, shards);
        topDocsStats.add(new TopDocsAndMaxScore(topDocs, searchHits.getMaxScore()), searchResponse.isTimedOut(), searchResponse.isTerminatedEarly());
        if (searchHits.getHits().length > 0) {
            // there is no point in adding empty search hits and merging them with the others. Also, empty search hits always come
            // without sort fields and collapse info, despite sort by field and/or field collapsing was requested, which causes
            // issues reconstructing the proper TopDocs instance and breaks mergeTopDocs which expects the same type for each result.
            topDocsList.add(topDocs);
        }
    }
    // after going through all the hits and collecting all their distinct shards, we assign shardIndex and set it to the ScoreDocs
    setTopDocsShardIndex(shards, topDocsList);
    TopDocs topDocs = SearchPhaseController.mergeTopDocs(topDocsList, size, from);
    SearchHits mergedSearchHits = topDocsToSearchHits(topDocs, topDocsStats);
    setSuggestShardIndex(shards, groupedSuggestions);
    Suggest suggest = groupedSuggestions.isEmpty() ? null : new Suggest(Suggest.reduce(groupedSuggestions));
    InternalAggregations reducedAggs = InternalAggregations.topLevelReduce(aggs, aggReduceContextBuilder.forFinalReduction());
    ShardSearchFailure[] shardFailures = failures.toArray(ShardSearchFailure.EMPTY_ARRAY);
    SearchProfileShardResults profileShardResults = profileResults.isEmpty() ? null : new SearchProfileShardResults(profileResults);
    // make failures ordering consistent between ordinary search and CCS by looking at the shard they come from
    Arrays.sort(shardFailures, FAILURES_COMPARATOR);
    InternalSearchResponse response = new InternalSearchResponse(mergedSearchHits, reducedAggs, suggest, profileShardResults, topDocsStats.timedOut, topDocsStats.terminatedEarly, numReducePhases);
    long tookInMillis = searchTimeProvider.buildTookInMillis();
    return new SearchResponse(response, null, totalShards, successfulShards, skippedShards, tookInMillis, shardFailures, clusters, null);
}
Also used : TotalHits(org.apache.lucene.search.TotalHits) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Suggest(org.opensearch.search.suggest.Suggest) TopDocsAndMaxScore(org.opensearch.common.lucene.search.TopDocsAndMaxScore) TopDocs(org.apache.lucene.search.TopDocs) CompletionSuggestion(org.opensearch.search.suggest.completion.CompletionSuggestion) ArrayList(java.util.ArrayList) List(java.util.List) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) SearchHits(org.opensearch.search.SearchHits) ProfileShardResult(org.opensearch.search.profile.ProfileShardResult) CompletionSuggestion(org.opensearch.search.suggest.completion.CompletionSuggestion) TreeMap(java.util.TreeMap) InternalSearchResponse(org.opensearch.search.internal.InternalSearchResponse) InternalAggregations(org.opensearch.search.aggregations.InternalAggregations) SearchProfileShardResults(org.opensearch.search.profile.SearchProfileShardResults) SearchShardTarget(org.opensearch.search.SearchShardTarget) InternalSearchResponse(org.opensearch.search.internal.InternalSearchResponse)

Example 2 with ProfileShardResult

use of org.opensearch.search.profile.ProfileShardResult in project OpenSearch by opensearch-project.

the class AggregationProfilerIT method testComplexProfile.

public void testComplexProfile() {
    SearchResponse response = client().prepareSearch("idx").setProfile(true).addAggregation(histogram("histo").field(NUMBER_FIELD).interval(1L).subAggregation(terms("tags").field(TAG_FIELD).subAggregation(avg("avg").field(NUMBER_FIELD)).subAggregation(max("max").field(NUMBER_FIELD))).subAggregation(terms("strings").field(STRING_FIELD).subAggregation(avg("avg").field(NUMBER_FIELD)).subAggregation(max("max").field(NUMBER_FIELD)).subAggregation(terms("tags").field(TAG_FIELD).subAggregation(avg("avg").field(NUMBER_FIELD)).subAggregation(max("max").field(NUMBER_FIELD))))).get();
    assertSearchResponse(response);
    Map<String, ProfileShardResult> profileResults = response.getProfileResults();
    assertThat(profileResults, notNullValue());
    assertThat(profileResults.size(), equalTo(getNumShards("idx").numPrimaries));
    for (ProfileShardResult profileShardResult : profileResults.values()) {
        assertThat(profileShardResult, notNullValue());
        AggregationProfileShardResult aggProfileResults = profileShardResult.getAggregationProfileResults();
        assertThat(aggProfileResults, notNullValue());
        List<ProfileResult> aggProfileResultsList = aggProfileResults.getProfileResults();
        assertThat(aggProfileResultsList, notNullValue());
        assertThat(aggProfileResultsList.size(), equalTo(1));
        ProfileResult histoAggResult = aggProfileResultsList.get(0);
        assertThat(histoAggResult, notNullValue());
        assertThat(histoAggResult.getQueryName(), equalTo("NumericHistogramAggregator"));
        assertThat(histoAggResult.getLuceneDescription(), equalTo("histo"));
        assertThat(histoAggResult.getTime(), greaterThan(0L));
        Map<String, Long> histoBreakdown = histoAggResult.getTimeBreakdown();
        assertThat(histoBreakdown, notNullValue());
        assertThat(histoBreakdown.keySet(), equalTo(BREAKDOWN_KEYS));
        assertThat(histoBreakdown.get(INITIALIZE), greaterThan(0L));
        assertThat(histoBreakdown.get(BUILD_LEAF_COLLECTOR), greaterThan(0L));
        assertThat(histoBreakdown.get(COLLECT), greaterThan(0L));
        assertThat(histoBreakdown.get(POST_COLLECTION), greaterThan(0L));
        assertThat(histoBreakdown.get(BUILD_AGGREGATION), greaterThan(0L));
        assertThat(histoBreakdown.get(REDUCE), equalTo(0L));
        Map<String, Object> histoDebugInfo = histoAggResult.getDebugInfo();
        assertThat(histoDebugInfo, notNullValue());
        assertThat(histoDebugInfo.keySet(), equalTo(org.opensearch.common.collect.Set.of(TOTAL_BUCKETS)));
        assertThat(((Number) histoDebugInfo.get(TOTAL_BUCKETS)).longValue(), greaterThan(0L));
        assertThat(histoAggResult.getProfiledChildren().size(), equalTo(2));
        Map<String, ProfileResult> histoAggResultSubAggregations = histoAggResult.getProfiledChildren().stream().collect(Collectors.toMap(ProfileResult::getLuceneDescription, s -> s));
        ProfileResult tagsAggResult = histoAggResultSubAggregations.get("tags");
        assertThat(tagsAggResult, notNullValue());
        assertThat(tagsAggResult.getQueryName(), equalTo(GlobalOrdinalsStringTermsAggregator.class.getSimpleName()));
        assertThat(tagsAggResult.getTime(), greaterThan(0L));
        Map<String, Long> tagsBreakdown = tagsAggResult.getTimeBreakdown();
        assertThat(tagsBreakdown, notNullValue());
        assertThat(tagsBreakdown.keySet(), equalTo(BREAKDOWN_KEYS));
        assertThat(tagsBreakdown.get(INITIALIZE), greaterThan(0L));
        assertThat(tagsBreakdown.get(BUILD_LEAF_COLLECTOR), greaterThan(0L));
        assertThat(tagsBreakdown.get(COLLECT), greaterThan(0L));
        assertThat(tagsBreakdown.get(POST_COLLECTION), greaterThan(0L));
        assertThat(tagsBreakdown.get(BUILD_AGGREGATION), greaterThan(0L));
        assertThat(tagsBreakdown.get(REDUCE), equalTo(0L));
        assertRemapTermsDebugInfo(tagsAggResult);
        assertThat(tagsAggResult.getProfiledChildren().size(), equalTo(2));
        Map<String, ProfileResult> tagsAggResultSubAggregations = tagsAggResult.getProfiledChildren().stream().collect(Collectors.toMap(ProfileResult::getLuceneDescription, s -> s));
        ProfileResult avgAggResult = tagsAggResultSubAggregations.get("avg");
        assertThat(avgAggResult, notNullValue());
        assertThat(avgAggResult.getQueryName(), equalTo("AvgAggregator"));
        assertThat(avgAggResult.getTime(), greaterThan(0L));
        Map<String, Long> avgBreakdown = avgAggResult.getTimeBreakdown();
        assertThat(avgBreakdown, notNullValue());
        assertThat(avgBreakdown.keySet(), equalTo(BREAKDOWN_KEYS));
        assertThat(avgBreakdown.get(INITIALIZE), greaterThan(0L));
        assertThat(avgBreakdown.get(BUILD_LEAF_COLLECTOR), greaterThan(0L));
        assertThat(avgBreakdown.get(COLLECT), greaterThan(0L));
        assertThat(avgBreakdown.get(POST_COLLECTION), greaterThan(0L));
        assertThat(avgBreakdown.get(BUILD_AGGREGATION), greaterThan(0L));
        assertThat(avgBreakdown.get(REDUCE), equalTo(0L));
        assertThat(avgAggResult.getDebugInfo(), equalTo(org.opensearch.common.collect.Map.of()));
        assertThat(avgAggResult.getProfiledChildren().size(), equalTo(0));
        ProfileResult maxAggResult = tagsAggResultSubAggregations.get("max");
        assertThat(maxAggResult, notNullValue());
        assertThat(maxAggResult.getQueryName(), equalTo("MaxAggregator"));
        assertThat(maxAggResult.getTime(), greaterThan(0L));
        Map<String, Long> maxBreakdown = maxAggResult.getTimeBreakdown();
        assertThat(maxBreakdown, notNullValue());
        assertThat(maxBreakdown.keySet(), equalTo(BREAKDOWN_KEYS));
        assertThat(maxBreakdown.get(INITIALIZE), greaterThan(0L));
        assertThat(maxBreakdown.get(BUILD_LEAF_COLLECTOR), greaterThan(0L));
        assertThat(maxBreakdown.get(COLLECT), greaterThan(0L));
        assertThat(maxBreakdown.get(POST_COLLECTION), greaterThan(0L));
        assertThat(maxBreakdown.get(BUILD_AGGREGATION), greaterThan(0L));
        assertThat(maxBreakdown.get(REDUCE), equalTo(0L));
        assertThat(maxAggResult.getDebugInfo(), equalTo(org.opensearch.common.collect.Map.of()));
        assertThat(maxAggResult.getProfiledChildren().size(), equalTo(0));
        ProfileResult stringsAggResult = histoAggResultSubAggregations.get("strings");
        assertThat(stringsAggResult, notNullValue());
        assertThat(stringsAggResult.getQueryName(), equalTo(GlobalOrdinalsStringTermsAggregator.class.getSimpleName()));
        assertThat(stringsAggResult.getTime(), greaterThan(0L));
        Map<String, Long> stringsBreakdown = stringsAggResult.getTimeBreakdown();
        assertThat(stringsBreakdown, notNullValue());
        assertThat(stringsBreakdown.keySet(), equalTo(BREAKDOWN_KEYS));
        assertThat(stringsBreakdown.get(INITIALIZE), greaterThan(0L));
        assertThat(stringsBreakdown.get(BUILD_LEAF_COLLECTOR), greaterThan(0L));
        assertThat(stringsBreakdown.get(COLLECT), greaterThan(0L));
        assertThat(stringsBreakdown.get(POST_COLLECTION), greaterThan(0L));
        assertThat(stringsBreakdown.get(BUILD_AGGREGATION), greaterThan(0L));
        assertThat(stringsBreakdown.get(REDUCE), equalTo(0L));
        assertRemapTermsDebugInfo(stringsAggResult);
        assertThat(stringsAggResult.getProfiledChildren().size(), equalTo(3));
        Map<String, ProfileResult> stringsAggResultSubAggregations = stringsAggResult.getProfiledChildren().stream().collect(Collectors.toMap(ProfileResult::getLuceneDescription, s -> s));
        avgAggResult = stringsAggResultSubAggregations.get("avg");
        assertThat(avgAggResult, notNullValue());
        assertThat(avgAggResult.getQueryName(), equalTo("AvgAggregator"));
        assertThat(avgAggResult.getTime(), greaterThan(0L));
        avgBreakdown = avgAggResult.getTimeBreakdown();
        assertThat(avgBreakdown, notNullValue());
        assertThat(avgBreakdown.keySet(), equalTo(BREAKDOWN_KEYS));
        assertThat(avgBreakdown.get(INITIALIZE), greaterThan(0L));
        assertThat(avgBreakdown.get(BUILD_LEAF_COLLECTOR), greaterThan(0L));
        assertThat(avgBreakdown.get(COLLECT), greaterThan(0L));
        assertThat(avgBreakdown.get(POST_COLLECTION), greaterThan(0L));
        assertThat(avgBreakdown.get(BUILD_AGGREGATION), greaterThan(0L));
        assertThat(avgBreakdown.get(REDUCE), equalTo(0L));
        assertThat(avgAggResult.getDebugInfo(), equalTo(org.opensearch.common.collect.Map.of()));
        assertThat(avgAggResult.getProfiledChildren().size(), equalTo(0));
        maxAggResult = stringsAggResultSubAggregations.get("max");
        assertThat(maxAggResult, notNullValue());
        assertThat(maxAggResult.getQueryName(), equalTo("MaxAggregator"));
        assertThat(maxAggResult.getTime(), greaterThan(0L));
        maxBreakdown = maxAggResult.getTimeBreakdown();
        assertThat(maxBreakdown, notNullValue());
        assertThat(maxBreakdown.keySet(), equalTo(BREAKDOWN_KEYS));
        assertThat(maxBreakdown.get(INITIALIZE), greaterThan(0L));
        assertThat(maxBreakdown.get(BUILD_LEAF_COLLECTOR), greaterThan(0L));
        assertThat(maxBreakdown.get(COLLECT), greaterThan(0L));
        assertThat(maxBreakdown.get(POST_COLLECTION), greaterThan(0L));
        assertThat(maxBreakdown.get(BUILD_AGGREGATION), greaterThan(0L));
        assertThat(maxBreakdown.get(REDUCE), equalTo(0L));
        assertThat(maxAggResult.getDebugInfo(), equalTo(org.opensearch.common.collect.Map.of()));
        assertThat(maxAggResult.getProfiledChildren().size(), equalTo(0));
        tagsAggResult = stringsAggResultSubAggregations.get("tags");
        assertThat(tagsAggResult, notNullValue());
        assertThat(tagsAggResult.getQueryName(), equalTo(GlobalOrdinalsStringTermsAggregator.class.getSimpleName()));
        assertThat(tagsAggResult.getLuceneDescription(), equalTo("tags"));
        assertThat(tagsAggResult.getTime(), greaterThan(0L));
        tagsBreakdown = tagsAggResult.getTimeBreakdown();
        assertThat(tagsBreakdown, notNullValue());
        assertThat(tagsBreakdown.keySet(), equalTo(BREAKDOWN_KEYS));
        assertThat(tagsBreakdown.get(INITIALIZE), greaterThan(0L));
        assertThat(tagsBreakdown.get(BUILD_LEAF_COLLECTOR), greaterThan(0L));
        assertThat(tagsBreakdown.get(COLLECT), greaterThan(0L));
        assertThat(tagsBreakdown.get(POST_COLLECTION), greaterThan(0L));
        assertThat(tagsBreakdown.get(BUILD_AGGREGATION), greaterThan(0L));
        assertThat(tagsBreakdown.get(REDUCE), equalTo(0L));
        assertRemapTermsDebugInfo(tagsAggResult);
        assertThat(tagsAggResult.getProfiledChildren().size(), equalTo(2));
        tagsAggResultSubAggregations = tagsAggResult.getProfiledChildren().stream().collect(Collectors.toMap(ProfileResult::getLuceneDescription, s -> s));
        avgAggResult = tagsAggResultSubAggregations.get("avg");
        assertThat(avgAggResult, notNullValue());
        assertThat(avgAggResult.getQueryName(), equalTo("AvgAggregator"));
        assertThat(avgAggResult.getTime(), greaterThan(0L));
        avgBreakdown = avgAggResult.getTimeBreakdown();
        assertThat(avgBreakdown, notNullValue());
        assertThat(avgBreakdown.keySet(), equalTo(BREAKDOWN_KEYS));
        assertThat(avgBreakdown.get(INITIALIZE), greaterThan(0L));
        assertThat(avgBreakdown.get(BUILD_LEAF_COLLECTOR), greaterThan(0L));
        assertThat(avgBreakdown.get(COLLECT), greaterThan(0L));
        assertThat(avgBreakdown.get(POST_COLLECTION), greaterThan(0L));
        assertThat(avgBreakdown.get(BUILD_AGGREGATION), greaterThan(0L));
        assertThat(avgBreakdown.get(REDUCE), equalTo(0L));
        assertThat(avgAggResult.getDebugInfo(), equalTo(org.opensearch.common.collect.Map.of()));
        assertThat(avgAggResult.getProfiledChildren().size(), equalTo(0));
        maxAggResult = tagsAggResultSubAggregations.get("max");
        assertThat(maxAggResult, notNullValue());
        assertThat(maxAggResult.getQueryName(), equalTo("MaxAggregator"));
        assertThat(maxAggResult.getTime(), greaterThan(0L));
        maxBreakdown = maxAggResult.getTimeBreakdown();
        assertThat(maxBreakdown, notNullValue());
        assertThat(maxBreakdown.keySet(), equalTo(BREAKDOWN_KEYS));
        assertThat(maxBreakdown.get(INITIALIZE), greaterThan(0L));
        assertThat(maxBreakdown.get(BUILD_LEAF_COLLECTOR), greaterThan(0L));
        assertThat(maxBreakdown.get(COLLECT), greaterThan(0L));
        assertThat(maxBreakdown.get(POST_COLLECTION), greaterThan(0L));
        assertThat(maxBreakdown.get(BUILD_AGGREGATION), greaterThan(0L));
        assertThat(maxBreakdown.get(REDUCE), equalTo(0L));
        assertThat(maxAggResult.getDebugInfo(), equalTo(org.opensearch.common.collect.Map.of()));
        assertThat(maxAggResult.getProfiledChildren().size(), equalTo(0));
    }
}
Also used : IndexRequestBuilder(org.opensearch.action.index.IndexRequestBuilder) XContentFactory.jsonBuilder(org.opensearch.common.xcontent.XContentFactory.jsonBuilder) BucketOrder(org.opensearch.search.aggregations.BucketOrder) DiversifiedOrdinalsSamplerAggregator(org.opensearch.search.aggregations.bucket.sampler.DiversifiedOrdinalsSamplerAggregator) ProfileShardResult(org.opensearch.search.profile.ProfileShardResult) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) AggregationBuilders.terms(org.opensearch.search.aggregations.AggregationBuilders.terms) ArrayList(java.util.ArrayList) SubAggCollectionMode(org.opensearch.search.aggregations.Aggregator.SubAggCollectionMode) Map(java.util.Map) AggregationBuilders.histogram(org.opensearch.search.aggregations.AggregationBuilders.histogram) AggregationBuilders.avg(org.opensearch.search.aggregations.AggregationBuilders.avg) SearchResponse(org.opensearch.action.search.SearchResponse) OpenSearchAssertions.assertAcked(org.opensearch.test.hamcrest.OpenSearchAssertions.assertAcked) Matchers.hasEntry(org.hamcrest.Matchers.hasEntry) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) Set(java.util.Set) AggregationBuilders.max(org.opensearch.search.aggregations.AggregationBuilders.max) GlobalOrdinalsStringTermsAggregator(org.opensearch.search.aggregations.bucket.terms.GlobalOrdinalsStringTermsAggregator) Collectors(java.util.stream.Collectors) List(java.util.List) AggregationBuilders.diversifiedSampler(org.opensearch.search.aggregations.AggregationBuilders.diversifiedSampler) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Matchers.greaterThan(org.hamcrest.Matchers.greaterThan) ProfileResult(org.opensearch.search.profile.ProfileResult) OpenSearchIntegTestCase(org.opensearch.test.OpenSearchIntegTestCase) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse) ProfileResult(org.opensearch.search.profile.ProfileResult) ProfileShardResult(org.opensearch.search.profile.ProfileShardResult)

Example 3 with ProfileShardResult

use of org.opensearch.search.profile.ProfileShardResult in project OpenSearch by opensearch-project.

the class AggregationProfilerIT method testMultiLevelProfileBreadthFirst.

public void testMultiLevelProfileBreadthFirst() {
    SearchResponse response = client().prepareSearch("idx").setProfile(true).addAggregation(histogram("histo").field(NUMBER_FIELD).interval(1L).subAggregation(terms("terms").collectMode(SubAggCollectionMode.BREADTH_FIRST).field(TAG_FIELD).subAggregation(avg("avg").field(NUMBER_FIELD)))).get();
    assertSearchResponse(response);
    Map<String, ProfileShardResult> profileResults = response.getProfileResults();
    assertThat(profileResults, notNullValue());
    assertThat(profileResults.size(), equalTo(getNumShards("idx").numPrimaries));
    for (ProfileShardResult profileShardResult : profileResults.values()) {
        assertThat(profileShardResult, notNullValue());
        AggregationProfileShardResult aggProfileResults = profileShardResult.getAggregationProfileResults();
        assertThat(aggProfileResults, notNullValue());
        List<ProfileResult> aggProfileResultsList = aggProfileResults.getProfileResults();
        assertThat(aggProfileResultsList, notNullValue());
        assertThat(aggProfileResultsList.size(), equalTo(1));
        ProfileResult histoAggResult = aggProfileResultsList.get(0);
        assertThat(histoAggResult, notNullValue());
        assertThat(histoAggResult.getQueryName(), equalTo("NumericHistogramAggregator"));
        assertThat(histoAggResult.getLuceneDescription(), equalTo("histo"));
        assertThat(histoAggResult.getTime(), greaterThan(0L));
        Map<String, Long> histoBreakdown = histoAggResult.getTimeBreakdown();
        assertThat(histoBreakdown, notNullValue());
        assertThat(histoBreakdown.keySet(), equalTo(BREAKDOWN_KEYS));
        assertThat(histoBreakdown.get(INITIALIZE), greaterThan(0L));
        assertThat(histoBreakdown.get(COLLECT), greaterThan(0L));
        assertThat(histoBreakdown.get(BUILD_AGGREGATION), greaterThan(0L));
        assertThat(histoBreakdown.get(REDUCE), equalTo(0L));
        Map<String, Object> histoDebugInfo = histoAggResult.getDebugInfo();
        assertThat(histoDebugInfo, notNullValue());
        assertThat(histoDebugInfo.keySet(), equalTo(org.opensearch.common.collect.Set.of(TOTAL_BUCKETS)));
        assertThat(((Number) histoDebugInfo.get(TOTAL_BUCKETS)).longValue(), greaterThan(0L));
        assertThat(histoAggResult.getProfiledChildren().size(), equalTo(1));
        ProfileResult termsAggResult = histoAggResult.getProfiledChildren().get(0);
        assertThat(termsAggResult, notNullValue());
        assertThat(termsAggResult.getQueryName(), equalTo(GlobalOrdinalsStringTermsAggregator.class.getSimpleName()));
        assertThat(termsAggResult.getLuceneDescription(), equalTo("terms"));
        assertThat(termsAggResult.getTime(), greaterThan(0L));
        Map<String, Long> termsBreakdown = termsAggResult.getTimeBreakdown();
        assertThat(termsBreakdown, notNullValue());
        assertThat(termsBreakdown.keySet(), equalTo(BREAKDOWN_KEYS));
        assertThat(termsBreakdown.get(INITIALIZE), greaterThan(0L));
        assertThat(termsBreakdown.get(COLLECT), greaterThan(0L));
        assertThat(termsBreakdown.get(BUILD_AGGREGATION), greaterThan(0L));
        assertThat(termsBreakdown.get(REDUCE), equalTo(0L));
        assertRemapTermsDebugInfo(termsAggResult);
        assertThat(termsAggResult.getProfiledChildren().size(), equalTo(1));
        ProfileResult avgAggResult = termsAggResult.getProfiledChildren().get(0);
        assertThat(avgAggResult, notNullValue());
        assertThat(avgAggResult.getQueryName(), equalTo("AvgAggregator"));
        assertThat(avgAggResult.getLuceneDescription(), equalTo("avg"));
        assertThat(avgAggResult.getTime(), greaterThan(0L));
        Map<String, Long> avgBreakdown = avgAggResult.getTimeBreakdown();
        assertThat(avgBreakdown, notNullValue());
        assertThat(avgBreakdown.keySet(), equalTo(BREAKDOWN_KEYS));
        assertThat(avgBreakdown.get(INITIALIZE), greaterThan(0L));
        assertThat(avgBreakdown.get(COLLECT), greaterThan(0L));
        assertThat(avgBreakdown.get(BUILD_AGGREGATION), greaterThan(0L));
        assertThat(avgBreakdown.get(REDUCE), equalTo(0L));
        assertThat(avgAggResult.getDebugInfo(), equalTo(org.opensearch.common.collect.Map.of()));
        assertThat(avgAggResult.getProfiledChildren().size(), equalTo(0));
    }
}
Also used : ProfileResult(org.opensearch.search.profile.ProfileResult) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse) ProfileShardResult(org.opensearch.search.profile.ProfileShardResult)

Example 4 with ProfileShardResult

use of org.opensearch.search.profile.ProfileShardResult in project OpenSearch by opensearch-project.

the class AggregationProfilerIT method testNoProfile.

public void testNoProfile() {
    SearchResponse response = client().prepareSearch("idx").setProfile(false).addAggregation(histogram("histo").field(NUMBER_FIELD).interval(1L).subAggregation(terms("tags").field(TAG_FIELD).subAggregation(avg("avg").field(NUMBER_FIELD)).subAggregation(max("max").field(NUMBER_FIELD))).subAggregation(terms("strings").field(STRING_FIELD).subAggregation(avg("avg").field(NUMBER_FIELD)).subAggregation(max("max").field(NUMBER_FIELD)).subAggregation(terms("tags").field(TAG_FIELD).subAggregation(avg("avg").field(NUMBER_FIELD)).subAggregation(max("max").field(NUMBER_FIELD))))).get();
    assertSearchResponse(response);
    Map<String, ProfileShardResult> profileResults = response.getProfileResults();
    assertThat(profileResults, notNullValue());
    assertThat(profileResults.size(), equalTo(0));
}
Also used : OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse) ProfileShardResult(org.opensearch.search.profile.ProfileShardResult)

Example 5 with ProfileShardResult

use of org.opensearch.search.profile.ProfileShardResult in project OpenSearch by opensearch-project.

the class AggregationProfilerIT method testDiversifiedAggProfile.

public void testDiversifiedAggProfile() {
    SearchResponse response = client().prepareSearch("idx").setProfile(true).addAggregation(diversifiedSampler("diversify").shardSize(10).field(STRING_FIELD).maxDocsPerValue(2).subAggregation(max("max").field(NUMBER_FIELD))).get();
    assertSearchResponse(response);
    Map<String, ProfileShardResult> profileResults = response.getProfileResults();
    assertThat(profileResults, notNullValue());
    assertThat(profileResults.size(), equalTo(getNumShards("idx").numPrimaries));
    for (ProfileShardResult profileShardResult : profileResults.values()) {
        assertThat(profileShardResult, notNullValue());
        AggregationProfileShardResult aggProfileResults = profileShardResult.getAggregationProfileResults();
        assertThat(aggProfileResults, notNullValue());
        List<ProfileResult> aggProfileResultsList = aggProfileResults.getProfileResults();
        assertThat(aggProfileResultsList, notNullValue());
        assertThat(aggProfileResultsList.size(), equalTo(1));
        ProfileResult diversifyAggResult = aggProfileResultsList.get(0);
        assertThat(diversifyAggResult, notNullValue());
        assertThat(diversifyAggResult.getQueryName(), equalTo(DiversifiedOrdinalsSamplerAggregator.class.getSimpleName()));
        assertThat(diversifyAggResult.getLuceneDescription(), equalTo("diversify"));
        assertThat(diversifyAggResult.getTime(), greaterThan(0L));
        Map<String, Long> diversifyBreakdown = diversifyAggResult.getTimeBreakdown();
        assertThat(diversifyBreakdown, notNullValue());
        assertThat(diversifyBreakdown.keySet(), equalTo(BREAKDOWN_KEYS));
        assertThat(diversifyBreakdown.get(INITIALIZE), greaterThan(0L));
        assertThat(diversifyBreakdown.get(BUILD_LEAF_COLLECTOR), greaterThan(0L));
        assertThat(diversifyBreakdown.get(COLLECT), greaterThan(0L));
        assertThat(diversifyBreakdown.get(POST_COLLECTION), greaterThan(0L));
        assertThat(diversifyBreakdown.get(BUILD_AGGREGATION), greaterThan(0L));
        assertThat(diversifyBreakdown.get(REDUCE), equalTo(0L));
        assertThat(diversifyAggResult.getDebugInfo(), equalTo(org.opensearch.common.collect.Map.of(DEFERRED, org.opensearch.common.collect.List.of("max"))));
        assertThat(diversifyAggResult.getProfiledChildren().size(), equalTo(1));
        ProfileResult maxAggResult = diversifyAggResult.getProfiledChildren().get(0);
        assertThat(maxAggResult, notNullValue());
        assertThat(maxAggResult.getQueryName(), equalTo("MaxAggregator"));
        assertThat(maxAggResult.getLuceneDescription(), equalTo("max"));
        assertThat(maxAggResult.getTime(), greaterThan(0L));
        Map<String, Long> maxBreakdown = maxAggResult.getTimeBreakdown();
        assertThat(maxBreakdown, notNullValue());
        assertThat(maxBreakdown.keySet(), equalTo(BREAKDOWN_KEYS));
        assertThat(diversifyBreakdown.get(INITIALIZE), greaterThan(0L));
        assertThat(diversifyBreakdown.get(BUILD_LEAF_COLLECTOR), greaterThan(0L));
        assertThat(diversifyBreakdown.get(COLLECT), greaterThan(0L));
        assertThat(diversifyBreakdown.get(POST_COLLECTION), greaterThan(0L));
        assertThat(maxBreakdown.get(BUILD_AGGREGATION), greaterThan(0L));
        assertThat(maxBreakdown.get(REDUCE), equalTo(0L));
        assertThat(maxAggResult.getDebugInfo(), equalTo(org.opensearch.common.collect.Map.of()));
        assertThat(maxAggResult.getProfiledChildren().size(), equalTo(0));
    }
}
Also used : ProfileResult(org.opensearch.search.profile.ProfileResult) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse) ProfileShardResult(org.opensearch.search.profile.ProfileShardResult)

Aggregations

ProfileShardResult (org.opensearch.search.profile.ProfileShardResult)20 ProfileResult (org.opensearch.search.profile.ProfileResult)14 IndexRequestBuilder (org.opensearch.action.index.IndexRequestBuilder)10 Matchers.emptyOrNullString (org.hamcrest.Matchers.emptyOrNullString)9 QueryBuilder (org.opensearch.index.query.QueryBuilder)9 RandomQueryGenerator.randomQueryBuilder (org.opensearch.search.profile.query.RandomQueryGenerator.randomQueryBuilder)9 SearchResponse (org.opensearch.action.search.SearchResponse)6 OpenSearchAssertions.assertSearchResponse (org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse)6 TotalHits (org.apache.lucene.search.TotalHits)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 SearchProfileShardResults (org.opensearch.search.profile.SearchProfileShardResults)3 HashMap (java.util.HashMap)2 TopDocs (org.apache.lucene.search.TopDocs)2 TopDocsAndMaxScore (org.opensearch.common.lucene.search.TopDocsAndMaxScore)2 SearchHits (org.opensearch.search.SearchHits)2 InternalAggregations (org.opensearch.search.aggregations.InternalAggregations)2 InternalSearchResponse (org.opensearch.search.internal.InternalSearchResponse)2 Suggest (org.opensearch.search.suggest.Suggest)2 CompletionSuggestion (org.opensearch.search.suggest.completion.CompletionSuggestion)2