doc.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Licensed to Elasticsearch B.V. under one or more contributor
  2. // license agreements. See the NOTICE file distributed with
  3. // this work for additional information regarding copyright
  4. // ownership. Elasticsearch B.V. licenses this file to you under
  5. // the Apache License, Version 2.0 (the "License"); you may
  6. // not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing,
  12. // software distributed under the License is distributed on an
  13. // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. // KIND, either express or implied. See the License for the
  15. // specific language governing permissions and limitations
  16. // under the License.
  17. /*
  18. Package esapi provides the Go API for Elasticsearch.
  19. It is automatically included in the client provided by the
  20. github.com/elastic/go-elasticsearch package:
  21. es, _ := elasticsearch.NewDefaultClient()
  22. res, _ := es.Info()
  23. log.Println(res)
  24. For each Elasticsearch API, such as "Index", the package exports two corresponding types:
  25. a function and a struct.
  26. The function type allows you to call the Elasticsearch API as a method on the client,
  27. passing the parameters as arguments:
  28. res, err := es.Index(
  29. "test", // Index name
  30. strings.NewReader(`{"title" : "Test"}`), // Document body
  31. es.Index.WithDocumentID("1"), // Document ID
  32. es.Index.WithRefresh("true"), // Refresh
  33. )
  34. if err != nil {
  35. log.Fatalf("ERROR: %s", err)
  36. }
  37. defer res.Body.Close()
  38. log.Println(res)
  39. // => [201 Created] {"_index":"test","_type":"_doc","_id":"1" ...
  40. The struct type allows for a more hands-on approach, where you create a new struct, with the
  41. request configuration as fields, and call the Do() method
  42. with a context and the client as arguments:
  43. req := esapi.IndexRequest{
  44. Index: "test", // Index name
  45. Body: strings.NewReader(`{"title" : "Test"}`), // Document body
  46. DocumentID: "1", // Document ID
  47. Refresh: "true", // Refresh
  48. }
  49. res, err := req.Do(context.Background(), es)
  50. if err != nil {
  51. log.Fatalf("Error getting response: %s", err)
  52. }
  53. defer res.Body.Close()
  54. log.Println(res)
  55. // => [200 OK] {"_index":"test","_type":"_doc","_id":"1","_version":2 ...
  56. The function type is a wrapper around the struct, and allows
  57. to configure and perform the request in a more expressive way.
  58. It has a minor overhead compared to using a struct directly;
  59. refer to the esapi_benchmark_test.go suite for concrete numbers.
  60. See the documentation for each API function or struct at
  61. https://godoc.org/github.com/elastic/go-elasticsearch,
  62. or locally by:
  63. go doc github.com/elastic/go-elasticsearch/v7/esapi Index
  64. go doc github.com/elastic/go-elasticsearch/v7/esapi IndexRequest
  65. Response
  66. The esapi.Response type is a lightweight wrapper around http.Response.
  67. The res.Body field is an io.ReadCloser, leaving the JSON parsing to the
  68. calling code, in the interest of performance and flexibility
  69. (eg. to allow using a custom JSON parser).
  70. It is imperative to close the response body for a non-nil response.
  71. The Response type implements a couple of convenience methods for accessing
  72. the status, checking an error status code or printing
  73. the response body for debugging purposes.
  74. Additional Information
  75. See the Elasticsearch documentation at
  76. https://www.elastic.co/guide/en/elasticsearch/reference/master/api-conventions.html for detailed information
  77. about the API endpoints and parameters.
  78. The Go API is generated from the Elasticsearch JSON specification at
  79. https://github.com/elastic/elasticsearch/tree/master/rest-api-spec/src/main/resources/rest-api-spec/api
  80. by the internal package available at
  81. https://github.com/elastic/go-elasticsearch/tree/master/internal/cmd/generate/commands/gensource.
  82. The API is tested by integration tests common to all Elasticsearch official clients, generated from the
  83. source at https://github.com/elastic/elasticsearch/tree/master/rest-api-spec/src/main/resources/rest-api-spec/test.
  84. The generator is provided by the internal package available at internal/cmd/generate/commands/gentests.
  85. */
  86. package esapi