|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.opensearch.rest.action.search; |
| 10 | + |
| 11 | +import org.opensearch.action.ActionRequest; |
| 12 | +import org.opensearch.action.ActionType; |
| 13 | +import org.opensearch.action.search.SearchAction; |
| 14 | +import org.opensearch.action.search.SearchRequest; |
| 15 | +import org.opensearch.common.SetOnce; |
| 16 | +import org.opensearch.common.util.FeatureFlags; |
| 17 | +import org.opensearch.core.action.ActionListener; |
| 18 | +import org.opensearch.core.action.ActionResponse; |
| 19 | +import org.opensearch.index.query.QueryBuilders; |
| 20 | +import org.opensearch.rest.RestRequest; |
| 21 | +import org.opensearch.search.aggregations.AggregationBuilders; |
| 22 | +import org.opensearch.search.builder.SearchSourceBuilder; |
| 23 | +import org.opensearch.tasks.Task; |
| 24 | +import org.opensearch.test.OpenSearchTestCase; |
| 25 | +import org.opensearch.test.client.NoOpNodeClient; |
| 26 | +import org.opensearch.test.rest.FakeRestChannel; |
| 27 | +import org.opensearch.test.rest.FakeRestRequest; |
| 28 | +import org.opensearch.transport.client.node.NodeClient; |
| 29 | + |
| 30 | +import static org.opensearch.common.util.FeatureFlags.STREAM_SEARCH; |
| 31 | +import static org.opensearch.common.util.FeatureFlags.STREAM_TRANSPORT; |
| 32 | +import static org.hamcrest.Matchers.equalTo; |
| 33 | + |
| 34 | +public class RestSearchActionTests extends OpenSearchTestCase { |
| 35 | + |
| 36 | + private NodeClient createMockNodeClient(SetOnce<ActionType<?>> capturedActionType) { |
| 37 | + return new NoOpNodeClient(this.getTestName()) { |
| 38 | + @Override |
| 39 | + public <Request extends ActionRequest, Response extends ActionResponse> Task executeLocally( |
| 40 | + ActionType<Response> action, |
| 41 | + Request request, |
| 42 | + ActionListener<Response> listener |
| 43 | + ) { |
| 44 | + capturedActionType.set(action); |
| 45 | + listener.onResponse(null); |
| 46 | + return new Task(1L, "test", action.name(), "test task", null, null); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public String getLocalNodeId() { |
| 51 | + return "test-node"; |
| 52 | + } |
| 53 | + }; |
| 54 | + } |
| 55 | + |
| 56 | + private void testActionExecution(ActionType<?> expectedAction) throws Exception { |
| 57 | + SetOnce<ActionType<?>> capturedActionType = new SetOnce<>(); |
| 58 | + try (NodeClient nodeClient = createMockNodeClient(capturedActionType)) { |
| 59 | + RestRequest request = new FakeRestRequest.Builder(xContentRegistry()).build(); |
| 60 | + FakeRestChannel channel = new FakeRestChannel(request, false, 0); |
| 61 | + |
| 62 | + new RestSearchAction().handleRequest(request, channel, nodeClient); |
| 63 | + |
| 64 | + assertThat(capturedActionType.get(), equalTo(expectedAction)); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 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 |
| 76 | + try (NodeClient nodeClient = new NoOpNodeClient(this.getTestName())) { |
| 77 | + RestRequest request = new FakeRestRequest.Builder(xContentRegistry()).build(); |
| 78 | + FakeRestChannel channel = new FakeRestChannel(request, false, 0); |
| 79 | + |
| 80 | + Exception e = expectThrows( |
| 81 | + IllegalArgumentException.class, |
| 82 | + () -> new RestSearchAction().handleRequest(request, channel, nodeClient) |
| 83 | + ); |
| 84 | + assertThat(e.getMessage(), equalTo("You need to enable stream transport first to use stream search.")); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 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); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + // Tests for canUseStreamSearch method |
| 99 | + public void testCanUseStreamSearchWithNullSource() { |
| 100 | + SearchRequest searchRequest = new SearchRequest(); |
| 101 | + assertFalse(RestSearchAction.canUseStreamSearch(searchRequest)); |
| 102 | + } |
| 103 | + |
| 104 | + public void testCanUseStreamSearchWithNoAggregations() { |
| 105 | + SearchRequest searchRequest = new SearchRequest(); |
| 106 | + SearchSourceBuilder source = new SearchSourceBuilder(); |
| 107 | + source.query(QueryBuilders.matchAllQuery()); |
| 108 | + searchRequest.source(source); |
| 109 | + assertFalse(RestSearchAction.canUseStreamSearch(searchRequest)); |
| 110 | + } |
| 111 | + |
| 112 | + public void testCanUseStreamSearchWithSingleTermsAggregation() { |
| 113 | + SearchRequest searchRequest = new SearchRequest(); |
| 114 | + SearchSourceBuilder source = new SearchSourceBuilder(); |
| 115 | + source.aggregation(AggregationBuilders.terms("test_terms").field("category")); |
| 116 | + searchRequest.source(source); |
| 117 | + assertTrue(RestSearchAction.canUseStreamSearch(searchRequest)); |
| 118 | + } |
| 119 | + |
| 120 | + public void testCanUseStreamSearchWithMultipleAggregations() { |
| 121 | + SearchRequest searchRequest = new SearchRequest(); |
| 122 | + SearchSourceBuilder source = new SearchSourceBuilder(); |
| 123 | + source.aggregation(AggregationBuilders.terms("test_terms").field("category")); |
| 124 | + source.aggregation(AggregationBuilders.avg("test_avg").field("price")); |
| 125 | + searchRequest.source(source); |
| 126 | + assertFalse(RestSearchAction.canUseStreamSearch(searchRequest)); |
| 127 | + } |
| 128 | + |
| 129 | + public void testCanUseStreamSearchWithSingleNonTermsAggregation() { |
| 130 | + SearchRequest searchRequest = new SearchRequest(); |
| 131 | + SearchSourceBuilder source = new SearchSourceBuilder(); |
| 132 | + source.aggregation(AggregationBuilders.avg("test_avg").field("price")); |
| 133 | + searchRequest.source(source); |
| 134 | + assertFalse(RestSearchAction.canUseStreamSearch(searchRequest)); |
| 135 | + } |
| 136 | + |
| 137 | + public void testCanUseStreamSearchWithSingleHistogramAggregation() { |
| 138 | + SearchRequest searchRequest = new SearchRequest(); |
| 139 | + SearchSourceBuilder source = new SearchSourceBuilder(); |
| 140 | + source.aggregation(AggregationBuilders.histogram("test_histogram").field("timestamp").interval(1000)); |
| 141 | + searchRequest.source(source); |
| 142 | + assertFalse(RestSearchAction.canUseStreamSearch(searchRequest)); |
| 143 | + } |
| 144 | +} |
0 commit comments