|
37 | 37 | import org.opensearch.action.index.IndexRequestBuilder; |
38 | 38 | import org.opensearch.action.search.SearchResponse; |
39 | 39 | import org.opensearch.common.settings.Settings; |
| 40 | +import org.opensearch.index.query.QueryBuilders; |
40 | 41 | import org.opensearch.search.aggregations.Aggregator.SubAggCollectionMode; |
41 | 42 | import org.opensearch.search.aggregations.BucketOrder; |
42 | 43 | import org.opensearch.search.aggregations.InternalAggregation; |
|
46 | 47 | import org.opensearch.search.aggregations.metrics.Stats; |
47 | 48 | import org.opensearch.search.profile.ProfileResult; |
48 | 49 | import org.opensearch.search.profile.ProfileShardResult; |
| 50 | +import org.opensearch.search.profile.fetch.FetchProfileShardResult; |
49 | 51 | import org.opensearch.search.profile.query.CollectorResult; |
50 | 52 | import org.opensearch.search.profile.query.QueryProfileShardResult; |
| 53 | +import org.opensearch.search.sort.SortBuilders; |
| 54 | +import org.opensearch.search.sort.SortOrder; |
51 | 55 | import org.opensearch.test.OpenSearchIntegTestCase; |
52 | 56 | import org.opensearch.test.ParameterizedStaticSettingsOpenSearchIntegTestCase; |
53 | 57 | import org.hamcrest.core.IsNull; |
|
69 | 73 | import static org.opensearch.search.aggregations.AggregationBuilders.max; |
70 | 74 | import static org.opensearch.search.aggregations.AggregationBuilders.stats; |
71 | 75 | import static org.opensearch.search.aggregations.AggregationBuilders.terms; |
| 76 | +import static org.opensearch.search.aggregations.AggregationBuilders.topHits; |
72 | 77 | import static org.opensearch.test.hamcrest.OpenSearchAssertions.assertAcked; |
73 | 78 | import static org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse; |
74 | 79 | import static org.hamcrest.Matchers.containsString; |
@@ -1000,4 +1005,93 @@ private void assertCollectorResultWithConcurrentSearchEnabled(QueryProfileShardR |
1000 | 1005 | assertThat(collectorResult.getCollectorResult().getProfiledChildren().get(1).getReason(), equalTo(REASON_AGGREGATION)); |
1001 | 1006 | } |
1002 | 1007 | } |
| 1008 | + |
| 1009 | + public void testTopHitsAggregationFetchProfiling() throws Exception { |
| 1010 | + SearchResponse response = client().prepareSearch("idx") |
| 1011 | + .setProfile(true) |
| 1012 | + .setQuery(QueryBuilders.matchAllQuery()) |
| 1013 | + .addAggregation(topHits("top_hits_agg1").size(1)) |
| 1014 | + .addAggregation(topHits("top_hits_agg2").size(1).sort(SortBuilders.fieldSort(NUMBER_FIELD).order(SortOrder.DESC))) |
| 1015 | + .get(); |
| 1016 | + |
| 1017 | + assertSearchResponse(response); |
| 1018 | + Map<String, ProfileShardResult> profileResults = response.getProfileResults(); |
| 1019 | + assertNotNull("Profile results should not be null", profileResults); |
| 1020 | + assertFalse("Profile results should not be empty", profileResults.isEmpty()); |
| 1021 | + |
| 1022 | + int shardsWithDocuments = 0; |
| 1023 | + int shardsWithCorrectProfile = 0; |
| 1024 | + |
| 1025 | + for (ProfileShardResult shardResult : profileResults.values()) { |
| 1026 | + FetchProfileShardResult fetchProfileResult = shardResult.getFetchProfileResult(); |
| 1027 | + if (fetchProfileResult != null && !fetchProfileResult.getFetchProfileResults().isEmpty()) { |
| 1028 | + shardsWithDocuments++; |
| 1029 | + List<ProfileResult> fetchProfileResults = fetchProfileResult.getFetchProfileResults(); |
| 1030 | + |
| 1031 | + // Count different types of fetch operations dynamically |
| 1032 | + int mainFetchCount = 0; |
| 1033 | + int topHitsAgg1Count = 0; |
| 1034 | + int topHitsAgg2Count = 0; |
| 1035 | + ProfileResult topHitsFetch1 = null; |
| 1036 | + ProfileResult topHitsFetch2 = null; |
| 1037 | + |
| 1038 | + for (ProfileResult result : fetchProfileResults) { |
| 1039 | + if ("fetch".equals(result.getQueryName())) { |
| 1040 | + mainFetchCount++; |
| 1041 | + } else if (result.getQueryName().contains("top_hits_agg1")) { |
| 1042 | + if (topHitsFetch1 == null) { |
| 1043 | + topHitsFetch1 = result; // Keep first instance for validation |
| 1044 | + } |
| 1045 | + topHitsAgg1Count++; |
| 1046 | + } else if (result.getQueryName().contains("top_hits_agg2")) { |
| 1047 | + if (topHitsFetch2 == null) { |
| 1048 | + topHitsFetch2 = result; // Keep first instance for validation |
| 1049 | + } |
| 1050 | + topHitsAgg2Count++; |
| 1051 | + } |
| 1052 | + } |
| 1053 | + |
| 1054 | + // Verify we have the expected aggregations (concurrent search may create multiple instances) |
| 1055 | + assertTrue("Should have at least 1 top_hits_agg1 fetch operation", topHitsAgg1Count >= 1); |
| 1056 | + assertTrue("Should have at least 1 top_hits_agg2 fetch operation", topHitsAgg2Count >= 1); |
| 1057 | + assertTrue("Should have at least one main fetch operation", mainFetchCount >= 1); |
| 1058 | + assertTrue("Should have at least 3 total fetch operations", fetchProfileResults.size() >= 3); |
| 1059 | + |
| 1060 | + assertNotNull("Should have top_hits_agg1 fetch operation", topHitsFetch1); |
| 1061 | + assertTrue("Should be top_hits aggregation fetch", topHitsFetch1.getQueryName().startsWith("fetch_top_hits_aggregation")); |
| 1062 | + assertTrue("Should contain aggregation name", topHitsFetch1.getQueryName().contains("top_hits_agg1")); |
| 1063 | + assertNotNull(topHitsFetch1.getTimeBreakdown()); |
| 1064 | + assertEquals("Top hits fetch should have 1 child (FetchSourcePhase)", 1, topHitsFetch1.getProfiledChildren().size()); |
| 1065 | + assertEquals("FetchSourcePhase", topHitsFetch1.getProfiledChildren().get(0).getQueryName()); |
| 1066 | + |
| 1067 | + assertNotNull("Should have top_hits_agg2 fetch operation", topHitsFetch2); |
| 1068 | + assertTrue("Should be top_hits aggregation fetch", topHitsFetch2.getQueryName().startsWith("fetch_top_hits_aggregation")); |
| 1069 | + assertTrue("Should contain aggregation name", topHitsFetch2.getQueryName().contains("top_hits_agg2")); |
| 1070 | + assertNotNull(topHitsFetch2.getTimeBreakdown()); |
| 1071 | + assertEquals("Top hits fetch should have 1 child (FetchSourcePhase)", 1, topHitsFetch2.getProfiledChildren().size()); |
| 1072 | + assertEquals("FetchSourcePhase", topHitsFetch2.getProfiledChildren().get(0).getQueryName()); |
| 1073 | + |
| 1074 | + for (ProfileResult fetchResult : fetchProfileResults) { |
| 1075 | + Map<String, Long> breakdown = fetchResult.getTimeBreakdown(); |
| 1076 | + assertTrue( |
| 1077 | + "CREATE_STORED_FIELDS_VISITOR timing should be present", |
| 1078 | + breakdown.containsKey("create_stored_fields_visitor") |
| 1079 | + ); |
| 1080 | + assertTrue("BUILD_SUB_PHASE_PROCESSORS timing should be present", breakdown.containsKey("build_sub_phase_processors")); |
| 1081 | + assertTrue("GET_NEXT_READER timing should be present", breakdown.containsKey("get_next_reader")); |
| 1082 | + assertTrue("LOAD_STORED_FIELDS timing should be present", breakdown.containsKey("load_stored_fields")); |
| 1083 | + assertTrue("LOAD_SOURCE timing should be present", breakdown.containsKey("load_source")); |
| 1084 | + } |
| 1085 | + |
| 1086 | + shardsWithCorrectProfile++; |
| 1087 | + } |
| 1088 | + } |
| 1089 | + |
| 1090 | + assertTrue("Should have at least one shard with documents", shardsWithDocuments > 0); |
| 1091 | + assertEquals( |
| 1092 | + "All shards with documents should have correct fetch profile structure", |
| 1093 | + shardsWithDocuments, |
| 1094 | + shardsWithCorrectProfile |
| 1095 | + ); |
| 1096 | + } |
1003 | 1097 | } |
0 commit comments