|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Elastica; |
|
4
|
|
|
|
|
5
|
|
|
use Elastica\Exception\InvalidException; |
|
6
|
|
|
use Elastica\Processor\AbstractProcessor; |
|
7
|
|
|
use Elasticsearch\Endpoints\AbstractEndpoint; |
|
8
|
|
|
use Elasticsearch\Endpoints\Ingest\Pipeline\Delete; |
|
9
|
|
|
use Elasticsearch\Endpoints\Ingest\Pipeline\Get; |
|
10
|
|
|
use Elasticsearch\Endpoints\Ingest\Pipeline\Put; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Elastica Pipeline object. |
|
14
|
|
|
* |
|
15
|
|
|
* Handles Pipeline management & definition. |
|
16
|
|
|
* |
|
17
|
|
|
* @author Federico Panini <[email protected]> |
|
18
|
|
|
* |
|
19
|
|
|
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/ingest-processors.html |
|
20
|
|
|
*/ |
|
21
|
|
|
class Pipeline extends Param |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $id; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var Client Client object |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $_client; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var AbstractProcessor[] |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $_processors = []; |
|
37
|
|
|
|
|
38
|
|
|
public function __construct(Client $client) |
|
39
|
|
|
{ |
|
40
|
|
|
$this->_client = $client; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Create a Pipeline. |
|
45
|
|
|
* |
|
46
|
|
|
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/put-pipeline-api.html |
|
47
|
|
|
*/ |
|
48
|
|
|
public function create(): Response |
|
49
|
|
|
{ |
|
50
|
|
|
if (empty($this->id)) { |
|
51
|
|
|
throw new InvalidException('You should set a valid pipeline id'); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
if (empty($this->_params['description'])) { |
|
55
|
|
|
throw new InvalidException('You should set a valid processor description.'); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
if (empty($this->_processors['processors'])) { |
|
59
|
|
|
throw new InvalidException('You should set a valid processor of type Elastica\Processor\AbstractProcessor.'); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
$endpoint = new Put(); |
|
63
|
|
|
$endpoint->setID($this->id); |
|
64
|
|
|
$endpoint->setBody($this->toArray()); |
|
65
|
|
|
|
|
66
|
|
|
return $this->requestEndpoint($endpoint); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Get a Pipeline Object. |
|
71
|
|
|
* |
|
72
|
|
|
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/get-pipeline-api.html |
|
73
|
|
|
*/ |
|
74
|
|
|
public function getPipeline(string $id): Response |
|
75
|
|
|
{ |
|
76
|
|
|
$endpoint = new Get(); |
|
77
|
|
|
$endpoint->setID($id); |
|
78
|
|
|
|
|
79
|
|
|
return $this->requestEndpoint($endpoint); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Delete a Pipeline. |
|
84
|
|
|
* |
|
85
|
|
|
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/delete-pipeline-api.html |
|
86
|
|
|
*/ |
|
87
|
|
|
public function deletePipeline(string $id): Response |
|
88
|
|
|
{ |
|
89
|
|
|
$endpoint = new Delete(); |
|
90
|
|
|
$endpoint->setID($id); |
|
91
|
|
|
|
|
92
|
|
|
return $this->requestEndpoint($endpoint); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Sets query as raw array. Will overwrite all already set arguments. |
|
97
|
|
|
* |
|
98
|
|
|
* @param array $processors array |
|
99
|
|
|
*/ |
|
100
|
|
|
public function setRawProcessors(array $processors): self |
|
101
|
|
|
{ |
|
102
|
|
|
$this->_processors = $processors; |
|
103
|
|
|
|
|
104
|
|
|
return $this; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
public function addProcessor(AbstractProcessor $processor): self |
|
108
|
|
|
{ |
|
109
|
|
|
if (empty($this->_processors)) { |
|
110
|
|
|
$this->_processors['processors'] = $processor->toArray(); |
|
111
|
|
|
$this->_params['processors'] = []; |
|
112
|
|
|
} else { |
|
113
|
|
|
$this->_processors['processors'] = \array_merge($this->_processors['processors'], $processor->toArray()); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
return $this; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
public function setId(string $id): self |
|
120
|
|
|
{ |
|
121
|
|
|
$this->id = $id; |
|
122
|
|
|
|
|
123
|
|
|
return $this; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @param AbstractProcessor[] |
|
128
|
|
|
*/ |
|
129
|
|
|
public function setProcessors(array $processors): self |
|
130
|
|
|
{ |
|
131
|
|
|
$this->setParam('processors', [$processors]); |
|
132
|
|
|
|
|
133
|
|
|
return $this; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
public function setDescription(string $description): self |
|
137
|
|
|
{ |
|
138
|
|
|
$this->setParam('description', $description); |
|
139
|
|
|
|
|
140
|
|
|
return $this; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Converts the params to an array. A default implementation exist to create |
|
145
|
|
|
* the an array out of the class name (last part of the class name) |
|
146
|
|
|
* and the params. |
|
147
|
|
|
*/ |
|
148
|
|
|
public function toArray(): array |
|
149
|
|
|
{ |
|
150
|
|
|
$this->_params['processors'] = [$this->_processors['processors']]; |
|
151
|
|
|
|
|
152
|
|
|
return $this->getParams(); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
public function getClient(): Client |
|
156
|
|
|
{ |
|
157
|
|
|
return $this->_client; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Makes calls to the elasticsearch server with usage official client Endpoint based on this index. |
|
162
|
|
|
*/ |
|
163
|
|
|
public function requestEndpoint(AbstractEndpoint $endpoint): Response |
|
164
|
|
|
{ |
|
165
|
|
|
$cloned = clone $endpoint; |
|
166
|
|
|
|
|
167
|
|
|
return $this->getClient()->requestEndpoint($cloned); |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
|