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.
 
 
 

165 lines
4.2 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. "encoding/json"
  7. "net/http"
  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. // Detected version of ES
  19. version string
  20. // user name for http basic auth
  21. AuthUsername string `json:"username"`
  22. // pass word for http basic auth
  23. AuthPassword string `json:"password"`
  24. }
  25. // Response holds an elasticsearch response
  26. type Response struct {
  27. Acknowledged bool
  28. Error string
  29. RawError json.RawMessage `json:"error"`
  30. Errors bool
  31. Status uint64
  32. Took uint64
  33. TimedOut bool `json:"timed_out"`
  34. Shards Shard `json:"_shards"`
  35. Hits Hits
  36. Index string `json:"_index"`
  37. ID string `json:"_id"`
  38. Type string `json:"_type"`
  39. Version int `json:"_version"`
  40. Found bool
  41. Count int
  42. // Used by the _stats API
  43. All All `json:"_all"`
  44. // Used by the _bulk API
  45. Items []map[string]Item `json:"items,omitempty"`
  46. // Used by the GET API
  47. Source map[string]interface{} `json:"_source"`
  48. Fields map[string]interface{} `json:"fields"`
  49. // Used by the _status API
  50. Indices map[string]IndexStatus
  51. // Scroll id for iteration
  52. ScrollID string `json:"_scroll_id"`
  53. Aggregations map[string]Aggregation `json:"aggregations,omitempty"`
  54. Raw map[string]interface{}
  55. }
  56. // Aggregation holds the aggregation portion of an ES response
  57. type Aggregation map[string]interface{}
  58. // Bucket represents a bucket for aggregation
  59. type Bucket map[string]interface{}
  60. // Document holds a document to send to elasticsearch
  61. type Document struct {
  62. // XXX : interface as we can support nil values
  63. Index interface{}
  64. Type string
  65. ID interface{}
  66. BulkCommand string
  67. Fields interface{}
  68. }
  69. // Item holds an item from the "items" field in a _bulk response
  70. type Item struct {
  71. Type string `json:"_type"`
  72. ID string `json:"_id"`
  73. Index string `json:"_index"`
  74. Version int `json:"_version"`
  75. Error string `json:"error"`
  76. Status uint64 `json:"status"`
  77. }
  78. // All represents the "_all" field when calling the _stats API
  79. // This is minimal but this is what I only need
  80. type All struct {
  81. Indices map[string]StatIndex `json:"indices"`
  82. Primaries map[string]StatPrimary `json:"primaries"`
  83. }
  84. // StatIndex contains stats for a specific index
  85. type StatIndex struct {
  86. Primaries map[string]StatPrimary `json:"primaries"`
  87. }
  88. // StatPrimary contains stats for a primary index
  89. type StatPrimary struct {
  90. // primary/docs:
  91. Count int
  92. Deleted int
  93. }
  94. // Shard holds the "shard" struct as returned by elasticsearch
  95. type Shard struct {
  96. Total uint64
  97. Successful uint64
  98. Failed uint64
  99. }
  100. // Hit holds a hit returned by a search
  101. type Hit struct {
  102. Index string `json:"_index"`
  103. Type string `json:"_type"`
  104. ID string `json:"_id"`
  105. Score float64 `json:"_score"`
  106. Source map[string]interface{} `json:"_source"`
  107. Highlight map[string]interface{} `json:"highlight"`
  108. Fields map[string]interface{} `json:"fields"`
  109. }
  110. // Hits holds the hits structure as returned by elasticsearch
  111. type Hits struct {
  112. Total uint64
  113. // max_score may contain the "null" value
  114. MaxScore interface{} `json:"max_score"`
  115. Hits []Hit
  116. }
  117. // SearchError holds errors returned from an ES search
  118. type SearchError struct {
  119. Msg string
  120. StatusCode uint64
  121. }
  122. // IndexStatus holds the status for a given index for the _status command
  123. type IndexStatus struct {
  124. // XXX : problem, int will be marshaled to a float64 which seems logical
  125. // XXX : is it better to use strings even for int values or to keep
  126. // XXX : interfaces and deal with float64 ?
  127. Index map[string]interface{}
  128. Translog map[string]uint64
  129. Docs map[string]uint64
  130. Merges map[string]interface{}
  131. Refresh map[string]interface{}
  132. Flush map[string]interface{}
  133. // TODO: add shards support later, we do not need it for the moment
  134. }