1212import org .elasticsearch .index .search .stats .SearchStats ;
1313import org .elasticsearch .index .search .stats .ShardSearchStats ;
1414import org .elasticsearch .search .builder .SearchSourceBuilder ;
15+ import org .elasticsearch .search .internal .ReaderContext ;
1516import org .elasticsearch .search .internal .SearchContext ;
1617import org .elasticsearch .search .internal .ShardSearchRequest ;
1718import org .elasticsearch .search .suggest .SuggestBuilder ;
1819import org .elasticsearch .test .ESTestCase ;
1920
21+ import java .util .Arrays ;
22+ import java .util .Objects ;
2023import java .util .concurrent .TimeUnit ;
2124
2225import static org .mockito .Mockito .mock ;
@@ -34,10 +37,32 @@ public void testDfsPhase() {
3437 listener .onPreDfsPhase (sc );
3538 listener .onDfsPhase (sc , TimeUnit .MILLISECONDS .toNanos (TEN_MILLIS ));
3639
37- SearchStats stats = listener .stats ();
38- assertEquals (0 , stats .getTotal ().getDfsCurrent ());
39- assertEquals (1 , stats .getTotal ().getDfsCount ());
40- assertEquals (TEN_MILLIS , stats .getTotal ().getDfsTimeInMillis ());
40+ SearchStats .Stats stats = listener .stats ().getTotal ();
41+ assertEquals (0 , stats .getDfsCurrent ());
42+ assertEquals (1 , stats .getDfsCount ());
43+ assertEquals (TEN_MILLIS , stats .getDfsTimeInMillis ());
44+ }
45+
46+ public void testDfsPhase_withGroups () {
47+ String [] groups = new String [] { "group1" };
48+
49+ ShardSearchStats listener = new ShardSearchStats ();
50+ SearchContext sc = mock (SearchContext .class );
51+ when (sc .groupStats ()).thenReturn (Arrays .asList (groups ));
52+
53+ listener .onPreDfsPhase (sc );
54+ listener .onDfsPhase (sc , TimeUnit .MILLISECONDS .toNanos (TEN_MILLIS ));
55+
56+ SearchStats searchStats = listener .stats (groups );
57+ SearchStats .Stats stats = searchStats .getTotal ();
58+ assertEquals (0 , stats .getDfsCurrent ());
59+ assertEquals (1 , stats .getDfsCount ());
60+ assertEquals (TEN_MILLIS , stats .getDfsTimeInMillis ());
61+
62+ stats = Objects .requireNonNull (searchStats .getGroupStats ()).get ("group1" );
63+ assertEquals (0 , stats .getDfsCurrent ());
64+ assertEquals (1 , stats .getDfsCount ());
65+ assertEquals (TEN_MILLIS , stats .getDfsTimeInMillis ());
4166 }
4267
4368 public void testDfsPhase_Failure () {
@@ -48,10 +73,10 @@ public void testDfsPhase_Failure() {
4873 listener .onPreDfsPhase (sc );
4974 listener .onFailedDfsPhase (sc );
5075
51- SearchStats stats = listener .stats ();
52- assertEquals (0 , stats .getTotal (). getDfsCurrent ());
53- assertEquals (0 , stats .getTotal (). getDfsCount ());
54- assertEquals (1 , stats .getTotal (). getDfsFailure ());
76+ SearchStats . Stats stats = listener .stats (). getTotal ();
77+ assertEquals (0 , stats .getDfsCurrent ());
78+ assertEquals (0 , stats .getDfsCount ());
79+ assertEquals (1 , stats .getDfsFailure ());
5580 }
5681
5782 public void testQueryPhase_SuggestOnly () {
@@ -66,13 +91,13 @@ public void testQueryPhase_SuggestOnly() {
6691 listener .onPreQueryPhase (sc );
6792 listener .onQueryPhase (sc , TimeUnit .MILLISECONDS .toNanos (TEN_MILLIS ));
6893
69- SearchStats stats = listener .stats ();
70- assertEquals (0 , stats .getTotal (). getSuggestCurrent ());
71- assertEquals (1 , stats .getTotal (). getSuggestCount ());
72- assertEquals (TEN_MILLIS , stats .getTotal (). getSuggestTimeInMillis ());
73- assertEquals (0 , stats .getTotal (). getQueryCurrent ());
74- assertEquals (0 , stats .getTotal (). getQueryCount ());
75- assertEquals (0 , stats .getTotal (). getQueryTimeInMillis ());
94+ SearchStats . Stats stats = listener .stats (). getTotal ();
95+ assertEquals (0 , stats .getSuggestCurrent ());
96+ assertEquals (1 , stats .getSuggestCount ());
97+ assertEquals (TEN_MILLIS , stats .getSuggestTimeInMillis ());
98+ assertEquals (0 , stats .getQueryCurrent ());
99+ assertEquals (0 , stats .getQueryCount ());
100+ assertEquals (0 , stats .getQueryTimeInMillis ());
76101 }
77102
78103 public void testQueryPhase () {
@@ -85,10 +110,67 @@ public void testQueryPhase() {
85110 listener .onPreQueryPhase (sc );
86111 listener .onQueryPhase (sc , TimeUnit .MILLISECONDS .toNanos (TEN_MILLIS ));
87112
88- SearchStats stats = listener .stats ();
89- assertEquals (0 , stats .getTotal ().getQueryCurrent ());
90- assertEquals (1 , stats .getTotal ().getQueryCount ());
91- assertEquals (TEN_MILLIS , stats .getTotal ().getQueryTimeInMillis ());
113+ SearchStats .Stats stats = listener .stats ().getTotal ();
114+ assertEquals (0 , stats .getQueryCurrent ());
115+ assertEquals (1 , stats .getQueryCount ());
116+ assertEquals (TEN_MILLIS , stats .getQueryTimeInMillis ());
117+ }
118+
119+ public void testQueryPhase_withGroups () {
120+ String [] groups = new String [] { "group1" };
121+
122+ ShardSearchStats listener = new ShardSearchStats ();
123+ SearchContext sc = mock (SearchContext .class );
124+ ShardSearchRequest req = mock (ShardSearchRequest .class );
125+ when (sc .request ()).thenReturn (req );
126+ when (sc .groupStats ()).thenReturn (Arrays .asList (groups ));
127+
128+ listener .onPreQueryPhase (sc );
129+ listener .onQueryPhase (sc , TimeUnit .MILLISECONDS .toNanos (TEN_MILLIS ));
130+
131+ SearchStats searchStats = listener .stats ("_all" );
132+ SearchStats .Stats stats = listener .stats ().getTotal ();
133+ assertEquals (0 , stats .getQueryCurrent ());
134+ assertEquals (1 , stats .getQueryCount ());
135+ assertEquals (TEN_MILLIS , stats .getQueryTimeInMillis ());
136+
137+ stats = Objects .requireNonNull (searchStats .getGroupStats ()).get ("group1" );
138+ assertEquals (0 , stats .getQueryCurrent ());
139+ assertEquals (1 , stats .getQueryCount ());
140+ assertEquals (TEN_MILLIS , stats .getQueryTimeInMillis ());
141+ }
142+
143+ public void testQueryPhase_withGroups_SuggestOnly () {
144+ String [] groups = new String [] { "group1" };
145+
146+ ShardSearchStats listener = new ShardSearchStats ();
147+ SearchContext sc = mock (SearchContext .class );
148+ ShardSearchRequest req = mock (ShardSearchRequest .class );
149+ SearchSourceBuilder ssb = new SearchSourceBuilder ().suggest (new SuggestBuilder ());
150+ when (sc .request ()).thenReturn (req );
151+ when (sc .groupStats ()).thenReturn (null );
152+ when (req .source ()).thenReturn (ssb );
153+ when (sc .groupStats ()).thenReturn (Arrays .asList (groups ));
154+
155+ listener .onPreQueryPhase (sc );
156+ listener .onQueryPhase (sc , TimeUnit .MILLISECONDS .toNanos (TEN_MILLIS ));
157+
158+ SearchStats searchStats = listener .stats ("_all" );
159+ SearchStats .Stats stats = listener .stats ().getTotal ();
160+ assertEquals (0 , stats .getSuggestCurrent ());
161+ assertEquals (1 , stats .getSuggestCount ());
162+ assertEquals (TEN_MILLIS , stats .getSuggestTimeInMillis ());
163+ assertEquals (0 , stats .getQueryCurrent ());
164+ assertEquals (0 , stats .getQueryCount ());
165+ assertEquals (0 , stats .getQueryTimeInMillis ());
166+
167+ stats = Objects .requireNonNull (searchStats .getGroupStats ()).get ("group1" );
168+ assertEquals (0 , stats .getSuggestCurrent ());
169+ assertEquals (1 , stats .getSuggestCount ());
170+ assertEquals (TEN_MILLIS , stats .getSuggestTimeInMillis ());
171+ assertEquals (0 , stats .getQueryCurrent ());
172+ assertEquals (0 , stats .getQueryCount ());
173+ assertEquals (0 , stats .getQueryTimeInMillis ());
92174 }
93175
94176 public void testQueryPhase_SuggestOnly_Failure () {
@@ -103,12 +185,12 @@ public void testQueryPhase_SuggestOnly_Failure() {
103185 listener .onPreQueryPhase (sc );
104186 listener .onFailedQueryPhase (sc );
105187
106- SearchStats stats = listener .stats ();
107- assertEquals (0 , stats .getTotal (). getSuggestCurrent ());
108- assertEquals (0 , stats .getTotal (). getSuggestCount ());
109- assertEquals (0 , stats .getTotal (). getQueryCurrent ());
110- assertEquals (0 , stats .getTotal (). getQueryCount ());
111- assertEquals (0 , stats .getTotal (). getQueryFailure ());
188+ SearchStats . Stats stats = listener .stats (). getTotal ();
189+ assertEquals (0 , stats .getSuggestCurrent ());
190+ assertEquals (0 , stats .getSuggestCount ());
191+ assertEquals (0 , stats .getQueryCurrent ());
192+ assertEquals (0 , stats .getQueryCount ());
193+ assertEquals (0 , stats .getQueryFailure ());
112194 }
113195
114196 public void testQueryPhase_Failure () {
@@ -121,10 +203,10 @@ public void testQueryPhase_Failure() {
121203 listener .onPreQueryPhase (sc );
122204 listener .onFailedQueryPhase (sc );
123205
124- SearchStats stats = listener .stats ();
125- assertEquals (0 , stats .getTotal (). getQueryCurrent ());
126- assertEquals (0 , stats .getTotal (). getQueryCount ());
127- assertEquals (1 , stats .getTotal (). getQueryFailure ());
206+ SearchStats . Stats stats = listener .stats (). getTotal ();
207+ assertEquals (0 , stats .getQueryCurrent ());
208+ assertEquals (0 , stats .getQueryCount ());
209+ assertEquals (1 , stats .getQueryFailure ());
128210 }
129211
130212 public void testFetchPhase () {
@@ -137,10 +219,32 @@ public void testFetchPhase() {
137219 listener .onPreFetchPhase (sc );
138220 listener .onFetchPhase (sc , TimeUnit .MILLISECONDS .toNanos (TEN_MILLIS ));
139221
140- SearchStats stats = listener .stats ();
141- assertEquals (0 , stats .getTotal ().getFetchCurrent ());
142- assertEquals (1 , stats .getTotal ().getFetchCount ());
143- assertEquals (TEN_MILLIS , stats .getTotal ().getFetchTimeInMillis ());
222+ SearchStats .Stats stats = listener .stats ().getTotal ();
223+ assertEquals (0 , stats .getFetchCurrent ());
224+ assertEquals (1 , stats .getFetchCount ());
225+ assertEquals (TEN_MILLIS , stats .getFetchTimeInMillis ());
226+ }
227+
228+ public void testFetchPhase_withGroups () {
229+ String [] groups = new String [] { "group1" };
230+
231+ ShardSearchStats listener = new ShardSearchStats ();
232+ SearchContext sc = mock (SearchContext .class );
233+ when (sc .groupStats ()).thenReturn (Arrays .asList (groups ));
234+
235+ listener .onPreFetchPhase (sc );
236+ listener .onFetchPhase (sc , TimeUnit .MILLISECONDS .toNanos (TEN_MILLIS ));
237+
238+ SearchStats searchStats = listener .stats ("_all" );
239+ SearchStats .Stats stats = listener .stats ().getTotal ();
240+ assertEquals (0 , stats .getFetchCurrent ());
241+ assertEquals (1 , stats .getFetchCount ());
242+ assertEquals (TEN_MILLIS , stats .getFetchTimeInMillis ());
243+
244+ stats = Objects .requireNonNull (searchStats .getGroupStats ()).get ("group1" );
245+ assertEquals (0 , stats .getFetchCurrent ());
246+ assertEquals (1 , stats .getFetchCount ());
247+ assertEquals (TEN_MILLIS , stats .getFetchTimeInMillis ());
144248 }
145249
146250 public void testFetchPhase_Failure () {
@@ -151,9 +255,41 @@ public void testFetchPhase_Failure() {
151255 listener .onPreFetchPhase (sc );
152256 listener .onFailedFetchPhase (sc );
153257
258+ SearchStats .Stats stats = listener .stats ().getTotal ();
259+ assertEquals (0 , stats .getFetchCurrent ());
260+ assertEquals (0 , stats .getFetchCount ());
261+ assertEquals (1 , stats .getFetchFailure ());
262+ }
263+
264+ public void testReaderContext () {
265+ ShardSearchStats listener = new ShardSearchStats ();
266+ ReaderContext rc = mock (ReaderContext .class );
267+ SearchContext sc = mock (SearchContext .class );
268+ when (sc .groupStats ()).thenReturn (null );
269+
270+ listener .onNewReaderContext (rc );
154271 SearchStats stats = listener .stats ();
155- assertEquals (0 , stats .getTotal ().getFetchCurrent ());
156- assertEquals (0 , stats .getTotal ().getFetchCount ());
157- assertEquals (1 , stats .getTotal ().getFetchFailure ());
272+ assertEquals (1 , stats .getOpenContexts ());
273+
274+ listener .onFreeReaderContext (rc );
275+ stats = listener .stats ();
276+ assertEquals (0 , stats .getOpenContexts ());
277+ }
278+
279+ public void testScrollContext () {
280+ ShardSearchStats listener = new ShardSearchStats ();
281+ ReaderContext rc = mock (ReaderContext .class );
282+ SearchContext sc = mock (SearchContext .class );
283+ when (sc .groupStats ()).thenReturn (null );
284+
285+ listener .onNewScrollContext (rc );
286+ SearchStats stats = listener .stats ();
287+ assertEquals (1 , stats .getTotal ().getScrollCurrent ());
288+
289+ listener .onFreeScrollContext (rc );
290+ stats = listener .stats ();
291+ assertEquals (0 , stats .getTotal ().getScrollCurrent ());
292+ assertEquals (1 , stats .getTotal ().getScrollCount ());
293+ assertTrue (stats .getTotal ().getScrollTimeInMillis () > 0 );
158294 }
159295}
0 commit comments