get.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. // Copyright 2012-2015 Oliver Eilhard. All rights reserved.
  2. // Use of this source code is governed by a MIT-license.
  3. // See http://olivere.mit-license.org/license.txt for details.
  4. package elastic
  5. import (
  6. "encoding/json"
  7. "fmt"
  8. "net/url"
  9. "strings"
  10. "github.com/olivere/elastic/uritemplates"
  11. )
  12. type GetService struct {
  13. client *Client
  14. index string
  15. typ string
  16. id string
  17. routing string
  18. preference string
  19. fields []string
  20. refresh *bool
  21. realtime *bool
  22. fsc *FetchSourceContext
  23. versionType string
  24. version *int64
  25. ignoreErrorsOnGeneratedFields *bool
  26. }
  27. func NewGetService(client *Client) *GetService {
  28. builder := &GetService{
  29. client: client,
  30. typ: "_all",
  31. }
  32. return builder
  33. }
  34. func (b *GetService) String() string {
  35. return fmt.Sprintf("[%v][%v][%v]: routing [%v]",
  36. b.index,
  37. b.typ,
  38. b.id,
  39. b.routing)
  40. }
  41. func (b *GetService) Index(index string) *GetService {
  42. b.index = index
  43. return b
  44. }
  45. func (b *GetService) Type(typ string) *GetService {
  46. b.typ = typ
  47. return b
  48. }
  49. func (b *GetService) Id(id string) *GetService {
  50. b.id = id
  51. return b
  52. }
  53. func (b *GetService) Parent(parent string) *GetService {
  54. if b.routing == "" {
  55. b.routing = parent
  56. }
  57. return b
  58. }
  59. func (b *GetService) Routing(routing string) *GetService {
  60. b.routing = routing
  61. return b
  62. }
  63. func (b *GetService) Preference(preference string) *GetService {
  64. b.preference = preference
  65. return b
  66. }
  67. func (b *GetService) Fields(fields ...string) *GetService {
  68. if b.fields == nil {
  69. b.fields = make([]string, 0)
  70. }
  71. b.fields = append(b.fields, fields...)
  72. return b
  73. }
  74. func (s *GetService) FetchSource(fetchSource bool) *GetService {
  75. if s.fsc == nil {
  76. s.fsc = NewFetchSourceContext(fetchSource)
  77. } else {
  78. s.fsc.SetFetchSource(fetchSource)
  79. }
  80. return s
  81. }
  82. func (s *GetService) FetchSourceContext(fetchSourceContext *FetchSourceContext) *GetService {
  83. s.fsc = fetchSourceContext
  84. return s
  85. }
  86. func (b *GetService) Refresh(refresh bool) *GetService {
  87. b.refresh = &refresh
  88. return b
  89. }
  90. func (b *GetService) Realtime(realtime bool) *GetService {
  91. b.realtime = &realtime
  92. return b
  93. }
  94. func (b *GetService) VersionType(versionType string) *GetService {
  95. b.versionType = versionType
  96. return b
  97. }
  98. func (b *GetService) Version(version int64) *GetService {
  99. b.version = &version
  100. return b
  101. }
  102. func (b *GetService) IgnoreErrorsOnGeneratedFields(ignore bool) *GetService {
  103. b.ignoreErrorsOnGeneratedFields = &ignore
  104. return b
  105. }
  106. // Validate checks if the operation is valid.
  107. func (s *GetService) Validate() error {
  108. var invalid []string
  109. if s.id == "" {
  110. invalid = append(invalid, "Id")
  111. }
  112. if s.index == "" {
  113. invalid = append(invalid, "Index")
  114. }
  115. if s.typ == "" {
  116. invalid = append(invalid, "Type")
  117. }
  118. if len(invalid) > 0 {
  119. return fmt.Errorf("missing required fields: %v", invalid)
  120. }
  121. return nil
  122. }
  123. func (b *GetService) Do() (*GetResult, error) {
  124. // Check pre-conditions
  125. if err := b.Validate(); err != nil {
  126. return nil, err
  127. }
  128. // Build url
  129. path, err := uritemplates.Expand("/{index}/{type}/{id}", map[string]string{
  130. "index": b.index,
  131. "type": b.typ,
  132. "id": b.id,
  133. })
  134. if err != nil {
  135. return nil, err
  136. }
  137. params := make(url.Values)
  138. if b.realtime != nil {
  139. params.Add("realtime", fmt.Sprintf("%v", *b.realtime))
  140. }
  141. if len(b.fields) > 0 {
  142. params.Add("fields", strings.Join(b.fields, ","))
  143. }
  144. if b.routing != "" {
  145. params.Add("routing", b.routing)
  146. }
  147. if b.preference != "" {
  148. params.Add("preference", b.preference)
  149. }
  150. if b.refresh != nil {
  151. params.Add("refresh", fmt.Sprintf("%v", *b.refresh))
  152. }
  153. if b.realtime != nil {
  154. params.Add("realtime", fmt.Sprintf("%v", *b.realtime))
  155. }
  156. if b.ignoreErrorsOnGeneratedFields != nil {
  157. params.Add("ignore_errors_on_generated_fields", fmt.Sprintf("%v", *b.ignoreErrorsOnGeneratedFields))
  158. }
  159. if len(b.fields) > 0 {
  160. params.Add("_fields", strings.Join(b.fields, ","))
  161. }
  162. if b.version != nil {
  163. params.Add("version", fmt.Sprintf("%d", *b.version))
  164. }
  165. if b.versionType != "" {
  166. params.Add("version_type", b.versionType)
  167. }
  168. if b.fsc != nil {
  169. for k, values := range b.fsc.Query() {
  170. params.Add(k, strings.Join(values, ","))
  171. }
  172. }
  173. // Get response
  174. res, err := b.client.PerformRequest("GET", path, params, nil)
  175. if err != nil {
  176. return nil, err
  177. }
  178. // Return result
  179. ret := new(GetResult)
  180. if err := json.Unmarshal(res.Body, ret); err != nil {
  181. return nil, err
  182. }
  183. return ret, nil
  184. }
  185. // -- Result of a get request.
  186. type GetResult struct {
  187. Index string `json:"_index"`
  188. Type string `json:"_type"`
  189. Id string `json:"_id"`
  190. Version int64 `json:"_version,omitempty"`
  191. Source *json.RawMessage `json:"_source,omitempty"`
  192. Found bool `json:"found,omitempty"`
  193. Fields []string `json:"fields,omitempty"`
  194. Error string `json:"error,omitempty"` // used only in MultiGet
  195. }