diff --git a/modules/tide_search/src/Plugin/search_api/backend/TideElasticsearchBackend.php b/modules/tide_search/src/Plugin/search_api/backend/TideElasticsearchBackend.php index 0fe230c9..5aa40fb1 100644 --- a/modules/tide_search/src/Plugin/search_api/backend/TideElasticsearchBackend.php +++ b/modules/tide_search/src/Plugin/search_api/backend/TideElasticsearchBackend.php @@ -4,6 +4,8 @@ use Drupal\Core\Form\FormStateInterface; use Drupal\elasticsearch_connector\Plugin\search_api\backend\SearchApiElasticsearchBackend; +use Drupal\search_api\IndexInterface; +use Elasticsearch\Common\Exceptions\ElasticsearchException; /** * Custom Elasticsearch Search API Backend definition. @@ -63,4 +65,86 @@ public function getNumberOfShards() { return (int) $this->configuration['number_of_shards']; } + /** + * {@inheritdoc} + */ + public function indexItems(IndexInterface $index, array $items) { + if (empty($items)) { + return []; + } + + // Static variable to track logged errors. + static $logged_errors = []; + if (count($logged_errors) > 5000) { + $logged_errors = []; + } + + $successfully_indexed_ids = []; + + try { + $params = $this->indexFactory->bulkIndex($index, $items); + // Ensure params is valid before sending. + if (empty($params)) { + return []; + } + + $response = $this->client->bulk($params); + + if (isset($response['items']) && is_array($response['items'])) { + foreach ($response['items'] as $item_response) { + $operation = key($item_response); + $result = $item_response[$operation] ?? []; + $id = $result['_id'] ?? NULL; + $status = $result['status'] ?? 0; + if ($status >= 200 && $status < 300) { + if ($id) { + $successfully_indexed_ids[] = $id; + } + } + else { + // Determine a static key for the error log. If ID is missing, + // use a generic key to avoid flooding logs with "Unknown ID". + $log_key = $id ? (string) $id : 'unknown_id_error'; + + if (!isset($logged_errors[$log_key])) { + $error_data = $result['error'] ?? 'Unknown error'; + + if (is_array($error_data)) { + $error_reason = $error_data['reason'] ?? 'Unknown reason'; + $caused_by = $error_data['caused_by']['reason'] ?? ''; + $error_type = $error_data['type'] ?? 'Unknown type'; + } + else { + // If it's a string, treat the whole thing as the reason. + $error_reason = (string) $error_data; + $caused_by = ''; + $error_type = 'String Error Format'; + } + + $this->logger->error('Failed to index item %id. Type: %type, Reason: %reason, Caused by: %caused_by', [ + '%id' => $id ?? 'NULL', + '%type' => $error_type, + '%reason' => $error_reason, + '%caused_by' => $caused_by, + ]); + + // Mark as logged. + $logged_errors[$log_key] = TRUE; + } + } + } + } + } + catch (ElasticsearchException $e) { + $this->logger->error('Elasticsearch error: @message', ['@message' => $e->getMessage()]); + return []; + } + catch (\Exception $e) { + $this->logger->error('Unexpected error during indexing: @message', ['@message' => $e->getMessage()]); + return []; + } + + return $successfully_indexed_ids; + } + }