You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

188 lines
4.5 KiB

  1. // Copyright 2013 Belogik. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package goes
  5. import (
  6. "net/http"
  7. "net/url"
  8. )
  9. // Client represents a connection to elasticsearch
  10. type Client struct {
  11. // The host to connect to
  12. Host string
  13. // The port to use
  14. Port string
  15. // Client is the http client used to make requests, allowing settings things
  16. // such as timeouts etc
  17. Client *http.Client
  18. }
  19. // Request holds a single request to elasticsearch
  20. type Request struct {
  21. // Which connection will be used
  22. Conn *Client
  23. // A search query
  24. Query interface{}
  25. // Which index to search into
  26. IndexList []string
  27. // Which type to search into
  28. TypeList []string
  29. // HTTP Method to user (GET, POST ...)
  30. method string
  31. // Which api keyword (_search, _bulk, etc) to use
  32. api string
  33. // Bulk data
  34. bulkData []byte
  35. // Request body
  36. Body []byte
  37. // A list of extra URL arguments
  38. ExtraArgs url.Values
  39. // Used for the id field when indexing a document
  40. id string
  41. }
  42. // Response holds an elasticsearch response
  43. type Response struct {
  44. Acknowledged bool
  45. Error string
  46. Errors bool
  47. Status uint64
  48. Took uint64
  49. TimedOut bool `json:"timed_out"`
  50. Shards Shard `json:"_shards"`
  51. Hits Hits
  52. Index string `json:"_index"`
  53. ID string `json:"_id"`
  54. Type string `json:"_type"`
  55. Version int `json:"_version"`
  56. Found bool
  57. Count int
  58. // Used by the _stats API
  59. All All `json:"_all"`
  60. // Used by the _bulk API
  61. Items []map[string]Item `json:"items,omitempty"`
  62. // Used by the GET API
  63. Source map[string]interface{} `json:"_source"`
  64. Fields map[string]interface{} `json:"fields"`
  65. // Used by the _status API
  66. Indices map[string]IndexStatus
  67. // Scroll id for iteration
  68. ScrollID string `json:"_scroll_id"`
  69. Aggregations map[string]Aggregation `json:"aggregations,omitempty"`
  70. Raw map[string]interface{}
  71. }
  72. // Aggregation holds the aggregation portion of an ES response
  73. type Aggregation map[string]interface{}
  74. // Bucket represents a bucket for aggregation
  75. type Bucket map[string]interface{}
  76. // Document holds a document to send to elasticsearch
  77. type Document struct {
  78. // XXX : interface as we can support nil values
  79. Index interface{}
  80. Type string
  81. ID interface{}
  82. BulkCommand string
  83. Fields interface{}
  84. }
  85. // Item holds an item from the "items" field in a _bulk response
  86. type Item struct {
  87. Type string `json:"_type"`
  88. ID string `json:"_id"`
  89. Index string `json:"_index"`
  90. Version int `json:"_version"`
  91. Error string `json:"error"`
  92. Status uint64 `json:"status"`
  93. }
  94. // All represents the "_all" field when calling the _stats API
  95. // This is minimal but this is what I only need
  96. type All struct {
  97. Indices map[string]StatIndex `json:"indices"`
  98. Primaries map[string]StatPrimary `json:"primaries"`
  99. }
  100. // StatIndex contains stats for a specific index
  101. type StatIndex struct {
  102. Primaries map[string]StatPrimary `json:"primaries"`
  103. }
  104. // StatPrimary contains stats for a primary index
  105. type StatPrimary struct {
  106. // primary/docs:
  107. Count int
  108. Deleted int
  109. }
  110. // Shard holds the "shard" struct as returned by elasticsearch
  111. type Shard struct {
  112. Total uint64
  113. Successful uint64
  114. Failed uint64
  115. }
  116. // Hit holds a hit returned by a search
  117. type Hit struct {
  118. Index string `json:"_index"`
  119. Type string `json:"_type"`
  120. ID string `json:"_id"`
  121. Score float64 `json:"_score"`
  122. Source map[string]interface{} `json:"_source"`
  123. Highlight map[string]interface{} `json:"highlight"`
  124. Fields map[string]interface{} `json:"fields"`
  125. }
  126. // Hits holds the hits structure as returned by elasticsearch
  127. type Hits struct {
  128. Total uint64
  129. // max_score may contain the "null" value
  130. MaxScore interface{} `json:"max_score"`
  131. Hits []Hit
  132. }
  133. // SearchError holds errors returned from an ES search
  134. type SearchError struct {
  135. Msg string
  136. StatusCode uint64
  137. }
  138. // IndexStatus holds the status for a given index for the _status command
  139. type IndexStatus struct {
  140. // XXX : problem, int will be marshaled to a float64 which seems logical
  141. // XXX : is it better to use strings even for int values or to keep
  142. // XXX : interfaces and deal with float64 ?
  143. Index map[string]interface{}
  144. Translog map[string]uint64
  145. Docs map[string]uint64
  146. Merges map[string]interface{}
  147. Refresh map[string]interface{}
  148. Flush map[string]interface{}
  149. // TODO: add shards support later, we do not need it for the moment
  150. }