diff --git a/src/Plugin/search_api/processor/ContentAccess.php b/src/Plugin/search_api/processor/ContentAccess.php
index 18e7f551..6382665e 100644
--- a/src/Plugin/search_api/processor/ContentAccess.php
+++ b/src/Plugin/search_api/processor/ContentAccess.php
@@ -173,19 +173,18 @@ public function addFieldValues(ItemInterface $item) {
     $fields = $this->getFieldsHelper()
       ->filterForPropertyPath($fields, NULL, 'search_api_node_grants');
     foreach ($fields as $field) {
-      // Collect grant information for the node.
-      if (!$node->access('view', $anonymous_user)) {
-        // If anonymous user has no permission we collect all grants with
-        // their realms in the item.
-        $sql = 'SELECT * FROM {node_access} WHERE (nid = 0 OR nid = :nid) AND grant_view = 1';
-        $args = [':nid' => $node->id()];
-        foreach ($this->getDatabase()->query($sql, $args) as $grant) {
+      // Collect grant records for the node. If there are none, use the pseudo
+      // grant "node_access__all".
+      $sql = 'SELECT gid, realm FROM {node_access} WHERE (nid = 0 OR nid = :nid) AND grant_view = 1';
+      $args = [':nid' => $node->id()];
+      $grant_records = $this->getDatabase()->query($sql, $args)->fetchAll();
+      if ($grant_records) {
+        foreach ($grant_records as $grant) {
           $field->addValue("node_access_{$grant->realm}:{$grant->gid}");
         }
       }
       else {
-        // Add the generic pseudo view grant if we are not using node access
-        // or the node is viewable by anonymous users.
+        // Add the generic pseudo view grant if we are not using node access.
         $field->addValue('node_access__all');
       }
     }
diff --git a/tests/search_api_test/search_api_test.module b/tests/search_api_test/search_api_test.module
index 74c5dc2b..deeb41a7 100644
--- a/tests/search_api_test/search_api_test.module
+++ b/tests/search_api_test/search_api_test.module
@@ -12,7 +12,12 @@
  * Implement hook_node_grants().
  */
 function search_api_test_node_grants(AccountInterface $account, $op) {
-  $grants['search_api_test'] = [$account->id()];
+  $grants = [];
+
+  if (\Drupal::state()->get('search_api_test_add_node_access_grant', TRUE)) {
+    $grants['search_api_test'] = [$account->id()];
+  }
+
   return $grants;
 }
 
@@ -20,14 +25,18 @@ function search_api_test_node_grants(AccountInterface $account, $op) {
  * Implement hook_node_access_records().
  */
 function search_api_test_node_access_records(NodeInterface $node) {
-  $grants[] = [
-    'realm' => 'search_api_test',
-    'gid' => $node->getOwnerId(),
-    'grant_view' => 1,
-    'grant_update' => 0,
-    'grant_delete' => 0,
-    'langcode' => $node->language()->getId(),
-  ];
+  $grants = [];
+
+  if (\Drupal::state()->get('search_api_test_add_node_access_grant', TRUE)) {
+    $grants[] = [
+      'realm' => 'search_api_test',
+      'gid' => $node->getOwnerId(),
+      'grant_view' => 1,
+      'grant_update' => 0,
+      'grant_delete' => 0,
+      'langcode' => $node->language()->getId(),
+    ];
+  }
 
   return $grants;
 }
diff --git a/tests/src/Kernel/Processor/ContentAccessTest.php b/tests/src/Kernel/Processor/ContentAccessTest.php
index 252fc3c7..0d287112 100644
--- a/tests/src/Kernel/Processor/ContentAccessTest.php
+++ b/tests/src/Kernel/Processor/ContentAccessTest.php
@@ -7,6 +7,7 @@
 use Drupal\comment\Entity\CommentType;
 use Drupal\comment\Tests\CommentTestTrait;
 use Drupal\Core\Database\Database;
+use Drupal\Core\Session\AnonymousUserSession;
 use Drupal\Core\TypedData\DataDefinitionInterface;
 use Drupal\node\Entity\Node;
 use Drupal\node\Entity\NodeType;
@@ -241,6 +242,17 @@ public function testQueryAccessWithNodeGrants() {
    * Tests comment indexing when all users have access to content.
    */
   public function testContentAccessAll() {
+    // Deactivate our custom grant and re-save the grant records.
+    \Drupal::state()->set('search_api_test_add_node_access_grant', FALSE);
+    /** @var \Drupal\node\NodeAccessControlHandlerInterface $access_control_handler */
+    $access_control_handler = \Drupal::entityTypeManager()
+      ->getAccessControlHandler('node');
+    $grants_storage = \Drupal::getContainer()->get('node.grant_storage');
+    foreach ($this->nodes as $node) {
+      $grants = $access_control_handler->acquireGrants($node);
+      $grants_storage->write($node, $grants);
+    }
+
     user_role_grant_permissions('anonymous', ['access content', 'access comments']);
     $items = [];
     foreach ($this->comments as $comment) {
@@ -258,9 +270,15 @@ public function testContentAccessAll() {
       $this->processor->addFieldValues($item);
     }
 
+    // Verify all items were indexed with the same "all" realm grant.
+    $all = ['node_access_all:0'];
     foreach ($items as $item) {
-      $this->assertEquals(['node_access__all'], $item->getField('node_grants')->getValues());
+      $this->assertEquals($all, $item->getField('node_grants')->getValues());
     }
+
+    // Verify that the anonymous user has exactly that grant.
+    $grants = node_access_grants('view', new AnonymousUserSession());
+    $this->assertEquals(['all' => [0]], $grants);
   }
 
   /**
