public function CommandHelper::searchIndexCommand in Search API 8
Returns an array of results.
Parameters
string $indexId: The index to search in.
string|null $keyword: (optional) The word to search for.
Return value
array An array of results, each of which is represented by an associative array with the following keys:
- id: The internal ID of the item.
- label: The label of the item, or NULL if it could not be determined.
Throws
\Drupal\search_api\ConsoleException Thrown if searching failed for any reason.
\Drupal\search_api\SearchApiException Thrown if no search query could be created for the given index, for example because it is disabled or its server could not be loaded.
File
- src/
Utility/ CommandHelper.php, line 472
Class
- CommandHelper
- Provides functionality to be used by CLI tools.
Namespace
Drupal\search_api\UtilityCode
public function searchIndexCommand($indexId, $keyword = NULL) {
$indexes = $this
->loadIndexes([
$indexId,
]);
if (empty($indexes[$indexId])) {
throw new ConsoleException($this
->t('@index was not found'));
}
$query = $indexes[$indexId]
->query();
if ($keyword !== NULL) {
$query
->keys($keyword);
}
$query
->range(0, 10);
try {
$results = $query
->execute();
} catch (SearchApiException $e) {
throw new ConsoleException($e
->getMessage(), 0, $e);
}
$rows = [];
foreach ($results
->getResultItems() as $item) {
try {
$label = $item
->getDatasource()
->getItemLabel($item
->getOriginalObject());
} catch (SearchApiException $e) {
$label = NULL;
}
$rows[] = [
'id' => $item
->getId(),
'label' => $label,
];
}
return $rows;
}