diff --git a/facets.module b/facets.module index ba63101..ad771c1 100644 --- a/facets.module +++ b/facets.module @@ -30,12 +30,13 @@ function facets_help($route_name, RouteMatchInterface $route_match) { * @param \Drupal\search_api\Query\QueryInterface $query */ function facets_search_api_query_alter(QueryInterface &$query) { - /** @var \Drupal\facets\FacetManager\DefaultFacetManager $facet_manager */ - $facet_manager = \Drupal::service('facets.manager'); + if ($query->getIndex()->getServerInstance()->supportsFeature('search_api_facets')) { + /** @var \Drupal\facets\FacetManager\DefaultFacetManager $facet_manager */ + $facet_manager = \Drupal::service('facets.manager'); - $search_id = $query->getOption('search id'); - - // Add the active filters. - $facet_manager->alterQuery($query, $search_id); + $search_id = $query->getOption('search id'); + // Add the active filters. + $facet_manager->alterQuery($query, $search_id); + } } diff --git a/src/Entity/Facet.php b/src/Entity/Facet.php index e4ef5bc..13b05f8 100644 --- a/src/Entity/Facet.php +++ b/src/Entity/Facet.php @@ -167,13 +167,6 @@ class Facet extends ConfigEntityBase implements FacetInterface { protected $results = []; /** - * The results. - * - * @var \Drupal\facets\Result\ResultInterface[] - */ - protected $unfiltered_results = []; - - /** * An array of active values. * * @var string[] @@ -570,20 +563,6 @@ class Facet extends ConfigEntityBase implements FacetInterface { /** * {@inheritdoc} */ - public function setUnfilteredResults(array $all_results = []) { - $this->unfiltered_results = $all_results; - } - - /** - * {@inheritdoc} - */ - public function getUnfilteredResults() { - return $this->unfiltered_results; - } - - /** - * {@inheritdoc} - */ public function isActiveValue($value) { $is_active = FALSE; if (in_array($value, $this->active_values)) { diff --git a/src/FacetInterface.php b/src/FacetInterface.php index 855001c..39738ab 100644 --- a/src/FacetInterface.php +++ b/src/FacetInterface.php @@ -141,26 +141,6 @@ interface FacetInterface extends ConfigEntityInterface { public function setResults(array $results); /** - * Sets an array of unfiltered results. - * - * These unfiltered results are used to set the correct count of the actual - * facet results when using the OR query operator. They are not results value - * objects like those in ::$results. - * - * @param array $all_results - * Unfiltered results. - */ - public function setUnfilteredResults(array $all_results = []); - - /** - * Returns an array of unfiltered results. - * - * @return array - * Unfiltered results. - */ - public function getUnfilteredResults(); - - /** * Returns the query type instance. * * @return string diff --git a/src/FacetManager/DefaultFacetManager.php b/src/FacetManager/DefaultFacetManager.php index 5004ca7..dd9e30c 100644 --- a/src/FacetManager/DefaultFacetManager.php +++ b/src/FacetManager/DefaultFacetManager.php @@ -144,12 +144,7 @@ class DefaultFacetManager { foreach ($this->getFacetsByFacetSourceId($facetsource_id) as $facet) { /** @var \Drupal\facets\QueryType\QueryTypeInterface $query_type_plugin */ $query_type_plugin = $this->queryTypePluginManager->createInstance($facet->getQueryType(), ['query' => $query, 'facet' => $facet]); - $unfiltered_results = $query_type_plugin->execute(); - - // Save unfiltered results in facet. - if (!is_null($unfiltered_results)) { - $facet->setUnfilteredResults($unfiltered_results); - } + $query_type_plugin->execute(); } } diff --git a/src/Plugin/facets/query_type/SearchApiString.php b/src/Plugin/facets/query_type/SearchApiString.php index 27930e0..34c2813 100644 --- a/src/Plugin/facets/query_type/SearchApiString.php +++ b/src/Plugin/facets/query_type/SearchApiString.php @@ -35,31 +35,12 @@ class SearchApiString extends QueryTypePluginBase { public function execute() { $query = $this->query; - $unfiltered_results = []; - // 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(); $exclude = $this->facet->getExclude(); - // 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 = $unfiltered_query - ->execute() - ->getExtraData('search_api_facets'); - // Set the options for the actual query. $options = &$query->getOptions(); $options['search_api_facets'][$field_identifier] = array( @@ -74,15 +55,13 @@ class SearchApiString extends QueryTypePluginBase { $active_items = $this->facet->getActiveItems(); if (count($active_items)) { - $filter = $query->createConditionGroup($operator); + $filter = $query->createConditionGroup($operator, ['facet:' . $field_identifier]); foreach ($active_items as $value) { $filter->addCondition($this->facet->getFieldIdentifier(), $value, $exclude ? '<>' : '='); } $query->addConditionGroup($filter); } } - - return $unfiltered_results; } /** @@ -96,17 +75,6 @@ class SearchApiString extends QueryTypePluginBase { foreach ($this->results as $key => $result) { if ($result['count'] || $query_operator == 'OR') { $count = $result['count']; - if ($query_operator === 'OR') { - $unfiltered_results = $this->facet->getUnfilteredResults(); - $field_identifier = $this->facet->getFieldIdentifier(); - - foreach ($unfiltered_results[$field_identifier] as $unfiltered_result) { - if ($unfiltered_result['filter'] === $result['filter']) { - $count = $unfiltered_result['count']; - } - } - } - $result = new Result(trim($result['filter'], '"'), trim($result['filter'], '"'), $count); $facet_results[] = $result; } diff --git a/src/QueryType/QueryTypeInterface.php b/src/QueryType/QueryTypeInterface.php index 9ea1473..746e1f5 100644 --- a/src/QueryType/QueryTypeInterface.php +++ b/src/QueryType/QueryTypeInterface.php @@ -9,9 +9,6 @@ interface QueryTypeInterface { /** * Adds facet info to the query using the backend native query object. - * - * @return array - * Returns an array of unfiltered results */ public function execute(); diff --git a/src/Tests/IntegrationTest.php b/src/Tests/IntegrationTest.php index bc71790..6a03faf 100644 --- a/src/Tests/IntegrationTest.php +++ b/src/Tests/IntegrationTest.php @@ -455,6 +455,47 @@ class IntegrationTest extends WebTestBase { } /** + * Tests calculations of facet count. + */ + public function testFacetCountCalculations() { + $this->addFacet('Type'); + $this->addFacet('Keywords', 'keywords'); + $this->createBlock('type'); + $this->createBlock('keywords'); + + $edit = ['widget' => 'links', 'widget_configs[show_numbers]' => '1']; + $this->drupalPostForm('admin/config/search/facets/keywords/edit', $edit, $this->t('Save')); + $this->drupalPostForm('admin/config/search/facets/type/edit', $edit, $this->t('Save')); + + $this->drupalGet('search-api-test-fulltext'); + $this->assertText('Displaying 5 search results'); + $this->assertText('article (2)'); + $this->assertText('grape (3)'); + + // Make sure that after clicking on article, which has only 2 entities, + // there are only 2 items left in the results for other facets as well. + // In this case, that means we can't have 3 entities tagged with grape. Both + // remaining entities are tagged with grape and strawberry. + $this->clickLinkPartialName('article'); + $this->assertNoText('grape (3)'); + $this->assertText('grape (2)'); + $this->assertText('strawberry (2)'); + + $this->drupalGet('search-api-test-fulltext'); + $this->assertText('Displaying 5 search results'); + $this->assertText('article (2)'); + $this->assertText('grape (3)'); + + // Make sure that after clicking on grape, which has only 3 entities, there + // are only 3 items left in the results for other facets as well. In this + // case, that means 2 entities of type article and 1 item. + $this->clickLinkPartialName('grape'); + $this->assertText('Displaying 3 search results'); + $this->assertText('article (2)'); + $this->assertText('item (1)'); + } + + /** * Configures empty behavior option to show a text on empty results. * * @param string $facet_name diff --git a/src/Tests/ProcessorIntegrationTest.php b/src/Tests/ProcessorIntegrationTest.php index def41cf..ceff798 100644 --- a/src/Tests/ProcessorIntegrationTest.php +++ b/src/Tests/ProcessorIntegrationTest.php @@ -309,11 +309,11 @@ class ProcessorIntegrationTest extends WebTestBase { protected function checkHideNonNarrowingProcessor() { $this->drupalGet('search-api-test-fulltext'); $this->assertText('Displaying 10 search results'); - $this->assertLink('grape'); + $this->assertLink('apple'); - $this->clickLink('grape'); - $this->assertText('Displaying 6 search results'); - $this->assertLink('orange'); + $this->clickLink('apple'); + $this->assertText('Displaying 4 search results'); + $this->assertLink('grape'); $form = [ 'facet_settings[hide_non_narrowing_result_processor][status]' => TRUE, @@ -322,11 +322,11 @@ class ProcessorIntegrationTest extends WebTestBase { $this->drupalGet('search-api-test-fulltext'); $this->assertText('Displaying 10 search results'); - $this->assertLink('grape'); + $this->assertLink('apple'); - $this->clickLink('grape'); - $this->assertText('Displaying 6 search results'); - $this->assertNoLink('orange'); + $this->clickLink('apple'); + $this->assertText('Displaying 4 search results'); + $this->assertNoLink('grape'); $form = [ 'facet_settings[hide_non_narrowing_result_processor][status]' => FALSE, diff --git a/tests/src/Unit/Plugin/query_type/SearchApiStringTest.php b/tests/src/Unit/Plugin/query_type/SearchApiStringTest.php index cc927c1..bec7fea 100644 --- a/tests/src/Unit/Plugin/query_type/SearchApiStringTest.php +++ b/tests/src/Unit/Plugin/query_type/SearchApiStringTest.php @@ -63,16 +63,6 @@ class SearchApiStringTest extends UnitTestCase { ['query_operator' => 'OR'], 'facets_facet' ); - - $facet->setUnfilteredResults([ - 'field_animal' => [ - ['count' => 9, 'filter' => 'unicorn'], - ['count' => 3, 'filter' => 'badger'], - ['count' => 7, 'filter' => 'narwhal'], - ['count' => 5, 'filter' => 'mushroom'], - ], - ]); - $facet->setFieldIdentifier('field_animal'); $original_results = [