Commit 9d94805b authored by borisson_'s avatar borisson_ Committed by borisson_

Issue #2711697 by borisson_, StryKaizer, claudiu.cristea, marthinal: Facet...

Issue #2711697 by borisson_, StryKaizer, claudiu.cristea, marthinal: Facet calculations are in the wrong order
parent 77ea6758
......@@ -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);
}
}
......@@ -166,13 +166,6 @@ class Facet extends ConfigEntityBase implements FacetInterface {
*/
protected $results = [];
/**
* The results.
*
* @var \Drupal\facets\Result\ResultInterface[]
*/
protected $unfiltered_results = [];
/**
* An array of active values.
*
......@@ -567,20 +560,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}
*/
......
......@@ -140,26 +140,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.
*
......
......@@ -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();
}
}
......
......@@ -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;
}
......
......@@ -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();
......
......@@ -454,6 +454,47 @@ class IntegrationTest extends WebTestBase {
$this->assertEqual(10, $facet->getWeight());
}
/**
* 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.
*
......
......@@ -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,
......
......@@ -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 = [
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment