| @@ 119-153 (lines=35) @@ | ||
| 116 | /** |
|
| 117 | * @throws \Longman\LaravelLodash\Elasticsearch\ElasticsearchException |
|
| 118 | */ |
|
| 119 | public function addDocumentsToIndex(string $indexName, string $typeName, array $items) |
|
| 120 | { |
|
| 121 | if (! $this->isEnabled()) { |
|
| 122 | return; |
|
| 123 | } |
|
| 124 | ||
| 125 | $params = [ |
|
| 126 | 'body' => [], |
|
| 127 | ]; |
|
| 128 | ||
| 129 | if (! empty($this->timeout)) { |
|
| 130 | $params['client'] = [ |
|
| 131 | 'timeout' => $this->timeout, |
|
| 132 | ]; |
|
| 133 | } |
|
| 134 | ||
| 135 | foreach ($items as $id => $item) { |
|
| 136 | $params['body'][] = [ |
|
| 137 | 'create' => [ |
|
| 138 | '_index' => $indexName, |
|
| 139 | '_type' => $typeName, |
|
| 140 | '_id' => $id, |
|
| 141 | ], |
|
| 142 | ]; |
|
| 143 | ||
| 144 | $params['body'][] = $item; |
|
| 145 | } |
|
| 146 | ||
| 147 | $responses = $this->client->bulk($params); |
|
| 148 | if ($responses['errors'] !== true) { |
|
| 149 | return; |
|
| 150 | } |
|
| 151 | ||
| 152 | $this->handleBulkError($responses, 'Error occurred during bulk create'); |
|
| 153 | } |
|
| 154 | ||
| 155 | /** |
|
| 156 | * @throws \Longman\LaravelLodash\Elasticsearch\ElasticsearchException |
|
| @@ 158-192 (lines=35) @@ | ||
| 155 | /** |
|
| 156 | * @throws \Longman\LaravelLodash\Elasticsearch\ElasticsearchException |
|
| 157 | */ |
|
| 158 | public function updateDocumentsInIndex(string $indexName, string $typeName, array $items) |
|
| 159 | { |
|
| 160 | if (! $this->isEnabled()) { |
|
| 161 | return; |
|
| 162 | } |
|
| 163 | ||
| 164 | $params = [ |
|
| 165 | 'body' => [], |
|
| 166 | ]; |
|
| 167 | ||
| 168 | if (! empty($this->timeout)) { |
|
| 169 | $params['client'] = [ |
|
| 170 | 'timeout' => $this->timeout, |
|
| 171 | ]; |
|
| 172 | } |
|
| 173 | ||
| 174 | foreach ($items as $id => $item) { |
|
| 175 | $params['body'][] = [ |
|
| 176 | 'update' => [ |
|
| 177 | '_index' => $indexName, |
|
| 178 | '_type' => $typeName, |
|
| 179 | '_id' => $id, |
|
| 180 | ], |
|
| 181 | ]; |
|
| 182 | ||
| 183 | $params['body'][] = ['doc' => $item]; |
|
| 184 | } |
|
| 185 | ||
| 186 | $responses = $this->client->bulk($params); |
|
| 187 | if ($responses['errors'] !== true) { |
|
| 188 | return; |
|
| 189 | } |
|
| 190 | ||
| 191 | $this->handleBulkError($responses, 'Error occurred during bulk update'); |
|
| 192 | } |
|
| 193 | ||
| 194 | /** |
|
| 195 | * @throws \Longman\LaravelLodash\Elasticsearch\ElasticsearchException |
|
| @@ 197-231 (lines=35) @@ | ||
| 194 | /** |
|
| 195 | * @throws \Longman\LaravelLodash\Elasticsearch\ElasticsearchException |
|
| 196 | */ |
|
| 197 | public function addOrUpdateDocumentsInIndex(string $indexName, string $typeName, array $items) |
|
| 198 | { |
|
| 199 | if (! $this->isEnabled()) { |
|
| 200 | return; |
|
| 201 | } |
|
| 202 | ||
| 203 | $params = [ |
|
| 204 | 'body' => [], |
|
| 205 | ]; |
|
| 206 | ||
| 207 | if (! empty($this->timeout)) { |
|
| 208 | $params['client'] = [ |
|
| 209 | 'timeout' => $this->timeout, |
|
| 210 | ]; |
|
| 211 | } |
|
| 212 | ||
| 213 | foreach ($items as $id => $item) { |
|
| 214 | $params['body'][] = [ |
|
| 215 | 'index' => [ |
|
| 216 | '_index' => $indexName, |
|
| 217 | '_type' => $typeName, |
|
| 218 | '_id' => $id, |
|
| 219 | ], |
|
| 220 | ]; |
|
| 221 | ||
| 222 | $params['body'][] = $item; |
|
| 223 | } |
|
| 224 | ||
| 225 | $responses = $this->client->bulk($params); |
|
| 226 | if ($responses['errors'] !== true) { |
|
| 227 | return; |
|
| 228 | } |
|
| 229 | ||
| 230 | $this->handleBulkError($responses, 'Error occurred during bulk index'); |
|
| 231 | } |
|
| 232 | ||
| 233 | public function refreshIndex(string $indexName): void |
|
| 234 | { |
|