diff --git a/src/Plugin/facets/query_type/SearchApiString.php b/src/Plugin/facets/query_type/SearchApiString.php index e44b037..eb2a484 100644 --- a/src/Plugin/facets/query_type/SearchApiString.php +++ b/src/Plugin/facets/query_type/SearchApiString.php @@ -41,26 +41,33 @@ class SearchApiString extends QueryTypePluginBase { public function execute() { $query = $this->query; - $unfiltered_results = array(); + $unfiltered_results = []; - // Alter the query here. + // Only alter the query when there's an actual query object to alter. if (!empty($query)) { $operator = $this->facet->getQueryOperator(); $field_identifier = $this->facet->getFieldIdentifier(); - $query2 = $query; - $options2 = &$query2->getOptions(); - $options2['search_api_facets'][$field_identifier] = array( + // Copy the query object so we can do an unfiltered query. We need to have + // this unfiltered results to make sure that the count of a facet is + // correct. The unfiltered results get returned to the facet manager, the + // facet manager will save it on facet::unfiltered_results. + $unfiltered_query = $query; + $unfiltered_options = &$unfiltered_query->getOptions(); + $unfiltered_options['search_api_facets'][$field_identifier] = array( 'field' => $field_identifier, 'limit' => 50, 'operator' => 'and', 'min_count' => 0, 'missing' => FALSE, ); - $unfiltered_results = $query2->execute()->getExtraData('search_api_facets'); + $unfiltered_results = $unfiltered_query + ->execute() + ->getExtraData('search_api_facets'); - $options = &$query->getOptions(); + // Set the options for the actual query. + $options = &$query->getOptions(); $options['search_api_facets'][$field_identifier] = array( 'field' => $field_identifier, 'limit' => 50,