From 76a92b49a8b64277a2cf9ae5d053921e3835b8fd Mon Sep 17 00:00:00 2001 From: Dimitris Athanasiou Date: Mon, 10 Jun 2019 19:50:19 +0300 Subject: [PATCH] [ML] Get resources action should be lenient when sort field is unmapped (#42991) (#43046) Get resources action sorts on the resource id. When there are no resources at all, then it is possible the index does not contain a mapping for the resource id field. In that case, the search api fails by default. This commit adjusts the search request to ignore unmapped fields. Closes elastic/kibana#37870 --- .../AbstractTransportGetResourcesAction.java | 6 +++++- x-pack/plugin/ml/qa/ml-with-security/build.gradle | 1 + .../rest-api-spec/test/ml/filter_crud.yml | 15 +++++++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/AbstractTransportGetResourcesAction.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/AbstractTransportGetResourcesAction.java index 1aaf14013ed..56d12a1f9e0 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/AbstractTransportGetResourcesAction.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/action/AbstractTransportGetResourcesAction.java @@ -30,6 +30,7 @@ import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.builder.SearchSourceBuilder; +import org.elasticsearch.search.sort.SortBuilders; import org.elasticsearch.transport.TransportService; import org.elasticsearch.xpack.core.action.util.ExpandedIdsMatcher; import org.elasticsearch.xpack.core.action.util.QueryPage; @@ -70,7 +71,10 @@ public abstract class AbstractTransportGetResourcesAction> listener) { String[] tokens = Strings.tokenizeToStringArray(request.getResourceId(), ","); SearchSourceBuilder sourceBuilder = new SearchSourceBuilder() - .sort(request.getResourceIdField()) + .sort(SortBuilders.fieldSort(request.getResourceIdField()) + // If there are no resources, there might be no mapping for the id field. + // This makes sure we don't get an error if that happens. + .unmappedType("long")) .query(buildQuery(tokens, request.getResourceIdField())); if (request.getPageParams() != null) { sourceBuilder.from(request.getPageParams().getFrom()) diff --git a/x-pack/plugin/ml/qa/ml-with-security/build.gradle b/x-pack/plugin/ml/qa/ml-with-security/build.gradle index 8d7f799d5a2..8f74b859247 100644 --- a/x-pack/plugin/ml/qa/ml-with-security/build.gradle +++ b/x-pack/plugin/ml/qa/ml-with-security/build.gradle @@ -44,6 +44,7 @@ integTestRunner { 'ml/filter_crud/Test invalid param combinations', 'ml/filter_crud/Test non-existing filter', 'ml/filter_crud/Test update filter given remove item is not present', + 'ml/filter_crud/Test get all filter given index exists but no mapping for filter_id', 'ml/get_datafeed_stats/Test get datafeed stats given missing datafeed_id', 'ml/get_datafeeds/Test get datafeed given missing datafeed_id', 'ml/jobs_crud/Test cannot create job with existing categorizer state document', diff --git a/x-pack/plugin/src/test/resources/rest-api-spec/test/ml/filter_crud.yml b/x-pack/plugin/src/test/resources/rest-api-spec/test/ml/filter_crud.yml index 1465f1cf0b4..884bc804377 100644 --- a/x-pack/plugin/src/test/resources/rest-api-spec/test/ml/filter_crud.yml +++ b/x-pack/plugin/src/test/resources/rest-api-spec/test/ml/filter_crud.yml @@ -328,3 +328,18 @@ setup: ml.get_filters: {} - match: { count: 0 } - match: { filters: [] } + +--- +"Test get all filter given index exists but no mapping for filter_id": + + - do: + indices.delete: + index: ".ml-meta" + - do: + indices.create: + index: ".ml-meta" + + - do: + ml.get_filters: {} + - match: { count: 0 } + - match: { filters: [] }