Skip to content
Snippets Groups Projects
Commit 35d1454e authored by Rupert Jabelman's avatar Rupert Jabelman Committed by Kim Pepper
Browse files

Issue #3477015: FacetResultParser returns incorrect values for date facets

parent c818a453
Branches 2.6.x
No related tags found
1 merge request!68[#3339329] Option to disable SSL verification
Pipeline #315313 failed
......@@ -35,20 +35,15 @@ class FacetResultParser {
public function parseFacetResult(QueryInterface $query, array $response): array {
$facetData = [];
$facets = $query->getOption('search_api_facets', []);
$indexFields = $query->getIndex()->getFields();
foreach ($facets as $facet_id => $facet) {
$terms = [];
$type = $indexFields[$facet_id]->getType();
// Handle 'and' operator.
if ($facet['operator'] === 'and') {
$buckets = $response['aggregations'][$facet_id]['buckets'];
array_walk($buckets, function ($value) use (&$terms) {
$terms[] = [
'count' => $value['doc_count'],
'filter' => '"' . $value['key'] . '"',
];
});
$facetData[$facet_id] = $terms;
$facetData[$facet_id] = $this->getFacetValues($buckets, $type);
continue;
}
if ($facet['operator'] === 'or') {
......@@ -57,19 +52,37 @@ class FacetResultParser {
continue;
}
$buckets = $response['aggregations'][$facet_id . '_global'][$facet_id]['buckets'];
array_walk($buckets, function ($value) use (&$terms) {
$terms[] = [
'count' => $value['doc_count'],
'filter' => '"' . $value['key'] . '"',
];
});
$facetData[$facet_id] = $terms;
$facetData[$facet_id] = $this->getFacetValues($buckets, $type);
continue;
}
$this->logger->warning("Invalid operator: %operator", ['%operator' => $facet['operator']]);
}
return $facetData;
}
/**
* Transform the aggregation response into an array of values for Facets.
*/
protected function getFacetValues(array $buckets, string $type): array {
$terms = [];
foreach ($buckets as $bucket) {
if ($type === 'date') {
// key_as_string is an ISO 8601 date with millisecond precision.
// EG: 2016-03-04T12:00:00.000Z.
$datetime = new \DateTimeImmutable($bucket['key_as_string']);
$filter = $datetime->getTimestamp();
}
else {
$filter = $bucket['key'];
}
$terms[] = [
'count' => $bucket['doc_count'],
'filter' => '"' . $filter . '"',
];
}
return $terms;
}
}
......@@ -5,6 +5,8 @@ declare(strict_types=1);
namespace Drupal\Tests\search_api_opensearch\Unit\SearchAPI\Query;
use Drupal\Tests\UnitTestCase;
use Drupal\search_api\IndexInterface;
use Drupal\search_api\Item\FieldInterface;
use Drupal\search_api\Query\QueryInterface;
use Drupal\search_api_opensearch\SearchAPI\Query\FacetResultParser;
use Prophecy\PhpUnit\ProphecyTrait;
......@@ -38,8 +40,28 @@ class FacetResultParserTest extends UnitTestCase {
'field' => 'field1',
'operator' => 'or',
],
'facet3' => [
'field' => 'field3',
'operator' => 'and',
],
]);
$index = $this->prophesize(IndexInterface::class);
$query->getIndex()->willReturn($index);
$field1 = $this->prophesize(FieldInterface::class);
$field1->getType()->willReturn('string');
$field2 = $this->prophesize(FieldInterface::class);
$field2->getType()->willReturn('string');
$field3 = $this->prophesize(FieldInterface::class);
$field3->getType()->willReturn('date');
$index->getFields()->willReturn([
'facet1' => $field1,
'facet2' => $field2,
'facet3' => $field3,
]);
$response = [
'aggregations' => [
'facet1' => [
......@@ -66,6 +88,22 @@ class FacetResultParserTest extends UnitTestCase {
],
],
],
'facet3' => [
'doc_count_error_upper_bound' => 0,
'sum_other_doc_count' => 0,
'buckets' => [
[
'key' => 1704974400000,
'key_as_string' => '2024-01-11T12:00:00.000Z',
'doc_count' => 3,
],
[
'key' => 1706184000000,
'key_as_string' => '2024-01-25T12:00:00.000Z',
'doc_count' => 2,
],
],
],
],
];
......@@ -88,10 +126,19 @@ class FacetResultParserTest extends UnitTestCase {
'filter' => '"whizz"',
],
],
'facet3' => [
[
'count' => 3,
'filter' => '"1704974400"',
],
[
'count' => 2,
'filter' => '"1706184000"',
],
],
];
$this->assertNotEmpty($facetData);
$this->assertEquals($expected, $facetData);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment