1212import org .opensearch .action .ActionType ;
1313import org .opensearch .action .search .SearchAction ;
1414import org .opensearch .action .search .SearchRequest ;
15+ import org .opensearch .action .search .SearchResponse ;
16+ import org .opensearch .action .search .StreamSearchAction ;
1517import org .opensearch .common .SetOnce ;
18+ import org .opensearch .common .settings .ClusterSettings ;
19+ import org .opensearch .common .settings .Settings ;
1620import org .opensearch .common .util .FeatureFlags ;
1721import org .opensearch .core .action .ActionListener ;
1822import org .opensearch .core .action .ActionResponse ;
2731import org .opensearch .test .rest .FakeRestRequest ;
2832import org .opensearch .transport .client .node .NodeClient ;
2933
30- import static org .opensearch .common . util . FeatureFlags . STREAM_SEARCH ;
34+ import static org .opensearch .action . search . StreamSearchTransportService . STREAM_SEARCH_ENABLED ;
3135import static org .opensearch .common .util .FeatureFlags .STREAM_TRANSPORT ;
3236import static org .hamcrest .Matchers .equalTo ;
3337
@@ -53,45 +57,67 @@ public String getLocalNodeId() {
5357 };
5458 }
5559
56- private void testActionExecution (ActionType <?> expectedAction ) throws Exception {
60+ private ClusterSettings createClusterSettingsWithStreamSearchEnabled () {
61+ Settings settings = Settings .builder ().put (STREAM_SEARCH_ENABLED .getKey (), true ).build ();
62+ return new ClusterSettings (settings , ClusterSettings .BUILT_IN_CLUSTER_SETTINGS );
63+ }
64+
65+ private SearchRequest createSearchRequestWithTermsAggregation () {
66+ SearchRequest searchRequest = new SearchRequest ();
67+ SearchSourceBuilder source = new SearchSourceBuilder ();
68+ source .aggregation (AggregationBuilders .terms ("test_terms" ).field ("category" ));
69+ searchRequest .source (source );
70+ return searchRequest ;
71+ }
72+
73+ public void testWithSearchStreamDisabled () throws Exception {
5774 SetOnce <ActionType <?>> capturedActionType = new SetOnce <>();
5875 try (NodeClient nodeClient = createMockNodeClient (capturedActionType )) {
5976 RestRequest request = new FakeRestRequest .Builder (xContentRegistry ()).build ();
6077 FakeRestChannel channel = new FakeRestChannel (request , false , 0 );
6178
6279 new RestSearchAction ().handleRequest (request , channel , nodeClient );
6380
64- assertThat (capturedActionType .get (), equalTo (expectedAction ));
81+ assertThat (capturedActionType .get (), equalTo (SearchAction . INSTANCE ));
6582 }
6683 }
6784
68- public void testWithSearchStreamFlagDisabled () throws Exception {
69- // When SEARCH_STREAM flag is disabled, always use SearchAction
70- testActionExecution (SearchAction .INSTANCE );
71- }
72-
73- @ LockFeatureFlag (STREAM_SEARCH )
74- public void testWithStreamSearchEnabledButStreamTransportDisabled () throws Exception {
75- // When SEARCH_STREAM is enabled but STREAM_TRANSPORT is disabled, should throw exception
85+ // When stream search is enabled but STREAM_TRANSPORT is disabled, should throw exception
86+ public void testWithStreamSearchEnabledButStreamTransportDisabled () {
7687 try (NodeClient nodeClient = new NoOpNodeClient (this .getTestName ())) {
77- RestRequest request = new FakeRestRequest .Builder (xContentRegistry ()).build ();
78- FakeRestChannel channel = new FakeRestChannel (request , false , 0 );
88+ RestRequest restRequest = new FakeRestRequest .Builder (xContentRegistry ()).build ();
89+ FakeRestChannel channel = new FakeRestChannel (restRequest , false , 0 );
7990
8091 Exception e = expectThrows (
8192 IllegalArgumentException .class ,
82- () -> new RestSearchAction () .handleRequest (request , channel , nodeClient )
93+ () -> new RestSearchAction (createClusterSettingsWithStreamSearchEnabled ()) .handleRequest (restRequest , channel , nodeClient )
8394 );
8495 assertThat (e .getMessage (), equalTo ("You need to enable stream transport first to use stream search." ));
8596 }
8697 }
8798
88- public void testWithStreamSearchAndTransportEnabled () throws Exception {
89- // When both SEARCH_STREAM and STREAM_TRANSPORT are enabled, should use StreamSearchAction
90- try (
91- FeatureFlags .TestUtils .FlagWriteLock searchStreamLock = new FeatureFlags .TestUtils .FlagWriteLock (STREAM_SEARCH );
92- FeatureFlags .TestUtils .FlagWriteLock streamTransportLock = new FeatureFlags .TestUtils .FlagWriteLock (STREAM_TRANSPORT )
93- ) {
94- testActionExecution (SearchAction .INSTANCE );
99+ @ LockFeatureFlag (STREAM_TRANSPORT )
100+ public void testWithStreamSearchAndTransportEnabled () {
101+ ClusterSettings clusterSettings = createClusterSettingsWithStreamSearchEnabled ();
102+ SearchRequest searchRequest = createSearchRequestWithTermsAggregation ();
103+
104+ SetOnce <ActionType <?>> capturedActionType = new SetOnce <>();
105+ try (NodeClient nodeClient = createMockNodeClient (capturedActionType )) {
106+ // Verify all conditions are met for stream search
107+ assertTrue (clusterSettings .get (STREAM_SEARCH_ENABLED ));
108+ assertTrue (FeatureFlags .isEnabled (FeatureFlags .STREAM_TRANSPORT ));
109+ assertTrue (RestSearchAction .canUseStreamSearch (searchRequest ));
110+
111+ // Execute the StreamSearchAction directly since we've verified the conditions
112+ nodeClient .executeLocally (StreamSearchAction .INSTANCE , searchRequest , new ActionListener <>() {
113+ @ Override
114+ public void onResponse (SearchResponse response ) {}
115+
116+ @ Override
117+ public void onFailure (Exception e ) {}
118+ });
119+
120+ assertThat (capturedActionType .get (), equalTo (StreamSearchAction .INSTANCE ));
95121 }
96122 }
97123
0 commit comments