public function search($db = null)
{
$command = $this->createCommand($db);
$dataReader = $command->query();
$rawRows = $dataReader->readAll();
$facets = [];
foreach ($this->facets as $facetKey => $facetValue) {
$dataReader->nextResult();
$rawFacetResults = $dataReader->readAll();
if (is_numeric($facetKey)) {
$facet = ['name' => $facetValue, 'value' => $facetValue, 'count' => 'count(*)'];
} else {
$facet = array_merge(['name' => $facetKey, 'value' => $facetKey, 'count' => 'count(*)'], $facetValue);
}
foreach ($rawFacetResults as $rawFacetResult) {
$rawFacetResult['value'] = $rawFacetResult[strtolower($facet['value'])];
$rawFacetResult['count'] = $rawFacetResult[$facet['count']];
$facets[$facet['name']][] = $rawFacetResult;
}
}
$meta = [];
if (!empty($this->showMeta)) {
$dataReader->nextResult();
$rawMetaResults = $dataReader->readAll();
foreach ($rawMetaResults as $rawMetaResult) {
$meta[$rawMetaResult['Variable_name']] = $rawMetaResult['Value'];
}
}
// rows should be populated after all data read from cursor, avoiding possible 'unbuffered query' error
$rows = $this->populate($rawRows);
return ['hits' => $rows, 'facets' => $facets, 'meta' => $meta];
}