2018-03-23 10:50:16 -05:00
|
|
|
package elasticsearch
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2018-03-26 06:48:57 -05:00
|
|
|
"fmt"
|
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
2018-03-23 10:50:16 -05:00
|
|
|
)
|
|
|
|
|
2018-03-27 12:42:25 -05:00
|
|
|
type BucketAgg struct {
|
|
|
|
Field string `json:"field"`
|
|
|
|
ID string `json:"id"`
|
|
|
|
Settings *simplejson.Json `json:"settings"`
|
|
|
|
Type string `jsons:"type"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Metric struct {
|
|
|
|
Field string `json:"field"`
|
|
|
|
Hide bool `json:"hide"`
|
|
|
|
ID string `json:"id"`
|
|
|
|
PipelineAggregate string `json:"pipelineAgg"`
|
|
|
|
Settings *simplejson.Json `json:"settings"`
|
|
|
|
Type string `json:"type"`
|
|
|
|
}
|
|
|
|
|
2018-03-23 10:50:16 -05:00
|
|
|
type QueryHeader struct {
|
|
|
|
SearchType string `json:"search_type"`
|
|
|
|
IgnoreUnavailable bool `json:"ignore_unavailable"`
|
|
|
|
Index interface{} `json:"index"`
|
2018-03-26 06:48:57 -05:00
|
|
|
MaxConcurrentShardRequests int `json:"max_concurrent_shard_requests,omitempty"`
|
2018-03-23 10:50:16 -05:00
|
|
|
}
|
|
|
|
|
2018-03-26 06:48:57 -05:00
|
|
|
func (q *QueryHeader) String() string {
|
2018-03-23 10:50:16 -05:00
|
|
|
r, _ := json.Marshal(q)
|
|
|
|
return string(r)
|
|
|
|
}
|
|
|
|
|
2018-03-26 06:48:57 -05:00
|
|
|
type Request struct {
|
2018-03-23 10:50:16 -05:00
|
|
|
Query map[string]interface{} `json:"query"`
|
|
|
|
Aggs Aggs `json:"aggs"`
|
|
|
|
Size int `json:"size"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Aggs map[string]interface{}
|
|
|
|
|
|
|
|
type HistogramAgg struct {
|
|
|
|
Interval string `json:"interval,omitempty"`
|
|
|
|
Field string `json:"field"`
|
|
|
|
MinDocCount int `json:"min_doc_count"`
|
|
|
|
Missing string `json:"missing,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type DateHistogramAgg struct {
|
|
|
|
HistogramAgg
|
|
|
|
ExtendedBounds ExtendedBounds `json:"extended_bounds"`
|
|
|
|
Format string `json:"format"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type FiltersAgg struct {
|
2018-03-26 13:12:43 -05:00
|
|
|
Filters map[string]interface{} `json:"filters"`
|
2018-03-23 10:50:16 -05:00
|
|
|
}
|
|
|
|
|
2018-03-27 12:42:25 -05:00
|
|
|
type TermsAgg struct {
|
2018-03-26 06:48:57 -05:00
|
|
|
Field string `json:"field"`
|
|
|
|
Size int `json:"size"`
|
|
|
|
Order map[string]interface{} `json:"order"`
|
|
|
|
Missing string `json:"missing,omitempty"`
|
2018-03-23 10:50:16 -05:00
|
|
|
}
|
|
|
|
|
2018-03-27 12:42:25 -05:00
|
|
|
type TermsAggWrap struct {
|
|
|
|
Terms TermsAgg `json:"terms"`
|
|
|
|
Aggs Aggs `json:"aggs"`
|
2018-03-23 10:50:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type ExtendedBounds struct {
|
|
|
|
Min string `json:"min"`
|
|
|
|
Max string `json:"max"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type RangeFilter struct {
|
|
|
|
Range map[string]RangeFilterSetting `json:"range"`
|
|
|
|
}
|
|
|
|
type RangeFilterSetting struct {
|
|
|
|
Gte string `json:"gte"`
|
|
|
|
Lte string `json:"lte"`
|
|
|
|
Format string `json:"format"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func newRangeFilter(field string, rangeFilterSetting RangeFilterSetting) *RangeFilter {
|
|
|
|
return &RangeFilter{
|
|
|
|
map[string]RangeFilterSetting{field: rangeFilterSetting}}
|
|
|
|
}
|
|
|
|
|
|
|
|
type QueryStringFilter struct {
|
|
|
|
QueryString QueryStringFilterSetting `json:"query_string"`
|
|
|
|
}
|
|
|
|
type QueryStringFilterSetting struct {
|
|
|
|
AnalyzeWildcard bool `json:"analyze_wildcard"`
|
|
|
|
Query string `json:"query"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func newQueryStringFilter(analyzeWildcard bool, query string) *QueryStringFilter {
|
|
|
|
return &QueryStringFilter{QueryStringFilterSetting{AnalyzeWildcard: analyzeWildcard, Query: query}}
|
|
|
|
}
|
|
|
|
|
|
|
|
type BoolQuery struct {
|
|
|
|
Filter []interface{} `json:"filter"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Responses struct {
|
|
|
|
Responses []Response `json:"responses"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Response struct {
|
|
|
|
Status int `json:"status"`
|
|
|
|
Err map[string]interface{} `json:"error"`
|
|
|
|
Aggregations map[string]interface{} `json:"aggregations"`
|
|
|
|
}
|
|
|
|
|
2018-03-26 06:48:57 -05:00
|
|
|
func (r *Response) getErrMsg() string {
|
2018-03-23 10:50:16 -05:00
|
|
|
var msg bytes.Buffer
|
|
|
|
errJson := simplejson.NewFromAny(r.Err)
|
|
|
|
errType, err := errJson.Get("type").String()
|
|
|
|
if err == nil {
|
|
|
|
msg.WriteString(fmt.Sprintf("type:%s", errType))
|
|
|
|
}
|
|
|
|
|
|
|
|
reason, err := errJson.Get("type").String()
|
|
|
|
if err == nil {
|
|
|
|
msg.WriteString(fmt.Sprintf("reason:%s", reason))
|
|
|
|
}
|
|
|
|
return msg.String()
|
|
|
|
}
|