@@ -92,6 +92,7 @@ public void onFailure(Exception e) {
9292
9393 private void innerRun () throws Exception {
9494 assert this .reducedQueryPhase == null ^ this .resultConsumer == null ;
95+ long phaseStartTimeInNanos = System .nanoTime ();
9596 // depending on whether we executed the RankFeaturePhase we may or may not have the reduced query result computed already
9697 final var reducedQueryPhase = this .reducedQueryPhase == null ? resultConsumer .reduce () : this .reducedQueryPhase ;
9798 final int numShards = context .getNumShards ();
@@ -104,22 +105,27 @@ private void innerRun() throws Exception {
104105 if (queryAndFetchOptimization ) {
105106 assert assertConsistentWithQueryAndFetchOptimization ();
106107 // query AND fetch optimization
107- moveToNextPhase (searchPhaseShardResults , reducedQueryPhase );
108+ moveToNextPhase (searchPhaseShardResults , reducedQueryPhase , phaseStartTimeInNanos );
108109 } else {
109110 ScoreDoc [] scoreDocs = reducedQueryPhase .sortedTopDocs ().scoreDocs ();
110111 // no docs to fetch -- sidestep everything and return
111112 if (scoreDocs .length == 0 ) {
112113 // we have to release contexts here to free up resources
113114 searchPhaseShardResults .asList ()
114115 .forEach (searchPhaseShardResult -> releaseIrrelevantSearchContext (searchPhaseShardResult , context ));
115- moveToNextPhase (new AtomicArray <>(0 ), reducedQueryPhase );
116+ moveToNextPhase (new AtomicArray <>(0 ), reducedQueryPhase , phaseStartTimeInNanos );
116117 } else {
117- innerRunFetch (scoreDocs , numShards , reducedQueryPhase );
118+ innerRunFetch (scoreDocs , numShards , reducedQueryPhase , phaseStartTimeInNanos );
118119 }
119120 }
120121 }
121122
122- private void innerRunFetch (ScoreDoc [] scoreDocs , int numShards , SearchPhaseController .ReducedQueryPhase reducedQueryPhase ) {
123+ private void innerRunFetch (
124+ ScoreDoc [] scoreDocs ,
125+ int numShards ,
126+ SearchPhaseController .ReducedQueryPhase reducedQueryPhase ,
127+ long phaseStartTimeInNanos
128+ ) {
123129 ArraySearchPhaseResults <FetchSearchResult > fetchResults = new ArraySearchPhaseResults <>(numShards );
124130 final List <Map <Integer , RankDoc >> rankDocsPerShard = false == shouldExplainRankScores (context .getRequest ())
125131 ? null
@@ -133,7 +139,7 @@ private void innerRunFetch(ScoreDoc[] scoreDocs, int numShards, SearchPhaseContr
133139 docIdsToLoad .length , // we count down every shard in the result no matter if we got any results or not
134140 () -> {
135141 try (fetchResults ) {
136- moveToNextPhase (fetchResults .getAtomicArray (), reducedQueryPhase );
142+ moveToNextPhase (fetchResults .getAtomicArray (), reducedQueryPhase , phaseStartTimeInNanos );
137143 }
138144 },
139145 context
@@ -258,8 +264,10 @@ public void onFailure(Exception e) {
258264
259265 private void moveToNextPhase (
260266 AtomicArray <? extends SearchPhaseResult > fetchResultsArr ,
261- SearchPhaseController .ReducedQueryPhase reducedQueryPhase
267+ SearchPhaseController .ReducedQueryPhase reducedQueryPhase ,
268+ long phaseStartTimeInNanos
262269 ) {
270+ context .getSearchResponseMetrics ().recordSearchPhaseDuration (getName (), System .nanoTime () - phaseStartTimeInNanos );
263271 context .executeNextPhase (NAME , () -> {
264272 var resp = SearchPhaseController .merge (context .getRequest ().scroll () != null , reducedQueryPhase , fetchResultsArr );
265273 context .addReleasable (resp );
0 commit comments