diff --git a/src/Plugin/search_api/backend/SearchApiSolrBackend.php b/src/Plugin/search_api/backend/SearchApiSolrBackend.php index 2d732e1..674bc13 100644 --- a/src/Plugin/search_api/backend/SearchApiSolrBackend.php +++ b/src/Plugin/search_api/backend/SearchApiSolrBackend.php @@ -1175,12 +1175,11 @@ class SearchApiSolrBackend extends BackendPluginBase { * An array describing facets that apply to the current results. */ protected function extractFacets(QueryInterface $query, Result $resultset) { - $facets = array(); - if (!$resultset->getFacetSet()) { - return $facets; + return array(); } + $facets = array(); $index = $query->getIndex(); $field_names = $this->getFieldNames($index); $fields = $index->getFields(); @@ -1373,7 +1372,7 @@ class SearchApiSolrBackend extends BackendPluginBase { // String fields have their own corresponding facet fields. $field = $field_names[$info['field']]; // Check for the "or" operator. - if (isset($info['operator']) && $info['operator'] === 'or') { + if (isset($info['operator']) && strtolower($info['operator']) === 'or') { // Remember that filters for this field should be tagged. $escaped = SearchApiSolrUtility::escapeFieldName($field_names[$info['field']]); $taggedFields[$escaped] = "{!tag=$escaped}"; @@ -1398,6 +1397,7 @@ class SearchApiSolrBackend extends BackendPluginBase { $facet_field->setMissing(TRUE); } } + // Tag filters of fields with "OR" facets. foreach ($taggedFields as $field => $tag) { $regex = '#(?solrAvailable = TRUE; } } - catch (\Exception $e) { - } + catch (\Exception $e) {} } /** @@ -86,11 +86,64 @@ class SearchApiSolrTest extends BackendTest { parent::testFramework(); } else { - $this->pass('Error: The Solr instance could not be found. Please enable a multi-core one on http://localhost:8983/solr/d8'); + $this->assertTrue(TRUE, 'Error: The Solr instance could not be found. Please enable a multi-core one on http://localhost:8983/solr/d8'); } } /** + * Tests facets. + */ + public function testFacets() { + $this->insertExampleContent(); + $this->indexItems($this->indexId); + + // Create a query object. + $query = Utility::createQuery($this->getIndex()); + + // Add a condition on the query object, to filter on category. + $conditions = $query->createConditionGroup('OR', array('facet:category')); + $conditions->addCondition('category', 'article_category'); + $query->addConditionGroup($conditions); + + // Add facet to the query. + $facets['category'] = array( + 'field' => 'category', + 'limit' => 1, + 'min_count' => 1, + 'missing' => TRUE, + 'operator' => 'or', + ); + $query->setOption('search_api_facets', $facets); + + // Get the result. + $results = $query->execute(); + + $expected_results = array( + 'entity:entity_test/4:en', + 'entity:entity_test/5:en', + ); + + // Asserts that the result count is correct, as well as that the entities 4 + // and 5 returned. And that the added condition actually filtered out the + // results so that the category of the returned results is article_category. + $this->assertEquals($expected_results, array_keys($results->getResultItems())); + $this->assertEquals(array('article_category'), $results->getResultItems()['entity:entity_test/4:en']->getField('category')->getValues()); + $this->assertEquals(array('article_category'), $results->getResultItems()['entity:entity_test/5:en']->getField('category')->getValues()); + $this->assertEquals(2, $results->getResultCount(), 'OR facets query returned correct number of results.'); + + $expected = array( + array('count' => 2, 'filter' => '"article_category"'), + array('count' => 2, 'filter' => '"item_category"'), + array('count' => 1, 'filter' => '!'), + ); + $category_facets = $results->getExtraData('search_api_facets')['category']; + usort($category_facets, array($this, 'facetCompare')); + + // Asserts that the returned facets are those that we expected. + $this->assertEquals($expected, $category_facets, 'Correct OR facets were returned'); + } + + /** * {@inheritdoc} */ protected function indexItems($index_id) { @@ -151,7 +204,7 @@ class SearchApiSolrTest extends BackendTest { sleep(2); $query = $this->buildSearch(); $results = $query->execute(); - $this->assertEqual($results->getResultCount(), 0, 'Clearing the server worked correctly.'); + $this->assertEquals(0, $results->getResultCount(), 'Clearing the server worked correctly.'); } /**