protected function SearchApiElasticsearchBackend::addSearchFacets 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::addSearchFacets()
- 8 src/Plugin/search_api/backend/SearchApiElasticsearchBackend.php \Drupal\elasticsearch_connector\Plugin\search_api\backend\SearchApiElasticsearchBackend::addSearchFacets()
- 8.2 src/Plugin/search_api/backend/SearchApiElasticsearchBackend.php \Drupal\elasticsearch_connector\Plugin\search_api\backend\SearchApiElasticsearchBackend::addSearchFacets()
- 8.5 src/Plugin/search_api/backend/SearchApiElasticsearchBackend.php \Drupal\elasticsearch_connector\Plugin\search_api\backend\SearchApiElasticsearchBackend::addSearchFacets()
Helper function build facets in search.
Parameters
array $params: Array of parameters to be sent in the body of a _search endpoint Elasticsearch request.
\Drupal\search_api\Query\QueryInterface $query: Search API query object.
File
- src/
Plugin/ search_api/ backend/ SearchApiElasticsearchBackend.php, line 847
Class
- SearchApiElasticsearchBackend
- Elasticsearch Search API Backend definition.
Namespace
Drupal\elasticsearch_connector\Plugin\search_api\backendCode
protected function addSearchFacets(array &$params, QueryInterface $query) {
// Search API facets.
$facets = $query
->getOption('search_api_facets');
$index_fields = $this
->getIndexFields($query);
if (!empty($facets)) {
// Loop through facets.
foreach ($facets as $facet_id => $facet_info) {
$field_id = $facet_info['field'];
$facet = [
$field_id => [],
];
// Skip if not recognized as a known field.
if (!isset($index_fields[$field_id])) {
continue;
}
// TODO: missing function reference.
$field_type = search_api_extract_inner_type($index_fields[$field_id]['type']);
// TODO: handle different types (GeoDistance and so on). See the
// supportedFeatures todo.
if ($field_type === 'date') {
$facet_type = 'date_histogram';
$facet[$field_id] = $this
->createDateFieldFacet($field_id, $facet);
}
else {
$facet_type = 'terms';
$facet[$field_id][$facet_type]['all_terms'] = (bool) $facet_info['missing'];
}
// Add the facet.
if (!empty($facet[$field_id])) {
// Add facet options.
$facet_info['facet_type'] = $facet_type;
$facet[$field_id] = $this
->addFacetOptions($facet[$field_id], $query, $facet_info);
}
$params['body']['facets'][$field_id] = $facet[$field_id];
}
}
}