ingest_simulate_pipeline.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // Copyright 2012-present 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. "context"
  7. "encoding/json"
  8. "fmt"
  9. "net/url"
  10. "gopkg.in/olivere/elastic.v5/uritemplates"
  11. )
  12. // IngestSimulatePipelineService executes a specific pipeline against the set of
  13. // documents provided in the body of the request.
  14. //
  15. // The API is documented at
  16. // https://www.elastic.co/guide/en/elasticsearch/reference/5.2/simulate-pipeline-api.html.
  17. type IngestSimulatePipelineService struct {
  18. client *Client
  19. pretty bool
  20. id string
  21. verbose *bool
  22. bodyJson interface{}
  23. bodyString string
  24. }
  25. // NewIngestSimulatePipelineService creates a new IngestSimulatePipeline.
  26. func NewIngestSimulatePipelineService(client *Client) *IngestSimulatePipelineService {
  27. return &IngestSimulatePipelineService{
  28. client: client,
  29. }
  30. }
  31. // Id specifies the pipeline ID.
  32. func (s *IngestSimulatePipelineService) Id(id string) *IngestSimulatePipelineService {
  33. s.id = id
  34. return s
  35. }
  36. // Verbose mode. Display data output for each processor in executed pipeline.
  37. func (s *IngestSimulatePipelineService) Verbose(verbose bool) *IngestSimulatePipelineService {
  38. s.verbose = &verbose
  39. return s
  40. }
  41. // Pretty indicates that the JSON response be indented and human readable.
  42. func (s *IngestSimulatePipelineService) Pretty(pretty bool) *IngestSimulatePipelineService {
  43. s.pretty = pretty
  44. return s
  45. }
  46. // BodyJson is the ingest definition, defined as a JSON-serializable simulate
  47. // definition. Use e.g. a map[string]interface{} here.
  48. func (s *IngestSimulatePipelineService) BodyJson(body interface{}) *IngestSimulatePipelineService {
  49. s.bodyJson = body
  50. return s
  51. }
  52. // BodyString is the simulate definition, defined as a string.
  53. func (s *IngestSimulatePipelineService) BodyString(body string) *IngestSimulatePipelineService {
  54. s.bodyString = body
  55. return s
  56. }
  57. // buildURL builds the URL for the operation.
  58. func (s *IngestSimulatePipelineService) buildURL() (string, url.Values, error) {
  59. var err error
  60. var path string
  61. // Build URL
  62. if s.id != "" {
  63. path, err = uritemplates.Expand("/_ingest/pipeline/{id}/_simulate", map[string]string{
  64. "id": s.id,
  65. })
  66. } else {
  67. path = "/_ingest/pipeline/_simulate"
  68. }
  69. if err != nil {
  70. return "", url.Values{}, err
  71. }
  72. // Add query string parameters
  73. params := url.Values{}
  74. if s.pretty {
  75. params.Set("pretty", "1")
  76. }
  77. if s.verbose != nil {
  78. params.Set("verbose", fmt.Sprintf("%v", *s.verbose))
  79. }
  80. return path, params, nil
  81. }
  82. // Validate checks if the operation is valid.
  83. func (s *IngestSimulatePipelineService) Validate() error {
  84. var invalid []string
  85. if s.bodyString == "" && s.bodyJson == nil {
  86. invalid = append(invalid, "BodyJson")
  87. }
  88. if len(invalid) > 0 {
  89. return fmt.Errorf("missing required fields: %v", invalid)
  90. }
  91. return nil
  92. }
  93. // Do executes the operation.
  94. func (s *IngestSimulatePipelineService) Do(ctx context.Context) (*IngestSimulatePipelineResponse, error) {
  95. // Check pre-conditions
  96. if err := s.Validate(); err != nil {
  97. return nil, err
  98. }
  99. // Get URL for request
  100. path, params, err := s.buildURL()
  101. if err != nil {
  102. return nil, err
  103. }
  104. // Setup HTTP request body
  105. var body interface{}
  106. if s.bodyJson != nil {
  107. body = s.bodyJson
  108. } else {
  109. body = s.bodyString
  110. }
  111. // Get HTTP response
  112. res, err := s.client.PerformRequest(ctx, "POST", path, params, body)
  113. if err != nil {
  114. return nil, err
  115. }
  116. // Return operation response
  117. ret := new(IngestSimulatePipelineResponse)
  118. if err := json.Unmarshal(res.Body, ret); err != nil {
  119. return nil, err
  120. }
  121. return ret, nil
  122. }
  123. // IngestSimulatePipelineResponse is the response of IngestSimulatePipeline.Do.
  124. type IngestSimulatePipelineResponse struct {
  125. Docs []*IngestSimulateDocumentResult `json:"docs"`
  126. }
  127. type IngestSimulateDocumentResult struct {
  128. Doc map[string]interface{} `json:"doc"`
  129. ProcessorResults []*IngestSimulateProcessorResult `json:"processor_results"`
  130. }
  131. type IngestSimulateProcessorResult struct {
  132. ProcessorTag string `json:"tag"`
  133. Doc map[string]interface{} `json:"doc"`
  134. }