models.go 6.8 KB

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