diff --git a/pkg/opensearch/lucene_handler.go b/pkg/opensearch/lucene_handler.go index 5be5db38..2359063b 100644 --- a/pkg/opensearch/lucene_handler.go +++ b/pkg/opensearch/lucene_handler.go @@ -44,7 +44,8 @@ func (h *luceneHandler) processQuery(q *Query) error { b := h.ms.Search(interval) b.Size(0) filters := b.Query().Bool().Filter() - filters.AddDateRangeFilter(h.client.GetTimeField(), es.DateFormatEpochMS, toMs, fromMs) + defaultTimeField := h.client.GetTimeField() + filters.AddDateRangeFilter(defaultTimeField, es.DateFormatEpochMS, toMs, fromMs) if q.RawQuery != "" { filters.AddQueryStringFilter(q.RawQuery, true) @@ -59,9 +60,9 @@ func (h *luceneHandler) processQuery(q *Query) error { switch { case q.Metrics[0].Type == rawDataType: - processRawDataQuery(q, b, h.client.GetTimeField()) + processRawDataQuery(q, b, defaultTimeField) default: - processTimeSeriesQuery(q, b, fromMs, toMs) + processTimeSeriesQuery(q, b, fromMs, toMs, defaultTimeField) } return nil @@ -83,7 +84,7 @@ func processRawDataQuery(q *Query, b *es.SearchRequestBuilder, defaultTimeField b.Size(size) } -func processTimeSeriesQuery(q *Query, b *es.SearchRequestBuilder, fromMs int64, toMs int64) { +func processTimeSeriesQuery(q *Query, b *es.SearchRequestBuilder, fromMs int64, toMs int64, defaultTimeField string) { metric := q.Metrics[0] b.Size(metric.Settings.Get("size").MustInt(500)) aggBuilder := b.Agg() @@ -92,7 +93,7 @@ func processTimeSeriesQuery(q *Query, b *es.SearchRequestBuilder, fromMs int64, for _, bucketAgg := range q.BucketAggs { switch bucketAgg.Type { case dateHistType: - aggBuilder = addDateHistogramAgg(aggBuilder, bucketAgg, fromMs, toMs) + aggBuilder = addDateHistogramAgg(aggBuilder, bucketAgg, fromMs, toMs, defaultTimeField) case histogramType: aggBuilder = addHistogramAgg(aggBuilder, bucketAgg) case filtersType: @@ -204,8 +205,13 @@ func (h *luceneHandler) executeQueries() (*backend.QueryDataResponse, error) { return rp.getTimeSeries(h.client.GetTimeField()) } -func addDateHistogramAgg(aggBuilder es.AggBuilder, bucketAgg *BucketAgg, timeFrom, timeTo int64) es.AggBuilder { - aggBuilder.DateHistogram(bucketAgg.ID, bucketAgg.Field, func(a *es.DateHistogramAgg, b es.AggBuilder) { +func addDateHistogramAgg(aggBuilder es.AggBuilder, bucketAgg *BucketAgg, timeFrom, timeTo int64, timeField string) es.AggBuilder { + // If no field is specified, use the time field + field := bucketAgg.Field + if field == "" { + field = timeField + } + aggBuilder.DateHistogram(bucketAgg.ID, field, func(a *es.DateHistogramAgg, b es.AggBuilder) { a.Interval = bucketAgg.Settings.Get("interval").MustString("auto") a.MinDocCount = bucketAgg.Settings.Get("min_doc_count").MustInt(0) a.ExtendedBounds = &es.ExtendedBounds{Min: timeFrom, Max: timeTo} diff --git a/pkg/opensearch/time_series_query_test.go b/pkg/opensearch/time_series_query_test.go index 14cc4c1a..f7196ebf 100644 --- a/pkg/opensearch/time_series_query_test.go +++ b/pkg/opensearch/time_series_query_test.go @@ -1031,3 +1031,39 @@ func TestTimeSeriesQueryParser(t *testing.T) { }) }) } + +func Test_Field_property(t *testing.T) { + from := time.Date(2018, 5, 15, 17, 50, 0, 0, time.UTC) + to := time.Date(2018, 5, 15, 17, 55, 0, 0, time.UTC) + t.Run("Should use timeField from datasource when not specified", func(t *testing.T) { + c := newFakeClient(es.Elasticsearch, "2.0.0") + _, err := executeTsdbQuery(c, `{ + "timeField": "@timestamp", + "metrics": [{ "type": "count", "id": "1" }], + "bucketAggs": [ + { "type": "date_histogram", "id": "2", "settings": { "min_doc_count": "1" } } + ] + }`, from, to, 15*time.Second) + assert.Nil(t, err) + + sr := c.multisearchRequests[0].Requests[0] + dateHistogramAgg := sr.Aggs[0].Aggregation.Aggregation.(*es.DateHistogramAgg) + assert.Equal(t, "@timestamp", dateHistogramAgg.Field) + }) + + t.Run("Should use field from bucket agg when specified", func(t *testing.T) { + c := newFakeClient(es.Elasticsearch, "2.0.0") + _, err := executeTsdbQuery(c, `{ + "timeField": "@timestamp", + "metrics": [{ "type": "count", "id": "1" }], + "bucketAggs": [ + { "type": "date_histogram", "id": "2", "field": "some_other_field", "settings": { "min_doc_count": "1" } } + ] + }`, from, to, 15*time.Second) + + assert.Nil(t, err) + sr := c.multisearchRequests[0].Requests[0] + dateHistogramAgg := sr.Aggs[0].Aggregation.Aggregation.(*es.DateHistogramAgg) + assert.Equal(t, "some_other_field", dateHistogramAgg.Field) + }) +}