models.go 7.0 KB

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