protected function SearchApiElasticsearchBackend::addSearchFacets in Elasticsearch Connector 8
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.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()
- 8.6 src/Plugin/search_api/backend/SearchApiElasticsearchBackend.php \Drupal\elasticsearch_connector\Plugin\search_api\backend\SearchApiElasticsearchBackend::addSearchFacets()
Helper function build facets in search.
File
- src/
Plugin/ search_api/ backend/ SearchApiElasticsearchBackend.php, line 1185 - Contains the SearchApiElasticsearchBackend object.
Class
- SearchApiElasticsearchBackend
- Plugin annotation @SearchApiBackend( id = "elasticsearch", label = @Translation("Elasticsearch"), description = @Translation("Index items using an Elasticsearch server.") )
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 trough facets.
$elasticsearch_facets = array();
foreach ($facets as $facet_id => $facet_info) {
$field_id = $facet_info['field'];
$facet = array(
$field_id => array(),
);
// Skip if not recognized as a known field.
if (!isset($index_fields[$field_id])) {
continue;
}
$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];
}
}
}