diff --git a/public/app/plugins/datasource/elasticsearch/partials/query.editor.html b/public/app/plugins/datasource/elasticsearch/partials/query.editor.html
index 29e66c477c5..99051583910 100644
--- a/public/app/plugins/datasource/elasticsearch/partials/query.editor.html
+++ b/public/app/plugins/datasource/elasticsearch/partials/query.editor.html
@@ -65,7 +65,7 @@
Select
-
+
diff --git a/public/app/plugins/datasource/elasticsearch/queryBuilder.js b/public/app/plugins/datasource/elasticsearch/queryBuilder.js
index 9f6c76268bc..5b9bf4c8371 100644
--- a/public/app/plugins/datasource/elasticsearch/queryBuilder.js
+++ b/public/app/plugins/datasource/elasticsearch/queryBuilder.js
@@ -72,20 +72,6 @@ function () {
return query;
};
- ElasticQueryBuilder.prototype._buildRangeFilter = function(target) {
- var filter = {"range":{}};
- filter["range"][target.timestampField] = {
- "gte": "$rangeFrom",
- "lte": "$rangeTo"
- };
- return filter;
- };
-
- ElasticQueryBuilder.prototype._buildTermFilter = function(target) {
- var filter = {"term":{}};
- filter["term"][target.termKey] = target.termValue;
- return filter;
- };
-
return ElasticQueryBuilder;
+
});
diff --git a/public/app/plugins/datasource/elasticsearch/queryCtrl.js b/public/app/plugins/datasource/elasticsearch/queryCtrl.js
index f2c3b1f0fbc..9a4b7b2a292 100644
--- a/public/app/plugins/datasource/elasticsearch/queryCtrl.js
+++ b/public/app/plugins/datasource/elasticsearch/queryCtrl.js
@@ -22,7 +22,7 @@ function (angular, _, ElasticQueryBuilder) {
var target = $scope.target;
target.function = target.function || 'mean';
target.timeField = target.timeField || '@timestamp';
- target.select = target.select || [{ agg: 'count' }];
+ target.select = target.select || [{ agg: 'Count' }];
target.groupByFields = target.groupByFields || [];
$scope.timeSegment = uiSegmentSrv.newSegment(target.timeField);
@@ -31,32 +31,95 @@ function (angular, _, ElasticQueryBuilder) {
return uiSegmentSrv.newSegment(group.field);
});
- $scope.selectSegments = _.map(target.select, function(select) {
- return uiSegmentSrv.newSegment(select.agg);
+ $scope.selectSegments = [];
+ _.each(target.select, function(select) {
+ if ($scope.selectSegments.length > 0) {
+ $scope.selectSegments.push(uiSegmentSrv.newCondition(" and "));
+ }
+ if (select.agg === 'Count') {
+ $scope.selectSegments.push(uiSegmentSrv.newSegment({value: select.agg, type: 'agg'}));
+ } else {
+ $scope.selectSegments.push(uiSegmentSrv.newSegment({value: select.agg, type: 'agg'}));
+ $scope.selectSegments.push(uiSegmentSrv.newSegment({value: select.field, type: 'field' }));
+ }
});
$scope.groupBySegments.push(uiSegmentSrv.newPlusButton());
+ $scope.selectSegments.push(uiSegmentSrv.newPlusButton());
$scope.removeSelectSegment = uiSegmentSrv.newSegment({fake: true, value: '-- remove select --'});
$scope.removeGroupBySegment = uiSegmentSrv.newSegment({fake: true, value: '-- remove group by --'});
};
- $scope.getFields = function() {
- return $scope.datasource.metricFindQuery('fields()').then($scope.transformToSegments(true));
+ $scope.getSelectSegments = function(segment, index) {
+ if (segment.type === 'agg' || segment.type === 'plus-button') {
+ var options = [
+ uiSegmentSrv.newSegment({value: 'Count', type: 'agg'}),
+ uiSegmentSrv.newSegment({value: 'Min', type: 'agg'}),
+ uiSegmentSrv.newSegment({value: 'Max', type: 'agg'}),
+ uiSegmentSrv.newSegment({value: 'Avg', type: 'agg'}),
+ ];
+ if (index > 0) {
+ options.splice(0, 0, angular.copy($scope.removeSelectSegment));
+ }
+ return $q.when(options);
+ }
+
+ return $scope.datasource.metricFindQuery('fields()')
+ .then($scope.transformToSegments(false))
+ .then(null, $scope.handleQueryError);
};
- $scope.transformToSegments = function(addTemplateVars) {
- return function(results) {
- var segments = _.map(results, function(segment) {
- return uiSegmentSrv.newSegment({ value: segment.text, expandable: segment.expandable });
- });
+ $scope.selectChanged = function(segment, index) {
+ if (segment.value === $scope.removeSelectSegment.value) {
+ var nextSegment = $scope.selectSegments[index + 1];
+ var remove = 2;
+ if (nextSegment && nextSegment.type === 'field') {
+ remove += 1;
+ }
+ $scope.selectSegments.splice(index-1, remove);
+ $scope.rebuildTargetSelects();
+ return;
+ }
- if (addTemplateVars) {
- _.each(templateSrv.variables, function(variable) {
- segments.unshift(uiSegmentSrv.newSegment({ type: 'template', value: '$' + variable.name, expandable: true }));
- });
+ if (segment.type === 'plus-button' && index > 0) {
+ $scope.selectSegments.splice(index, 0, uiSegmentSrv.newCondition(' And '));
+ segment.type = 'agg';
+ index += 1;
+ }
+
+ if (segment.type === 'agg') {
+ var nextSegment = $scope.selectSegments[index + 1];
+
+ if (segment.value === 'Count') {
+ if (nextSegment && nextSegment.type === 'field') {
+ $scope.selectSegments.splice(index + 1, 1);
+ }
+ } else if (!nextSegment || nextSegment.type !== 'field') {
+ $scope.selectSegments.splice(index + 1, 0, uiSegmentSrv.newSegment({value: 'select field', fake: true, type: 'field'}));
+ }
+ }
+
+ if ((index+1) === $scope.selectSegments.length) {
+ $scope.selectSegments.push(uiSegmentSrv.newPlusButton());
+ }
+
+ $scope.rebuildTargetSelects();
+ };
+
+ $scope.rebuildTargetSelects = function() {
+ $scope.target.select = [];
+ for (var i = 0; i < $scope.selectSegments.length; i++) {
+ var segment = $scope.selectSegments[i];
+ var select = {agg: segment.value };
+
+ if (segment.type === 'agg' && segment.value !== 'Count') {
+ select.field = $scope.selectSegments[i+1].value;
+ i += 2;
+ } else {
+ i += 1;
}
- return segments;
+ $scope.target.select.push(select);
};
};
@@ -90,34 +153,20 @@ function (angular, _, ElasticQueryBuilder) {
$scope.$parent.get_data();
};
- $scope.valueFieldChanged = function() {
- $scope.target.valueField = $scope.valueFieldSegment.value;
- $scope.$parent.get_data();
- };
+ $scope.transformToSegments = function(addTemplateVars) {
+ return function(results) {
+ var segments = _.map(results, function(segment) {
+ return uiSegmentSrv.newSegment({ value: segment.text, expandable: segment.expandable });
+ });
- $scope.keyFieldChanged = function() {
- $scope.target.keyField = $scope.keyFieldSegment.value;
- $scope.$parent.get_data();
- };
+ if (addTemplateVars) {
+ _.each(templateSrv.variables, function(variable) {
+ segments.unshift(uiSegmentSrv.newSegment({ type: 'template', value: '$' + variable.name, expandable: true }));
+ });
+ }
- $scope.termValueSegmentChanged = function() {
- $scope.target.termValue = $scope.termValueSegment.value;
- $scope.$parent.get_data();
- };
-
- $scope.termKeySegmentChanged = function() {
- $scope.target.termKey = $scope.termKeySegment.value;
- $scope.$parent.get_data();
- };
-
- $scope.groupByFieldChanged = function() {
- $scope.target.groupBy = $scope.groupByFieldSegment.value;
- $scope.$parent.get_data();
- };
-
- $scope.changeFunction = function(func) {
- $scope.target.function = func;
- $scope.$parent.get_data();
+ return segments;
+ };
};
$scope.handleQueryError = function(err) {