Debian Codegraph @

ingest_simulate_pipeline.go

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