index.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. "github.com/olivere/elastic/uritemplates"
  10. )
  11. // IndexResult is the result of indexing a document in Elasticsearch.
  12. type IndexResult struct {
  13. Index string `json:"_index"`
  14. Type string `json:"_type"`
  15. Id string `json:"_id"`
  16. Version int `json:"_version"`
  17. Created bool `json:"created"`
  18. }
  19. // IndexService adds documents to Elasticsearch.
  20. type IndexService struct {
  21. client *Client
  22. index string
  23. _type string
  24. id string
  25. routing string
  26. parent string
  27. opType string
  28. refresh *bool
  29. version *int64
  30. versionType string
  31. timestamp string
  32. ttl string
  33. timeout string
  34. bodyString string
  35. bodyJson interface{}
  36. pretty bool
  37. }
  38. func NewIndexService(client *Client) *IndexService {
  39. builder := &IndexService{
  40. client: client,
  41. }
  42. return builder
  43. }
  44. func (b *IndexService) Index(name string) *IndexService {
  45. b.index = name
  46. return b
  47. }
  48. func (b *IndexService) Type(_type string) *IndexService {
  49. b._type = _type
  50. return b
  51. }
  52. func (b *IndexService) Id(id string) *IndexService {
  53. b.id = id
  54. return b
  55. }
  56. func (b *IndexService) Routing(routing string) *IndexService {
  57. b.routing = routing
  58. return b
  59. }
  60. func (b *IndexService) Parent(parent string) *IndexService {
  61. b.parent = parent
  62. return b
  63. }
  64. // OpType is either "create" or "index" (the default).
  65. func (b *IndexService) OpType(opType string) *IndexService {
  66. b.opType = opType
  67. return b
  68. }
  69. func (b *IndexService) Refresh(refresh bool) *IndexService {
  70. b.refresh = &refresh
  71. return b
  72. }
  73. func (b *IndexService) Version(version int64) *IndexService {
  74. b.version = &version
  75. return b
  76. }
  77. // VersionType is either "internal" (default), "external",
  78. // "external_gt", "external_gte", or "force".
  79. // See http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-index_.html#_version_types
  80. // for details.
  81. func (b *IndexService) VersionType(versionType string) *IndexService {
  82. b.versionType = versionType
  83. return b
  84. }
  85. func (b *IndexService) Timestamp(timestamp string) *IndexService {
  86. b.timestamp = timestamp
  87. return b
  88. }
  89. func (b *IndexService) TTL(ttl string) *IndexService {
  90. b.ttl = ttl
  91. return b
  92. }
  93. func (b *IndexService) Timeout(timeout string) *IndexService {
  94. b.timeout = timeout
  95. return b
  96. }
  97. func (b *IndexService) BodyString(body string) *IndexService {
  98. b.bodyString = body
  99. return b
  100. }
  101. func (b *IndexService) BodyJson(json interface{}) *IndexService {
  102. b.bodyJson = json
  103. return b
  104. }
  105. func (b *IndexService) Pretty(pretty bool) *IndexService {
  106. b.pretty = pretty
  107. return b
  108. }
  109. func (b *IndexService) Do() (*IndexResult, error) {
  110. // Build url
  111. var path, method string
  112. if b.id != "" {
  113. // Create document with manual id
  114. method = "PUT"
  115. path = "/{index}/{type}/{id}"
  116. } else {
  117. // Automatic ID generation
  118. // See: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/docs-index_.html#index-creation
  119. method = "POST"
  120. path = "/{index}/{type}/"
  121. }
  122. path, err := uritemplates.Expand(path, map[string]string{
  123. "index": b.index,
  124. "type": b._type,
  125. "id": b.id,
  126. })
  127. if err != nil {
  128. return nil, err
  129. }
  130. // Parameters
  131. params := make(url.Values)
  132. if b.pretty {
  133. params.Set("pretty", "true")
  134. }
  135. if b.routing != "" {
  136. params.Set("routing", b.routing)
  137. }
  138. if b.parent != "" {
  139. params.Set("parent", b.parent)
  140. }
  141. if b.opType != "" {
  142. params.Set("op_type", b.opType)
  143. }
  144. if b.refresh != nil && *b.refresh {
  145. params.Set("refresh", "true")
  146. }
  147. if b.version != nil {
  148. params.Set("version", fmt.Sprintf("%d", *b.version))
  149. }
  150. if b.versionType != "" {
  151. params.Set("version_type", b.versionType)
  152. }
  153. if b.timestamp != "" {
  154. params.Set("timestamp", b.timestamp)
  155. }
  156. if b.ttl != "" {
  157. params.Set("ttl", b.ttl)
  158. }
  159. if b.timeout != "" {
  160. params.Set("timeout", b.timeout)
  161. }
  162. /*
  163. routing string
  164. parent string
  165. opType string
  166. refresh *bool
  167. version *int64
  168. versionType string
  169. timestamp string
  170. ttl string
  171. */
  172. // Body
  173. var body interface{}
  174. if b.bodyJson != nil {
  175. body = b.bodyJson
  176. } else {
  177. body = b.bodyString
  178. }
  179. // Get response
  180. res, err := b.client.PerformRequest(method, path, params, body)
  181. if err != nil {
  182. return nil, err
  183. }
  184. // Return result
  185. ret := new(IndexResult)
  186. if err := json.Unmarshal(res.Body, ret); err != nil {
  187. return nil, err
  188. }
  189. return ret, nil
  190. }