protected function SearchApiElasticsearchBackend::parseFacets in Elasticsearch Connector 8.6
Same name and namespace in other branches
- 8.7 src/Plugin/search_api/backend/SearchApiElasticsearchBackend.php \Drupal\elasticsearch_connector\Plugin\search_api\backend\SearchApiElasticsearchBackend::parseFacets()
- 8.5 src/Plugin/search_api/backend/SearchApiElasticsearchBackend.php \Drupal\elasticsearch_connector\Plugin\search_api\backend\SearchApiElasticsearchBackend::parseFacets()
Parse the result set and add the facet values.
Parameters
\Drupal\search_api\Query\ResultSet $results: Result set, all items matched in a search.
\Drupal\search_api\Query\QueryInterface $query: Search API query object.
1 call to SearchApiElasticsearchBackend::parseFacets()
- SearchApiElasticsearchBackend::search in src/
Plugin/ search_api/ backend/ SearchApiElasticsearchBackend.php - Executes a search on this server.
File
- src/
Plugin/ search_api/ backend/ SearchApiElasticsearchBackend.php, line 706
Class
- SearchApiElasticsearchBackend
- Elasticsearch Search API Backend definition.
Namespace
Drupal\elasticsearch_connector\Plugin\search_api\backendCode
protected function parseFacets(ResultSet $results, QueryInterface $query) {
$response = $results
->getExtraData('elasticsearch_response');
$facets = $query
->getOption('search_api_facets');
// Create an empty array that will be attached to the result object.
$attach = [];
foreach ($facets as $key => $facet) {
$terms = [];
// Handle 'and' operator.
if ($facet['operator'] == 'and' || $facet['operator'] == 'or' && !isset($response['aggregations'][$key . '_global'])) {
if (!empty($facet['type']) && $facet['type'] == 'stats') {
$terms = $response['aggregations'][$key];
}
else {
$buckets = $response['aggregations'][$key]['buckets'];
array_walk($buckets, function ($value) use (&$terms) {
$terms[] = [
'count' => $value['doc_count'],
'filter' => '"' . $value['key'] . '"',
];
});
}
}
elseif ($facet['operator'] == 'or') {
if (!empty($facet['type']) && $facet['type'] == 'stats') {
$terms = $response['aggregations'][$key . '_global'];
}
else {
$buckets = $response['aggregations'][$key . '_global'][$key]['buckets'];
array_walk($buckets, function ($value) use (&$terms) {
$terms[] = [
'count' => $value['doc_count'],
'filter' => '"' . $value['key'] . '"',
];
});
}
}
$attach[$key] = $terms;
}
$results
->setExtraData('search_api_facets', $attach);
}