160160import static org .elasticsearch .core .TimeValue .timeValueMillis ;
161161import static org .elasticsearch .core .TimeValue .timeValueMinutes ;
162162import static org .elasticsearch .index .seqno .SequenceNumbers .UNASSIGNED_SEQ_NO ;
163+ import static org .elasticsearch .search .SearchService .SearchOperationType .QUERY ;
164+ import static org .elasticsearch .search .SearchService .SearchOperationType .FETCH ;
165+ import static org .elasticsearch .search .SearchService .SearchOperationType .DFS ;
163166import static org .elasticsearch .search .rank .feature .RankFeatureShardPhase .EMPTY_RESULT ;
164167
165168public class SearchService extends AbstractLifecycleComponent implements IndexEventListener {
@@ -577,7 +580,7 @@ private DfsSearchResult executeDfsPhase(ShardSearchRequest request, SearchShardT
577580 Releasable scope = tracer .withScope (task );
578581 Releasable ignored = readerContext .markAsUsed (getKeepAlive (request ));
579582 SearchContext context = createContext (readerContext , request , task , ResultsType .DFS , false );
580- SearchOperationListenerExecutor executor = new SearchOperationListenerExecutor (context , false , true , System .nanoTime ())
583+ SearchOperationListenerExecutor executor = new SearchOperationListenerExecutor (context , DFS , System .nanoTime ())
581584 ) {
582585 DfsPhase .execute (context );
583586 executor .success ();
@@ -828,7 +831,7 @@ public void executeRankFeaturePhase(RankFeatureShardRequest request, SearchShard
828831 private QueryFetchSearchResult executeFetchPhase (ReaderContext reader , SearchContext context , long afterQueryTime ) {
829832 try (
830833 Releasable scope = tracer .withScope (context .getTask ());
831- SearchOperationListenerExecutor executor = new SearchOperationListenerExecutor (context , true , afterQueryTime )
834+ SearchOperationListenerExecutor executor = new SearchOperationListenerExecutor (context , FETCH , afterQueryTime )
832835 ) {
833836 fetchPhase .execute (context , shortcutDocIdsToLoad (context ), null );
834837 if (reader .singleSession ()) {
@@ -992,7 +995,7 @@ public void executeFetchPhase(ShardFetchRequest request, CancellableTask task, A
992995 try (
993996 SearchOperationListenerExecutor executor = new SearchOperationListenerExecutor (
994997 searchContext ,
995- true ,
998+ FETCH ,
996999 System .nanoTime ()
9971000 )
9981001 ) {
@@ -1975,6 +1978,10 @@ public AggregationReduceContext forFinalReduction() {
19751978 };
19761979 }
19771980
1981+ enum SearchOperationType {
1982+ QUERY , FETCH , DFS
1983+ }
1984+
19781985 /**
19791986 * This helper class ensures we only execute either the success or the failure path for {@link SearchOperationListener}.
19801987 * This is crucial for some implementations like {@link org.elasticsearch.index.search.stats.ShardSearchStats}.
@@ -1983,28 +1990,22 @@ private static final class SearchOperationListenerExecutor implements AutoClosea
19831990 private final SearchOperationListener listener ;
19841991 private final SearchContext context ;
19851992 private final long time ;
1986- private final boolean fetch ;
1987- private final boolean dfs ;
1993+ private final SearchOperationType searchType ;
19881994 private long afterQueryTime = -1 ;
19891995 private boolean closed = false ;
19901996
19911997 SearchOperationListenerExecutor (SearchContext context ) {
1992- this (context , false , System .nanoTime ());
1993- }
1994-
1995- SearchOperationListenerExecutor (SearchContext context , boolean fetch , long startTime ) {
1996- this (context , fetch , false , startTime );
1998+ this (context , QUERY , System .nanoTime ());
19971999 }
19982000
1999- SearchOperationListenerExecutor (SearchContext context , boolean fetch , boolean dfs , long startTime ) {
2001+ SearchOperationListenerExecutor (SearchContext context , SearchOperationType searchType , long startTime ) {
20002002 this .listener = context .indexShard ().getSearchOperationListener ();
20012003 this .context = context ;
20022004 time = startTime ;
2003- this .fetch = fetch ;
2004- this .dfs = dfs ;
2005- if (fetch ) {
2005+ this .searchType = searchType ;
2006+ if (this .searchType == FETCH ) {
20062007 listener .onPreFetchPhase (context );
2007- } else {
2008+ } else if ( this . searchType == QUERY ) {
20082009 listener .onPreQueryPhase (context );
20092010 }
20102011 }
@@ -2019,17 +2020,17 @@ public void close() {
20192020 if (closed == false ) {
20202021 closed = true ;
20212022 if (afterQueryTime != -1 ) {
2022- if (fetch ) {
2023+ if (this . searchType == FETCH ) {
20232024 listener .onFetchPhase (context , afterQueryTime - time );
2024- } else if (dfs ) {
2025+ } else if (this . searchType == DFS ) {
20252026 listener .onDfsPhase (context , afterQueryTime - time );
20262027 } else {
20272028 listener .onQueryPhase (context , afterQueryTime - time );
20282029 }
20292030 } else {
2030- if (fetch ) {
2031+ if (this . searchType == FETCH ) {
20312032 listener .onFailedFetchPhase (context );
2032- } else {
2033+ } else if ( this . searchType == QUERY ) {
20332034 listener .onFailedQueryPhase (context );
20342035 }
20352036 }
0 commit comments