models.go 7.3 KB

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