I have read and tried https://www.drupal.org/docs/8/modules/search-api/developer-documentation/executing-a-search-in-code
I am using drupal 8 and using the default content index for testing. I am able to run the following code and I get a result count, but not result items.
$index = \Drupal\search_api\Entity\Index::load('default_index');
$query = $index->query();
// Change the parse mode for the search.
$parse_mode = \Drupal::service('plugin.manager.search_api.parse_mode')
->createInstance('direct');
$parse_mode->setConjunction('OR');
$query->setParseMode($parse_mode);
// Set fulltext search keywords and fields.
$query->keys('');
$query->setFulltextFields(['title', 'name', 'body']);
// Do paging.
$query->range(20, 10);
// Set one or more tags for the query.
// @see hook_search_api_query_TAG_alter()
// @see hook_search_api_results_TAG_alter()
$query->addTag('custom_search');
// Execute the search.
$results = $query->execute();
echo "Result count: {$results->getResultCount()}\n";
$ids = implode(', ', array_keys($results->getResultItems()));
echo "Returned IDs: $ids.\n";
Count shows a value but I can't get any result items. I tried with $query->keys(''); $query->keys('*'); and some value $query->keys('some value'); In all cases the count seems to be correct. The search therefore seems to be executed correctly, but how can I retrieve the items?