Skip to content

Commit f810d75

Browse files
committed
Add an integ test for aggregations in search
Signed-off-by: Daniel Widdis <[email protected]>
1 parent 560f479 commit f810d75

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

plugin/src/test/java/org/opensearch/ml/rest/MLCommonsTenantAwareRestTestCase.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,18 @@ public abstract class MLCommonsTenantAwareRestTestCase extends MLCommonsRestTest
5757

5858
// REST body
5959
protected static final String MATCH_ALL_QUERY = "{\"query\":{\"match_all\":{}}}";
60+
protected static final String AGGREGATION_QUERY = "{\n"
61+
+ " \"size\": 0,\n"
62+
+ " \"aggs\": {\n"
63+
+ " \"unique_model_names\": {\n"
64+
+ " \"terms\": {\n"
65+
+ " \"field\": \"name.keyword\",\n"
66+
+ " \"size\": 10000\n"
67+
+ " }\n"
68+
+ " }\n"
69+
+ " }\n"
70+
+ "}";
71+
6072
protected static final String EMPTY_CONTENT = "{}";
6173

6274
// REST Response error reasons
@@ -84,6 +96,7 @@ public abstract class MLCommonsTenantAwareRestTestCase extends MLCommonsRestTest
8496
protected final RestRequest tenantMatchAllRequest = getRestRequestWithHeadersAndContent(tenantId, MATCH_ALL_QUERY);
8597
protected final RestRequest otherTenantMatchAllRequest = getRestRequestWithHeadersAndContent(otherTenantId, MATCH_ALL_QUERY);
8698
protected final RestRequest nullTenantMatchAllRequest = getRestRequestWithHeadersAndContent(null, MATCH_ALL_QUERY);
99+
protected final RestRequest tenantAggregationRequest = getRestRequestWithHeadersAndContent(tenantId, AGGREGATION_QUERY);
87100

88101
protected static boolean isMultiTenancyEnabled() throws IOException {
89102
// pass -Dtests.rest.tenantaware=true on gradle command line to enable

plugin/src/test/java/org/opensearch/ml/rest/RestMLModelTenantAwareIT.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.opensearch.client.Response;
1717
import org.opensearch.client.ResponseException;
1818
import org.opensearch.core.rest.RestStatus;
19+
import org.opensearch.ml.utils.TestHelper;
1920
import org.opensearch.rest.RestRequest;
2021

2122
public class RestMLModelTenantAwareIT extends MLCommonsTenantAwareRestTestCase {
@@ -277,6 +278,25 @@ public void testModelCRUD() throws Exception {
277278
assertNull(searchResponse.getHits().getHits()[1].getSourceAsMap().get(TENANT_ID_FIELD));
278279
}
279280

281+
// Test search with aggregations to verify no error is thrown
282+
Response restResponse = makeRequest(tenantAggregationRequest, GET, MODELS_PATH + "_search");
283+
assertOK(restResponse);
284+
// We can't (easily) parse the JSON from this response into a SearchResponse for the same reason as the bug we're testing
285+
// So we test the string values instead
286+
String responseJson = TestHelper.httpEntityToString(restResponse.getEntity());
287+
assertTrue(responseJson.contains("\"aggregations\":{\"unique_model_names\""));
288+
if (multiTenancyEnabled) {
289+
assertTrue(responseJson.contains("\"value\":1,\"relation\":\"eq\""));
290+
assertTrue(responseJson.contains("\"key\":\"remote model for connector_id "));
291+
assertTrue(responseJson.contains("\"doc_count\":1"));
292+
} else {
293+
assertTrue(responseJson.contains("\"value\":3,\"relation\":\"eq\""));
294+
// one connector will have two doc_count, other will have one
295+
assertTrue(responseJson.contains("\"key\":\"remote model for connector_id "));
296+
assertTrue(responseJson.contains("\"doc_count\":2"));
297+
assertTrue(responseJson.contains("\"doc_count\":1"));
298+
}
299+
280300
/*
281301
* Delete
282302
*/

0 commit comments

Comments
 (0)