models.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. package es
  2. import (
  3. "encoding/json"
  4. "github.com/grafana/grafana/pkg/tsdb"
  5. )
  6. // SearchRequest represents a search request
  7. type SearchRequest struct {
  8. Index string
  9. Interval tsdb.Interval
  10. Size int
  11. Sort map[string]interface{}
  12. Query *Query
  13. Aggs AggArray
  14. CustomProps map[string]interface{}
  15. }
  16. // MarshalJSON returns the JSON encoding of the request.
  17. func (r *SearchRequest) MarshalJSON() ([]byte, error) {
  18. root := make(map[string]interface{})
  19. root["size"] = r.Size
  20. if len(r.Sort) > 0 {
  21. root["sort"] = r.Sort
  22. }
  23. for key, value := range r.CustomProps {
  24. root[key] = value
  25. }
  26. root["query"] = r.Query
  27. if len(r.Aggs) > 0 {
  28. root["aggs"] = r.Aggs
  29. }
  30. return json.Marshal(root)
  31. }
  32. // SearchResponseHits represents search response hits
  33. type SearchResponseHits struct {
  34. Hits []map[string]interface{}
  35. Total int64
  36. }
  37. // SearchResponse represents a search response
  38. type SearchResponse struct {
  39. Error map[string]interface{} `json:"error"`
  40. Aggregations map[string]interface{} `json:"aggregations"`
  41. Hits *SearchResponseHits `json:"hits"`
  42. }
  43. // func (r *Response) getErrMsg() string {
  44. // var msg bytes.Buffer
  45. // errJson := simplejson.NewFromAny(r.Err)
  46. // errType, err := errJson.Get("type").String()
  47. // if err == nil {
  48. // msg.WriteString(fmt.Sprintf("type:%s", errType))
  49. // }
  50. // reason, err := errJson.Get("type").String()
  51. // if err == nil {
  52. // msg.WriteString(fmt.Sprintf("reason:%s", reason))
  53. // }
  54. // return msg.String()
  55. // }
  56. // MultiSearchRequest represents a multi search request
  57. type MultiSearchRequest struct {
  58. Requests []*SearchRequest
  59. }
  60. // MultiSearchResponse represents a multi search response
  61. type MultiSearchResponse struct {
  62. Status int `json:"status,omitempty"`
  63. Responses []*SearchResponse `json:"responses"`
  64. }
  65. // Query represents a query
  66. type Query struct {
  67. Bool *BoolQuery `json:"bool"`
  68. }
  69. // BoolQuery represents a bool query
  70. type BoolQuery struct {
  71. Filters []Filter
  72. }
  73. // NewBoolQuery create a new bool query
  74. func NewBoolQuery() *BoolQuery {
  75. return &BoolQuery{Filters: make([]Filter, 0)}
  76. }
  77. // MarshalJSON returns the JSON encoding of the boolean query.
  78. func (q *BoolQuery) MarshalJSON() ([]byte, error) {
  79. root := make(map[string]interface{})
  80. if len(q.Filters) > 0 {
  81. if len(q.Filters) == 1 {
  82. root["filter"] = q.Filters[0]
  83. } else {
  84. root["filter"] = q.Filters
  85. }
  86. }
  87. return json.Marshal(root)
  88. }
  89. // Filter represents a search filter
  90. type Filter interface{}
  91. // QueryStringFilter represents a query string search filter
  92. type QueryStringFilter struct {
  93. Filter
  94. Query string
  95. AnalyzeWildcard bool
  96. }
  97. // MarshalJSON returns the JSON encoding of the query string filter.
  98. func (f *QueryStringFilter) MarshalJSON() ([]byte, error) {
  99. root := map[string]interface{}{
  100. "query_string": map[string]interface{}{
  101. "query": f.Query,
  102. "analyze_wildcard": f.AnalyzeWildcard,
  103. },
  104. }
  105. return json.Marshal(root)
  106. }
  107. // RangeFilter represents a range search filter
  108. type RangeFilter struct {
  109. Filter
  110. Key string
  111. Gte string
  112. Lte string
  113. Format string
  114. }
  115. // DateFormatEpochMS represents a date format of epoch milliseconds (epoch_millis)
  116. const DateFormatEpochMS = "epoch_millis"
  117. // MarshalJSON returns the JSON encoding of the query string filter.
  118. func (f *RangeFilter) MarshalJSON() ([]byte, error) {
  119. root := map[string]map[string]map[string]interface{}{
  120. "range": {
  121. f.Key: {
  122. "lte": f.Lte,
  123. "gte": f.Gte,
  124. },
  125. },
  126. }
  127. if f.Format != "" {
  128. root["range"][f.Key]["format"] = f.Format
  129. }
  130. return json.Marshal(root)
  131. }
  132. // Aggregation represents an aggregation
  133. type Aggregation interface{}
  134. // Agg represents a key and aggregation
  135. type Agg struct {
  136. Key string
  137. Aggregation *aggContainer
  138. }
  139. // MarshalJSON returns the JSON encoding of the agg
  140. func (a *Agg) MarshalJSON() ([]byte, error) {
  141. root := map[string]interface{}{
  142. a.Key: a.Aggregation,
  143. }
  144. return json.Marshal(root)
  145. }
  146. // AggArray represents a collection of key/aggregation pairs
  147. type AggArray []*Agg
  148. // MarshalJSON returns the JSON encoding of the agg
  149. func (a AggArray) MarshalJSON() ([]byte, error) {
  150. aggsMap := make(map[string]Aggregation)
  151. for _, subAgg := range a {
  152. aggsMap[subAgg.Key] = subAgg.Aggregation
  153. }
  154. return json.Marshal(aggsMap)
  155. }
  156. type aggContainer struct {
  157. Type string
  158. Aggregation Aggregation
  159. Aggs AggArray
  160. }
  161. // MarshalJSON returns the JSON encoding of the aggregation container
  162. func (a *aggContainer) MarshalJSON() ([]byte, error) {
  163. root := map[string]interface{}{
  164. a.Type: a.Aggregation,
  165. }
  166. if len(a.Aggs) > 0 {
  167. root["aggs"] = a.Aggs
  168. }
  169. return json.Marshal(root)
  170. }
  171. type aggDef struct {
  172. key string
  173. aggregation *aggContainer
  174. builders []AggBuilder
  175. }
  176. func newAggDef(key string, aggregation *aggContainer) *aggDef {
  177. return &aggDef{
  178. key: key,
  179. aggregation: aggregation,
  180. builders: make([]AggBuilder, 0),
  181. }
  182. }
  183. // HistogramAgg represents a histogram aggregation
  184. type HistogramAgg struct {
  185. Interval int `json:"interval,omitempty"`
  186. Field string `json:"field"`
  187. MinDocCount int `json:"min_doc_count"`
  188. Missing *int `json:"missing,omitempty"`
  189. }
  190. // DateHistogramAgg represents a date histogram aggregation
  191. type DateHistogramAgg struct {
  192. Field string `json:"field"`
  193. Interval string `json:"interval,omitempty"`
  194. MinDocCount int `json:"min_doc_count"`
  195. Missing *string `json:"missing,omitempty"`
  196. ExtendedBounds *ExtendedBounds `json:"extended_bounds"`
  197. Format string `json:"format"`
  198. }
  199. // FiltersAggregation represents a filters aggregation
  200. type FiltersAggregation struct {
  201. Filters map[string]interface{} `json:"filters"`
  202. }
  203. // TermsAggregation represents a terms aggregation
  204. type TermsAggregation struct {
  205. Field string `json:"field"`
  206. Size int `json:"size"`
  207. Order map[string]interface{} `json:"order"`
  208. MinDocCount *int `json:"min_doc_count,omitempty"`
  209. Missing *string `json:"missing,omitempty"`
  210. }
  211. // ExtendedBounds represents extended bounds
  212. type ExtendedBounds struct {
  213. Min string `json:"min"`
  214. Max string `json:"max"`
  215. }
  216. // GeoHashGridAggregation represents a geo hash grid aggregation
  217. type GeoHashGridAggregation struct {
  218. Field string `json:"field"`
  219. Precision int `json:"precision"`
  220. }
  221. // MetricAggregation represents a metric aggregation
  222. type MetricAggregation struct {
  223. Field string
  224. Settings map[string]interface{}
  225. }
  226. // MarshalJSON returns the JSON encoding of the metric aggregation
  227. func (a *MetricAggregation) MarshalJSON() ([]byte, error) {
  228. root := map[string]interface{}{
  229. "field": a.Field,
  230. }
  231. for k, v := range a.Settings {
  232. if k != "" && v != nil {
  233. root[k] = v
  234. }
  235. }
  236. return json.Marshal(root)
  237. }
  238. // PipelineAggregation represents a metric aggregation
  239. type PipelineAggregation struct {
  240. BucketPath string
  241. Settings map[string]interface{}
  242. }
  243. // MarshalJSON returns the JSON encoding of the pipeline aggregation
  244. func (a *PipelineAggregation) MarshalJSON() ([]byte, error) {
  245. root := map[string]interface{}{
  246. "buckets_path": a.BucketPath,
  247. }
  248. for k, v := range a.Settings {
  249. if k != "" && v != nil {
  250. root[k] = v
  251. }
  252. }
  253. return json.Marshal(root)
  254. }