1
0
Fork 0
telegraf/plugins/inputs/opensearch_query/query.go
Daniel Baumann 4978089aab
Adding upstream version 1.34.4.
Signed-off-by: Daniel Baumann <daniel@debian.org>
2025-05-24 07:26:29 +02:00

45 lines
1.1 KiB
Go

package opensearch_query
import (
"encoding/json"
"time"
)
type query struct {
Size int `json:"size"`
Aggregations aggregationRequest `json:"aggregations"`
Query interface{} `json:"query,omitempty"`
}
type boolQuery struct {
FilterQueryString string
TimestampField string
TimeRangeFrom time.Time
TimeRangeTo time.Time
DateFieldFormat string
}
// MarshalJSON customizes the JSON marshaling for boolQuery.
func (b *boolQuery) MarshalJSON() ([]byte, error) {
// Construct range
dateTimeRange := map[string]interface{}{
"from": b.TimeRangeFrom,
"to": b.TimeRangeTo,
"include_lower": true,
"include_upper": true,
}
if b.DateFieldFormat != "" {
dateTimeRange["format"] = b.DateFieldFormat
}
rangeFilter := map[string]map[string]interface{}{"range": {b.TimestampField: dateTimeRange}}
// Construct Filter
queryFilter := map[string]map[string]interface{}{
"query_string": {"query": b.FilterQueryString},
}
// Construct boolean query
bq := map[string]map[string]interface{}{"bool": {"filter": []interface{}{rangeFilter, queryFilter}}}
return json.Marshal(&bq)
}