diff --git a/search_api_db/src/Plugin/search_api/backend/Database.php b/search_api_db/src/Plugin/search_api/backend/Database.php index 3beb7ec..094ddf0 100644 --- a/search_api_db/src/Plugin/search_api/backend/Database.php +++ b/search_api_db/src/Plugin/search_api/backend/Database.php @@ -1915,11 +1915,17 @@ class Database extends BackendPluginBase { // If "Include missing facet" is disabled, we use an INNER JOIN and add IS // NOT NULL for shared tables. + $is_text_type = Utility::isTextType($field['type']); $alias = $this->getTableAlias($field, $select, TRUE, $facet['missing'] ? 'leftJoin' : 'innerJoin'); - $select->addField($alias, Utility::isTextType($field['type']) ? 'word' : 'value', 'value'); - if (!$facet['missing'] && !Utility::isTextType($field['type'])) { + $select->addField($alias, $is_text_type ? 'word' : 'value', 'value'); + if ($is_text_type) { + $select->condition("$alias.field_name", $this->getTextFieldName($facet['field'])); + } + if (!$facet['missing'] && !$is_text_type) { $select->isNotNull($alias . '.' . 'value'); } + + $select->addExpression('COUNT(DISTINCT t.item_id)', 'num'); $select->groupBy('value'); $select->orderBy('num', 'DESC'); diff --git a/search_api_db/src/Tests/BackendTest.php b/search_api_db/src/Tests/BackendTest.php index a0e87b0..314f615 100644 --- a/search_api_db/src/Tests/BackendTest.php +++ b/search_api_db/src/Tests/BackendTest.php @@ -695,6 +695,30 @@ class BackendTest extends EntityUnitTestBase { $facets = $results->getExtraData('search_api_facets', array())['type']; usort($facets, array($this, 'facetCompare')); $this->assertEqual($facets, $expected, 'Correct facets were returned'); + + // Regression tests for #2469547. + $query = $this->buildSearch(); + $facets = array(); + $facets['body'] = array( + 'field' => $this->getFieldId('body'), + 'limit' => 0, + 'min_count' => 1, + 'missing' => FALSE, + ); + $query->setOption('search_api_facets', $facets); + $query->condition($this->getFieldId('id'), 5, '<>'); + $query->range(0, 0); + $results = $query->execute(); + $expected = array( + array('count' => 4, 'filter' => '"test"'), + array('count' => 1, 'filter' => '"bar"'), + array('count' => 1, 'filter' => '"foobar"'), + ); + // We can't guarantee the order of returned facets, since "bar" and "foobar" + // both occur once, so we have to do a more complex check. + $facets = $results->getExtraData('search_api_facets', array())['body']; + usort($facets, array($this, 'facetCompare')); + $this->assertEqual($facets, $expected, 'Correct facets were returned for a fulltext field.'); } /**