Browse Source

Search Applications: remove indices array and rely on alias (#98036)

* Stop returning the indices list for GET search app

* Stop returning the indices list for the list search app API

* Stop storing indices list

* Remove indices from system index mapping

* Check for alias in PUT rest tests

* Documentation changes

* Do not check for alias existence since we are already doing get alias
Ioana Tagirta 2 years ago
parent
commit
f3d0073066

+ 0 - 1
docs/reference/search-application/apis/get-search-application.asciidoc

@@ -55,7 +55,6 @@ A sample response:
 ----
 {
     "name": "my-app",
-    "indices": [ "index1", "index2" ],
     "updated_at_millis": 1682105622204,
     "template": {
       "script": {

+ 2 - 4
docs/reference/search-application/apis/list-search-applications.asciidoc

@@ -62,12 +62,10 @@ A sample response:
   "count": 2,
   "results": [
     {
-      "name": "app-1",
-      "indices": [ "index1", "index2" ]
+      "name": "app-1"
     },
     {
-      "name": "app-2",
-      "indices": [ "index3", "index4" ]
+      "name": "app-2"
     }
   ],
   "updated_at_millis": 1682105622204

+ 17 - 8
x-pack/plugin/ent-search/qa/rest/src/yamlRestTest/resources/rest-api-spec/test/entsearch/20_search_application_put.yml

@@ -116,15 +116,15 @@ teardown:
   - match: { result: "created" }
 
   - do:
-      indices.exists_alias:
-        name: test-search-application
+      indices.get_alias:
+        index: test-search-application
 
-  - is_true: ''
+  - match: { test-index1.aliases.test-search-application: {} }
+  - match: { test-index2.aliases.test-search-application: {} }
 
   - do:
       search_application.get:
         name: test-search-application
-  - match: { indices: [ "test-index1", "test-index2" ] }
   - match:
       template:
         script:
@@ -159,6 +159,12 @@ teardown:
               properties:
                 another_query_string:
                   type: "string"
+  - do:
+      indices.get_alias:
+        index: another-test-search-application
+
+  - match: { test-index1.aliases.another-test-search-application: {} }
+  - match: { test-index2.aliases.another-test-search-application: {} }
 
   - do:
       search_application.get:
@@ -176,8 +182,6 @@ teardown:
             another_query_string:
               type: "string"
 
-  - match: { indices: [ "test-index1", "test-index2" ] }
-
 ---
 "Update Search Application with null template, results in default template being applied":
   - do:
@@ -201,6 +205,13 @@ teardown:
 
   - match: { result: "updated" }
 
+  - do:
+      indices.get_alias:
+        index: test-updated-search-application
+
+  - match: { test-index3.aliases.test-updated-search-application: { } }
+  - match: { test-index4.aliases.test-updated-search-application: { } }
+
   - do:
       search_application.get:
         name: test-updated-search-application
@@ -214,8 +225,6 @@ teardown:
             query_string: "*"
             default_field: "*"
 
-  - match: { indices: [ "test-index3", "test-index4" ] }
-
 ---
 "Update Search Application - Dictionary is not a valid JSON Schema":
   - do:

+ 0 - 3
x-pack/plugin/ent-search/qa/rest/src/yamlRestTest/resources/rest-api-spec/test/entsearch/30_search_application_get.yml

@@ -58,7 +58,6 @@ teardown:
         name: test-search-application-1
 
   - match: { name: "test-search-application-1" }
-  - match: { indices: [ "test-index1", "test-index2" ] }
   - match: { analytics_collection_name: "test-analytics" }
   - match: {
     template: {
@@ -91,7 +90,6 @@ teardown:
         name: test-search-application-2
 
   - match: { name: "test-search-application-2" }
-  - match: { indices: [ "test-index1", "test-index2" ] }
   - match: { analytics_collection_name: "test-analytics" }
   - match: {
     template: {
@@ -155,7 +153,6 @@ teardown:
         name: test-search-application-1
 
   - match: { name: "test-search-application-1" }
-  - match: { indices: [ "test-index1", "test-index2" ] }
   - match: { analytics_collection_name: "test-analytics" }
   - match: {
     template: {

+ 0 - 9
x-pack/plugin/ent-search/qa/rest/src/yamlRestTest/resources/rest-api-spec/test/entsearch/50_search_application_list.yml

@@ -67,19 +67,16 @@ teardown:
 
   # Alphabetical order for results
   - match: { results.0.name: "another-test-search-application" }
-  - match: { results.0.indices: [ "test-index1", "test-index2" ] }
   - match: { results.0.analytics_collection_name: "test-another-analytics" }
   - match: { results.0.updated_at_millis: $updatedAtMillis0 }
   - gte: { results.0.updated_at_millis: 0 }
 
   - match: { results.1.name: "test-search-application-1" }
-  - match: { results.1.indices: [ "test-index1" ] }
   - match: { results.1.analytics_collection_name: "test-analytics1" }
   - match: { results.1.updated_at_millis: $updatedAtMillis1 }
   - gte: { results.1.updated_at_millis: 0 }
 
   - match: { results.2.name: "test-search-application-2" }
-  - match: { results.2.indices: [ "test-index2" ] }
   - match: { results.2.analytics_collection_name: "test-analytics2" }
   - match: { results.2.updated_at_millis: $updatedAtMillis2 }
   - gte: { results.2.updated_at_millis: 0 }
@@ -95,13 +92,11 @@ teardown:
   - match: { count: 3 }
 
   - match: { results.0.name: "test-search-application-1" }
-  - match: { results.0.indices: [ "test-index1" ] }
   - match: { results.0.analytics_collection_name: "test-analytics1" }
   - match: { results.0.updated_at_millis: $updatedAtMillis0 }
   - gte: { results.0.updated_at_millis: 0 }
 
   - match: { results.1.name: "test-search-application-2" }
-  - match: { results.1.indices: [ "test-index2" ] }
   - match: { results.1.analytics_collection_name: "test-analytics2" }
   - match: { results.1.updated_at_millis: $updatedAtMillis1 }
   - gte: { results.1.updated_at_millis: 0 }
@@ -117,13 +112,11 @@ teardown:
   - match: { count: 3 }
 
   - match: { results.0.name: "another-test-search-application" }
-  - match: { results.0.indices: [ "test-index1", "test-index2" ] }
   - match: { results.0.analytics_collection_name: "test-another-analytics" }
   - match: { results.0.updated_at_millis: $updatedAtMillis0 }
   - gte: { results.0.updated_at_millis: 0 }
 
   - match: { results.1.name: "test-search-application-1" }
-  - match: { results.1.indices: [ "test-index1" ] }
   - match: { results.1.analytics_collection_name: "test-analytics1" }
   - match: { results.1.updated_at_millis: $updatedAtMillis1 }
   - gte: { results.1.updated_at_millis: 0 }
@@ -140,13 +133,11 @@ teardown:
   - match: { count: 2 }
 
   - match: { results.0.name: "test-search-application-1" }
-  - match: { results.0.indices: [ "test-index1" ] }
   - match: { results.0.analytics_collection_name: "test-analytics1" }
   - match: { results.0.updated_at_millis: $updatedAtMillis0 }
   - gte: { results.0.updated_at_millis: 0 }
 
   - match: { results.1.name: "test-search-application-2" }
-  - match: { results.1.indices: [ "test-index2" ] }
   - match: { results.1.analytics_collection_name: "test-analytics2" }
   - match: { results.1.updated_at_millis: $updatedAtMillis1 }
   - gte: { results.1.updated_at_millis: 0 }

+ 3 - 8
x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/search/SearchApplication.java

@@ -35,7 +35,6 @@ import java.util.List;
 import java.util.Map;
 import java.util.Objects;
 
-import static org.elasticsearch.xcontent.ConstructingObjectParser.constructorArg;
 import static org.elasticsearch.xcontent.ConstructingObjectParser.optionalConstructorArg;
 
 /**
@@ -119,7 +118,7 @@ public class SearchApplication implements Writeable, ToXContentObject {
                 }
             }
             @SuppressWarnings("unchecked")
-            final String[] indices = ((List<String>) params[1]).toArray(String[]::new);
+            final String[] indices = (params[1] != null) ? ((List<String>) params[1]).toArray(String[]::new) : new String[0];
             final String analyticsCollectionName = (String) params[2];
             final Long maybeUpdatedAtMillis = (Long) params[3];
             long updatedAtMillis = (maybeUpdatedAtMillis != null ? maybeUpdatedAtMillis : System.currentTimeMillis());
@@ -139,7 +138,7 @@ public class SearchApplication implements Writeable, ToXContentObject {
 
     static {
         PARSER.declareStringOrNull(optionalConstructorArg(), NAME_FIELD);
-        PARSER.declareStringArray(constructorArg(), INDICES_FIELD);
+        PARSER.declareStringArray(optionalConstructorArg(), INDICES_FIELD);
         PARSER.declareStringOrNull(optionalConstructorArg(), ANALYTICS_COLLECTION_NAME_FIELD);
         PARSER.declareLong(optionalConstructorArg(), UPDATED_AT_MILLIS_FIELD);
         PARSER.declareObjectOrNull(optionalConstructorArg(), (p, c) -> SearchApplicationTemplate.parse(p), null, TEMPLATE_FIELD);
@@ -183,7 +182,6 @@ public class SearchApplication implements Writeable, ToXContentObject {
     public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
         builder.startObject();
         builder.field(NAME_FIELD.getPreferredName(), name);
-        builder.field(INDICES_FIELD.getPreferredName(), indices);
         if (analyticsCollectionName != null) {
             builder.field(ANALYTICS_COLLECTION_NAME_FIELD.getPreferredName(), analyticsCollectionName);
         }
@@ -239,7 +237,6 @@ public class SearchApplication implements Writeable, ToXContentObject {
         if (o == null || getClass() != o.getClass()) return false;
         SearchApplication app = (SearchApplication) o;
         return name.equals(app.name)
-            && Arrays.equals(indices, app.indices)
             && Objects.equals(analyticsCollectionName, app.analyticsCollectionName)
             && updatedAtMillis == app.updatedAtMillis()
             && Objects.equals(searchApplicationTemplate, app.searchApplicationTemplate);
@@ -247,9 +244,7 @@ public class SearchApplication implements Writeable, ToXContentObject {
 
     @Override
     public int hashCode() {
-        int result = Objects.hash(name, analyticsCollectionName, updatedAtMillis, searchApplicationTemplate);
-        result = 31 * result + Arrays.hashCode(indices);
-        return result;
+        return Objects.hash(name, analyticsCollectionName, updatedAtMillis, searchApplicationTemplate);
     }
 
     @Override

+ 0 - 7
x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/search/SearchApplicationIndexService.java

@@ -164,10 +164,6 @@ public class SearchApplicationIndexService {
                     builder.field("type", "keyword");
                     builder.endObject();
 
-                    builder.startObject(SearchApplication.INDICES_FIELD.getPreferredName());
-                    builder.field("type", "keyword");
-                    builder.endObject();
-
                     builder.startObject(SearchApplication.ANALYTICS_COLLECTION_NAME_FIELD.getPreferredName());
                     builder.field("type", "keyword");
                     builder.endObject();
@@ -320,7 +316,6 @@ public class SearchApplicationIndexService {
             try (XContentBuilder source = XContentFactory.jsonBuilder(buffer)) {
                 source.startObject()
                     .field(SearchApplication.NAME_FIELD.getPreferredName(), app.name())
-                    .field(SearchApplication.INDICES_FIELD.getPreferredName(), app.indices())
                     .field(SearchApplication.ANALYTICS_COLLECTION_NAME_FIELD.getPreferredName(), app.analyticsCollectionName())
                     .field(SearchApplication.UPDATED_AT_MILLIS_FIELD.getPreferredName(), app.updatedAtMillis())
                     .directFieldAsBase64(
@@ -421,7 +416,6 @@ public class SearchApplicationIndexService {
                 .size(size)
                 .query(new QueryStringQueryBuilder(queryString))
                 .docValueField(SearchApplication.NAME_FIELD.getPreferredName())
-                .docValueField(SearchApplication.INDICES_FIELD.getPreferredName())
                 .docValueField(SearchApplication.ANALYTICS_COLLECTION_NAME_FIELD.getPreferredName())
                 .docValueField(SearchApplication.UPDATED_AT_MILLIS_FIELD.getPreferredName())
                 .storedFields(Collections.singletonList("_none_"))
@@ -459,7 +453,6 @@ public class SearchApplicationIndexService {
         final String resourceName = documentFields.get(SearchApplication.NAME_FIELD.getPreferredName()).getValue();
         return new SearchApplicationListItem(
             resourceName,
-            documentFields.get(SearchApplication.INDICES_FIELD.getPreferredName()).getValues().toArray(String[]::new),
             documentFields.get(SearchApplication.ANALYTICS_COLLECTION_NAME_FIELD.getPreferredName()).getValue(),
             documentFields.get(SearchApplication.UPDATED_AT_MILLIS_FIELD.getPreferredName()).getValue()
         );

+ 5 - 31
x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/search/SearchApplicationListItem.java

@@ -18,11 +18,8 @@ import org.elasticsearch.xcontent.XContentBuilder;
 import org.elasticsearch.xcontent.XContentParser;
 
 import java.io.IOException;
-import java.util.Arrays;
-import java.util.List;
 import java.util.Objects;
 
-import static org.elasticsearch.xcontent.ConstructingObjectParser.constructorArg;
 import static org.elasticsearch.xcontent.ConstructingObjectParser.optionalConstructorArg;
 
 /**
@@ -32,12 +29,10 @@ import static org.elasticsearch.xcontent.ConstructingObjectParser.optionalConstr
 public class SearchApplicationListItem implements Writeable, ToXContentObject {
 
     public static final ParseField NAME_FIELD = new ParseField("name");
-    public static final ParseField INDICES_FIELD = new ParseField("indices");
     public static final ParseField ANALYTICS_COLLECTION_NAME_FIELD = new ParseField("analytics_collection_name");
 
     public static final ParseField UPDATED_AT_MILLIS_FIELD = new ParseField("updated_at_millis");
     private final String name;
-    private final String[] indices;
     private final String analyticsCollectionName;
 
     private final long updatedAtMillis;
@@ -45,25 +40,20 @@ public class SearchApplicationListItem implements Writeable, ToXContentObject {
     /**
      * Constructs a SearchApplicationListItem.
      *
-     * @param name The name of the search application
-     * @param indices The indices associated with the search application
+     * @param name                    The name of the search application
      * @param analyticsCollectionName The analytics collection associated with this application if one exists
-     * @param updatedAtMillis The timestamp in milliseconds when this search application was last updated.
+     * @param updatedAtMillis         The timestamp in milliseconds when this search application was last updated.
      */
-    public SearchApplicationListItem(String name, String[] indices, @Nullable String analyticsCollectionName, long updatedAtMillis) {
+    public SearchApplicationListItem(String name, @Nullable String analyticsCollectionName, long updatedAtMillis) {
         Objects.requireNonNull(name, "Name cannot be null on a SearchApplicationListItem");
         this.name = name;
 
-        Objects.requireNonNull(name, "Indices cannot be null on a SearchApplicationListItem");
-        this.indices = indices;
-
         this.analyticsCollectionName = analyticsCollectionName;
         this.updatedAtMillis = updatedAtMillis;
     }
 
     public SearchApplicationListItem(StreamInput in) throws IOException {
         this.name = in.readString();
-        this.indices = in.readStringArray();
         this.analyticsCollectionName = in.readOptionalString();
         this.updatedAtMillis = in.readLong();
     }
@@ -72,7 +62,6 @@ public class SearchApplicationListItem implements Writeable, ToXContentObject {
     public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
         builder.startObject();
         builder.field(NAME_FIELD.getPreferredName(), name);
-        builder.field(INDICES_FIELD.getPreferredName(), indices);
         if (analyticsCollectionName != null) {
             builder.field(ANALYTICS_COLLECTION_NAME_FIELD.getPreferredName(), analyticsCollectionName);
         }
@@ -87,16 +76,14 @@ public class SearchApplicationListItem implements Writeable, ToXContentObject {
         (params) -> {
             final String name = (String) params[0];
             @SuppressWarnings("unchecked")
-            final String[] indices = ((List<String>) params[1]).toArray(String[]::new);
             final String analyticsCollectionName = (String) params[2];
             final Long updatedAtMillis = (Long) params[3];
-            return new SearchApplicationListItem(name, indices, analyticsCollectionName, updatedAtMillis);
+            return new SearchApplicationListItem(name, analyticsCollectionName, updatedAtMillis);
         }
     );
 
     static {
         PARSER.declareStringOrNull(optionalConstructorArg(), NAME_FIELD);
-        PARSER.declareStringArray(constructorArg(), INDICES_FIELD);
         PARSER.declareStringOrNull(optionalConstructorArg(), ANALYTICS_COLLECTION_NAME_FIELD);
         PARSER.declareLong(optionalConstructorArg(), UPDATED_AT_MILLIS_FIELD);
     }
@@ -108,7 +95,6 @@ public class SearchApplicationListItem implements Writeable, ToXContentObject {
     @Override
     public void writeTo(StreamOutput out) throws IOException {
         out.writeString(name);
-        out.writeStringArray(indices);
         out.writeOptionalString(analyticsCollectionName);
         out.writeLong(updatedAtMillis);
     }
@@ -122,15 +108,6 @@ public class SearchApplicationListItem implements Writeable, ToXContentObject {
         return name;
     }
 
-    /**
-     * Returns the indices associated with the {@link SearchApplicationListItem}.
-     *
-     * @return the indices.
-     */
-    public String[] indices() {
-        return indices;
-    }
-
     /**
      * Returns the analytics collection associated with the {@link SearchApplicationListItem} if one exists.
      *
@@ -155,15 +132,12 @@ public class SearchApplicationListItem implements Writeable, ToXContentObject {
         if (o == null || getClass() != o.getClass()) return false;
         SearchApplicationListItem item = (SearchApplicationListItem) o;
         return name.equals(item.name)
-            && Arrays.equals(indices, item.indices)
             && Objects.equals(analyticsCollectionName, item.analyticsCollectionName)
             && updatedAtMillis == item.updatedAtMillis;
     }
 
     @Override
     public int hashCode() {
-        int result = Objects.hash(name, analyticsCollectionName, updatedAtMillis);
-        result = 31 * result + Arrays.hashCode(indices);
-        return result;
+        return Objects.hash(name, analyticsCollectionName, updatedAtMillis);
     }
 }

+ 1 - 13
x-pack/plugin/ent-search/src/test/java/org/elasticsearch/xpack/application/search/SearchApplicationIndexServiceTests.java

@@ -158,7 +158,6 @@ public class SearchApplicationIndexServiceTests extends ESSingleNodeTestCase {
             for (int i = 0; i < NUM_INDICES; i++) {
                 SearchApplicationListItem app = apps.get(i);
                 assertThat(app.name(), equalTo("my_search_app_" + i));
-                assertThat(app.indices(), equalTo(new String[] { "index_" + i }));
             }
         }
 
@@ -173,7 +172,6 @@ public class SearchApplicationIndexServiceTests extends ESSingleNodeTestCase {
                 int index = i + 5;
                 SearchApplicationListItem app = apps.get(i);
                 assertThat(app.name(), equalTo("my_search_app_" + index));
-                assertThat(app.indices(), equalTo(new String[] { "index_" + index }));
             }
         }
     }
@@ -193,22 +191,13 @@ public class SearchApplicationIndexServiceTests extends ESSingleNodeTestCase {
         }
 
         {
-            for (String queryString : new String[] {
-                "*my_search_app_4*",
-                "name:my_search_app_4",
-                "my_search_app_4",
-                "*_4",
-                "indices:index_4",
-                "index_4",
-                "*_4" }) {
-
+            for (String queryString : new String[] { "*my_search_app_4*", "name:my_search_app_4", "my_search_app_4", "*_4", "*_4" }) {
                 SearchApplicationIndexService.SearchApplicationResult searchResponse = awaitListSearchApplication(queryString, 0, 10);
                 final List<SearchApplicationListItem> apps = searchResponse.items();
                 assertNotNull(apps);
                 assertThat(apps.size(), equalTo(1));
                 assertThat(searchResponse.totalResults(), equalTo(1L));
                 assertThat(apps.get(0).name(), equalTo("my_search_app_4"));
-                assertThat(apps.get(0).indices(), equalTo(new String[] { "index_4" }));
             }
         }
     }
@@ -246,7 +235,6 @@ public class SearchApplicationIndexServiceTests extends ESSingleNodeTestCase {
             for (int i = 0; i < 4; i++) {
                 SearchApplicationListItem app = apps.get(i);
                 assertThat(app.name(), equalTo("my_search_app_" + i));
-                assertThat(app.indices(), equalTo(new String[] { "index_" + i }));
             }
         }
     }

+ 1 - 1
x-pack/plugin/ent-search/src/test/java/org/elasticsearch/xpack/application/search/action/ListSearchApplicationActionResponseBWCSerializingTests.java

@@ -25,7 +25,7 @@ public class ListSearchApplicationActionResponseBWCSerializingTests extends Abst
     private static ListSearchApplicationAction.Response randomSearchApplicationListItem() {
         return new ListSearchApplicationAction.Response(randomList(10, () -> {
             SearchApplication app = SearchApplicationTestUtils.randomSearchApplication();
-            return new SearchApplicationListItem(app.name(), app.indices(), app.analyticsCollectionName(), app.updatedAtMillis());
+            return new SearchApplicationListItem(app.name(), app.analyticsCollectionName(), app.updatedAtMillis());
         }), randomLongBetween(0, 1000));
     }