Skip to content

Commit

Permalink
[Ml Data Frame] Return bad_request on preview when config is invalid (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
davidkyle committed May 28, 2019
1 parent c21745c commit aea600f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.elasticsearch.xpack.core.dataframe.transforms.DataFrameIndexerTransformStats;
import org.elasticsearch.xpack.core.dataframe.transforms.DataFrameTransformConfig;
import org.elasticsearch.xpack.core.dataframe.transforms.SourceConfig;
import org.elasticsearch.xpack.dataframe.transforms.pivot.AggregationResultUtils;
import org.elasticsearch.xpack.dataframe.transforms.pivot.Pivot;

import java.util.List;
Expand Down Expand Up @@ -102,14 +103,21 @@ private void getPreview(Pivot pivot, SourceConfig source, ActionListener<List<Ma
pivot.buildSearchRequest(source, null, NUMBER_OF_PREVIEW_BUCKETS),
ActionListener.wrap(
r -> {
final CompositeAggregation agg = r.getAggregations().get(COMPOSITE_AGGREGATION_NAME);
DataFrameIndexerTransformStats stats = DataFrameIndexerTransformStats.withDefaultTransformId();
// remove all internal fields
List<Map<String, Object>> results = pivot.extractResults(agg, deducedMappings, stats)
.peek(record -> {
record.keySet().removeIf(k -> k.startsWith("_"));
}).collect(Collectors.toList());
listener.onResponse(results);

try {
final CompositeAggregation agg = r.getAggregations().get(COMPOSITE_AGGREGATION_NAME);
DataFrameIndexerTransformStats stats = DataFrameIndexerTransformStats.withDefaultTransformId();
// remove all internal fields
List<Map<String, Object>> results = pivot.extractResults(agg, deducedMappings, stats)
.peek(record -> {
record.keySet().removeIf(k -> k.startsWith("_"));
}).collect(Collectors.toList());

listener.onResponse(results);
} catch (AggregationResultUtils.AggregationExtractionException extractionException) {
listener.onFailure(
new ElasticsearchStatusException(extractionException.getMessage(), RestStatus.BAD_REQUEST));
}
},
listener::onFailure
));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,38 @@ setup:
"aggs": {"avg_response": {"avg": {"field": "responsetime"}}}
}
}
---
"Test preview returns bad request with invalid agg":
- do:
catch: bad_request
data_frame.preview_data_frame_transform:
body: >
{
"source": { "index": "airline-data" },
"pivot": {
"group_by": {
"time": {"date_histogram": {"fixed_interval": "1h", "field": "time", "format": "yyyy-MM-DD HH"}}},
"aggs": {
"avg_response": {"avg": {"field": "responsetime"}},
"time.min": {"min": {"field": "time"}}
}
}
}
- do:
catch: /mixed object types of nested and non-nested fields \[time.min\]/
data_frame.preview_data_frame_transform:
body: >
{
"source": { "index": "airline-data" },
"pivot": {
"group_by": {
"time": {"date_histogram": {"fixed_interval": "1h", "field": "time", "format": "yyyy-MM-DD HH"}}},
"aggs": {
"avg_response": {"avg": {"field": "responsetime"}},
"time.min": {"min": {"field": "time"}}
}
}
}

0 comments on commit aea600f

Please sign in to comment.