1 | <?php |
||
9 | class Type |
||
10 | { |
||
11 | /** |
||
12 | * @var string |
||
13 | */ |
||
14 | private $name; |
||
15 | |||
16 | /** |
||
17 | * @var Index |
||
18 | */ |
||
19 | private $index; |
||
20 | |||
21 | /** |
||
22 | * @var array|null |
||
23 | */ |
||
24 | private $mappings; |
||
25 | |||
26 | /** |
||
27 | * @param string $name |
||
28 | * @param Index $index |
||
29 | * @param array $mappings |
||
30 | */ |
||
31 | public function __construct($name, Index $index, array $mappings = null) |
||
32 | { |
||
33 | $this->name = $name; |
||
34 | $this->index = $index; |
||
35 | $this->mappings = $mappings; |
||
36 | } |
||
37 | |||
38 | /** |
||
39 | * @return string |
||
40 | */ |
||
41 | protected function getIndexName() |
||
45 | |||
46 | /** |
||
47 | * @return string |
||
48 | */ |
||
49 | protected function getIndexId() |
||
53 | |||
54 | /** |
||
55 | * Creates client request params appending options that define the index and type. |
||
56 | * |
||
57 | * @param array $params |
||
58 | * @return array |
||
59 | */ |
||
60 | protected function createRequestParams(array $params = []) |
||
61 | { |
||
62 | return array_merge([ |
||
63 | 'index' => $this->getIndexName(), |
||
64 | 'type' => $this->getName(), |
||
65 | ], $params); |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * @return Client |
||
70 | */ |
||
71 | public function getClient() |
||
75 | |||
76 | /** |
||
77 | * Checks if the index exists in ES. |
||
78 | * |
||
79 | * @return bool |
||
80 | */ |
||
81 | public function exists() |
||
82 | { |
||
83 | return $this->getClient()->indices()->exists( |
||
84 | $this->createRequestParams() |
||
85 | ); |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * Creates the type mapping in ES. |
||
90 | */ |
||
91 | public function createMappings() |
||
92 | { |
||
93 | if (empty($this->mappings)) { |
||
94 | return; |
||
95 | } |
||
96 | |||
97 | $this->getClient()->indices()->putMapping( |
||
98 | $this->createRequestParams([ |
||
99 | 'body' => [$this->name => $this->mappings] |
||
100 | ]) |
||
101 | ); |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * Deletes the type mappings in ES. |
||
106 | */ |
||
107 | public function deleteMappings() |
||
108 | { |
||
109 | $this->getClient()->indices()->deleteMapping( |
||
110 | $this->createRequestParams() |
||
111 | ); |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * Resets the type mappings in ES. |
||
116 | */ |
||
117 | public function reset() |
||
118 | { |
||
119 | $this->deleteMappings(); |
||
120 | $this->createMappings(); |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * @return array |
||
125 | */ |
||
126 | public function getMappings() |
||
130 | |||
131 | /** |
||
132 | * @return string |
||
133 | */ |
||
134 | public function getName() |
||
138 | |||
139 | /** |
||
140 | * @return Index |
||
141 | */ |
||
142 | public function getIndex() |
||
146 | |||
147 | /** |
||
148 | * @param Document $document |
||
149 | */ |
||
150 | public function putDocument(Document $document) |
||
154 | |||
155 | /** |
||
156 | * @param string|int $id |
||
157 | */ |
||
158 | public function deleteDocument($id) |
||
162 | |||
163 | /** |
||
164 | * @param array $documents |
||
165 | */ |
||
166 | public function putDocuments(array $documents) |
||
170 | |||
171 | /** |
||
172 | * @param array $ids |
||
173 | */ |
||
174 | public function deleteDocuments(array $ids) |
||
178 | |||
179 | /** |
||
180 | * @param array|string $query Array that will be serialized or raw JSON. |
||
181 | * @param array $options |
||
182 | * @return array |
||
183 | */ |
||
184 | public function search($query, array $options = []) |
||
188 | |||
189 | /** |
||
190 | * @param array|string $query Array that will be serialized or raw JSON. |
||
191 | * @param array $options |
||
192 | * @return int|null |
||
193 | */ |
||
194 | public function count($query, array $options = []) |
||
198 | |||
199 | /** |
||
200 | * @param array|string $query |
||
201 | * @param array $options |
||
202 | */ |
||
203 | public function deleteByQuery($query, array $options = []) |
||
207 | } |
||
208 |