Commit 888290d8 authored by ekes's avatar ekes Committed by borisson_

Issue #2872404 by borisson_, ekes: Use native facet range queries with solr

parent 2ed48251
......@@ -183,7 +183,7 @@ class SearchApiDisplay extends FacetSourcePluginBase implements SearchApiFacetSo
// query type.
foreach ($facets as $facet) {
$configuration = [
'query' => NULL,
'query' => $results->getQuery(),
'facet' => $facet,
'results' => isset($facet_results[$facet->getFieldIdentifier()]) ? $facet_results[$facet->getFieldIdentifier()] : [],
];
......
......@@ -68,27 +68,51 @@ abstract class QueryTypeRangeBase extends QueryTypePluginBase {
public function build() {
$query_operator = $this->facet->getQueryOperator();
// Go through the results and add facet results grouped by filters
// defined by self::calculateResultFilter().
if (!empty($this->results)) {
// If there were no results or no query object, we can't do anything.
if (empty($this->results)) {
return $this->facet;
}
$supportedFeatures = array_flip($this->query
->getIndex()
->getServerInstance()
->getBackend()
->getSupportedFeatures());
// Range grouping is supported.
if (isset($supportedFeatures['search_api_granular'])) {
$facet_results = [];
foreach ($this->results as $result) {
if ($result['count'] || $query_operator == 'or') {
$count = $result['count'];
$result_filter = $this->calculateResultFilter(trim($result['filter'], '"'));
if (isset($facet_results[$result_filter['raw']])) {
$facet_results[$result_filter['raw']]->setCount(
$facet_results[$result_filter['raw']]->getCount() + $count
);
}
else {
$facet_results[$result_filter['raw']] = new Result($this->facet, $result_filter['raw'], $result_filter['display'], $count);
}
$result_filter = trim($result['filter'], '"');
$facet_results[] = new Result($this->facet, $result_filter, $result_filter, $result['count']);
}
}
$this->facet->setResults($facet_results);
return $this->facet;
}
// Non supported backend range grouping.
$facet_results = [];
foreach ($this->results as $result) {
// Go through the results and add facet results grouped by filters
// defined by self::calculateResultFilter().
if ($result['count'] || $query_operator == 'or') {
$count = $result['count'];
$result_filter = $this->calculateResultFilter(trim($result['filter'], '"'));
if (isset($facet_results[$result_filter['raw']])) {
$facet_results[$result_filter['raw']]->setCount(
$facet_results[$result_filter['raw']]->getCount() + $count
);
}
else {
$facet_results[$result_filter['raw']] = new Result($this->facet, $result_filter['raw'], $result_filter['display'], $count);
}
}
}
$this->facet->setResults($facet_results);
return $this->facet;
}
......
......@@ -5,7 +5,10 @@ namespace Drupal\Tests\facets\Kernel\Plugin\query_type;
use Drupal\KernelTests\KernelTestBase;
use Drupal\facets\Entity\Facet;
use Drupal\facets\Plugin\facets\query_type\SearchApiDate;
use Drupal\search_api\Backend\BackendInterface;
use Drupal\search_api\IndexInterface;
use Drupal\search_api\Plugin\views\query\SearchApiQuery;
use Drupal\search_api\ServerInterface;
/**
* Kernel test for date query type.
......@@ -32,7 +35,15 @@ class SearchApiDateTest extends KernelTestBase {
* @dataProvider resultsProvider
*/
public function testQueryTypeAnd($granularity, $original_results, $grouped_results) {
$query = new SearchApiQuery([], 'search_api_query', []);
$backend = $this->prophesize(BackendInterface::class);
$backend->getSupportedFeatures()->willReturn([]);
$server = $this->prophesize(ServerInterface::class);
$server->getBackend()->willReturn($backend);
$index = $this->prophesize(IndexInterface::class);
$index->getServerInstance()->willReturn($server);
$query = $this->prophesize(SearchApiQuery::class);
$query->getIndex()->willReturn($index);
$facetReflection = new \ReflectionClass('Drupal\facets\Entity\Facet');
$facet = new Facet(
['query_operator' => 'AND', 'widget' => 'links'],
......@@ -54,7 +65,7 @@ class SearchApiDateTest extends KernelTestBase {
$query_type = new SearchApiDate(
[
'facet' => $facet,
'query' => $query,
'query' => $query->reveal(),
'results' => $original_results,
],
'search_api_string',
......
......@@ -4,7 +4,10 @@ namespace Drupal\Tests\facets\Unit\Plugin\query_type;
use Drupal\facets\Entity\Facet;
use Drupal\facets\Plugin\facets\query_type\SearchApiGranular;
use Drupal\search_api\Backend\BackendInterface;
use Drupal\search_api\IndexInterface;
use Drupal\search_api\Plugin\views\query\SearchApiQuery;
use Drupal\search_api\ServerInterface;
use Drupal\Tests\UnitTestCase;
/**
......@@ -18,7 +21,15 @@ class SearchApiGranularTest extends UnitTestCase {
* Tests string query type without executing the query with an "AND" operator.
*/
public function testQueryTypeAnd() {
$query = new SearchApiQuery([], 'search_api_query', []);
$backend = $this->prophesize(BackendInterface::class);
$backend->getSupportedFeatures()->willReturn([]);
$server = $this->prophesize(ServerInterface::class);
$server->getBackend()->willReturn($backend);
$index = $this->prophesize(IndexInterface::class);
$index->getServerInstance()->willReturn($server);
$query = $this->prophesize(SearchApiQuery::class);
$query->getIndex()->willReturn($index);
$facetReflection = new \ReflectionClass('Drupal\facets\Entity\Facet');
$facet = new Facet(
['query_operator' => 'AND', 'widget' => 'links'],
......@@ -49,7 +60,7 @@ class SearchApiGranularTest extends UnitTestCase {
$query_type = new SearchApiGranular(
[
'facet' => $facet,
'query' => $query,
'query' => $query->reveal(),
'results' => $original_results,
],
'search_api_string',
......
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