You are here

public function SearchApiSolrBackend::deleteItems in Search API Solr 4.x

Same name and namespace in other branches
  1. 8.3 src/Plugin/search_api/backend/SearchApiSolrBackend.php \Drupal\search_api_solr\Plugin\search_api\backend\SearchApiSolrBackend::deleteItems()
  2. 8 src/Plugin/search_api/backend/SearchApiSolrBackend.php \Drupal\search_api_solr\Plugin\search_api\backend\SearchApiSolrBackend::deleteItems()
  3. 8.2 src/Plugin/search_api/backend/SearchApiSolrBackend.php \Drupal\search_api_solr\Plugin\search_api\backend\SearchApiSolrBackend::deleteItems()

Throws

\Drupal\Component\Plugin\Exception\PluginException

\Drupal\search_api\SearchApiException

File

src/Plugin/search_api/backend/SearchApiSolrBackend.php, line 1239

Class

SearchApiSolrBackend
Apache Solr backend for search api.

Namespace

Drupal\search_api_solr\Plugin\search_api\backend

Code

public function deleteItems(IndexInterface $index, array $ids) {
  try {
    $index_id = $this
      ->getTargetedIndexId($index);
    $site_hash = $this
      ->getTargetedSiteHash($index);
    $solr_ids = [];
    foreach ($ids as $id) {
      $solr_ids[] = $this
        ->createId($site_hash, $index_id, $id);
    }
    $connector = $this
      ->getSolrConnector();
    $update_query = $connector
      ->getUpdateQuery();

    // Delete documents by IDs (this would cover the case if this index
    // contains stand-alone (childless) documents). And then also delete by
    // _root_ field which assures children (nested) documents will be removed
    // too. The field _root_ is assigned to the ID of the top-level document
    // across an entire block of parent + children.
    $update_query
      ->addDeleteQuery('_root_:("' . implode('" OR "', $solr_ids) . '")');
    $update_query
      ->addDeleteByIds($solr_ids);
    $connector
      ->update($update_query, $this
      ->getCollectionEndpoint($index));
    \Drupal::state()
      ->set('search_api_solr.' . $index
      ->id() . '.last_update', \Drupal::time()
      ->getCurrentTime());
  } catch (ExceptionInterface $e) {
    throw new SearchApiSolrException($e
      ->getMessage(), $e
      ->getCode(), $e);
  }
}