[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
This commit is contained in:
Dimitris Athanasiou 2019-06-10 19:50:19 +03:00 committed by GitHub
parent 014bad1f61
commit 76a92b49a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 1 deletions

View File

@ -30,6 +30,7 @@ import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.transport.TransportService; import org.elasticsearch.transport.TransportService;
import org.elasticsearch.xpack.core.action.util.ExpandedIdsMatcher; import org.elasticsearch.xpack.core.action.util.ExpandedIdsMatcher;
import org.elasticsearch.xpack.core.action.util.QueryPage; import org.elasticsearch.xpack.core.action.util.QueryPage;
@ -70,7 +71,10 @@ public abstract class AbstractTransportGetResourcesAction<Resource extends ToXCo
protected void searchResources(AbstractGetResourcesRequest request, ActionListener<QueryPage<Resource>> listener) { protected void searchResources(AbstractGetResourcesRequest request, ActionListener<QueryPage<Resource>> listener) {
String[] tokens = Strings.tokenizeToStringArray(request.getResourceId(), ","); String[] tokens = Strings.tokenizeToStringArray(request.getResourceId(), ",");
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder() 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())); .query(buildQuery(tokens, request.getResourceIdField()));
if (request.getPageParams() != null) { if (request.getPageParams() != null) {
sourceBuilder.from(request.getPageParams().getFrom()) sourceBuilder.from(request.getPageParams().getFrom())

View File

@ -44,6 +44,7 @@ integTestRunner {
'ml/filter_crud/Test invalid param combinations', 'ml/filter_crud/Test invalid param combinations',
'ml/filter_crud/Test non-existing filter', 'ml/filter_crud/Test non-existing filter',
'ml/filter_crud/Test update filter given remove item is not present', '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_datafeed_stats/Test get datafeed stats given missing datafeed_id',
'ml/get_datafeeds/Test get datafeed 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', 'ml/jobs_crud/Test cannot create job with existing categorizer state document',

View File

@ -328,3 +328,18 @@ setup:
ml.get_filters: {} ml.get_filters: {}
- match: { count: 0 } - match: { count: 0 }
- match: { filters: [] } - 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: [] }