Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
search_api_opensearch
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
search_api_opensearch
Commits
35d1454e
Commit
35d1454e
authored
4 months ago
by
Rupert Jabelman
Committed by
Kim Pepper
4 months ago
Browse files
Options
Downloads
Patches
Plain Diff
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
4 months ago
Stage: build
Stage: validate
Stage: test
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/SearchAPI/Query/FacetResultParser.php
+29
-16
29 additions, 16 deletions
src/SearchAPI/Query/FacetResultParser.php
tests/src/Unit/SearchAPI/Query/FacetResultParserTest.php
+48
-1
48 additions, 1 deletion
tests/src/Unit/SearchAPI/Query/FacetResultParserTest.php
with
77 additions
and
17 deletions
src/SearchAPI/Query/FacetResultParser.php
+
29
−
16
View file @
35d1454e
...
...
@@ -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
)
{
$t
erms
=
[]
;
$t
ype
=
$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
;
}
}
This diff is collapsed.
Click to expand it.
tests/src/Unit/SearchAPI/Query/FacetResultParserTest.php
+
48
−
1
View file @
35d1454e
...
...
@@ -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
);
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment