Skip to content

Commit

Permalink
Merge pull request #4 from sitepark/feature/date-filter-and-facets
Browse files Browse the repository at this point in the history
feat: date filter and facets
  • Loading branch information
sitepark-veltrup authored May 10, 2024
2 parents be2b5ad + 96902df commit 3643bfe
Show file tree
Hide file tree
Showing 56 changed files with 2,047 additions and 926 deletions.
4 changes: 2 additions & 2 deletions src/Console/Command/Suggest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Atoolo\Resource\ResourceChannelFactory;
use Atoolo\Search\Console\Command\Io\TypifiedInput;
use Atoolo\Search\Dto\Search\Query\Filter\ArchiveFilter;
use Atoolo\Search\Dto\Search\Query\Filter\NotFilter;
use Atoolo\Search\Dto\Search\Query\Filter\ObjectTypeFilter;
use Atoolo\Search\Dto\Search\Query\SuggestQuery;
use Atoolo\Search\Dto\Search\Result\SuggestResult;
Expand Down Expand Up @@ -76,8 +77,7 @@ protected function execute(

protected function buildQuery(string $terms, string $lang): SuggestQuery
{
$excludeMedia = new ObjectTypeFilter(['media'], 'media');
$excludeMedia = $excludeMedia->exclude();
$excludeMedia = new NotFilter(new ObjectTypeFilter(['media']));
return new SuggestQuery(
$terms,
$lang,
Expand Down
24 changes: 24 additions & 0 deletions src/Dto/Search/Query/DateRangeRound.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Atoolo\Search\Dto\Search\Query;

/**
* @codeCoverageIgnore
*/
enum DateRangeRound : string
{
case START_OF_DAY = 'START_OF_DAY';
case START_OF_PREVIOUS_DAY = 'START_OF_PREVIOUS_DAY';
case END_OF_DAY = 'END_OF_DAY';
case END_OF_PREVIOUS_DAY = 'END_OF_PREVIOUS_DAY';
case START_OF_MONTH = 'START_OF_MONTH';
case START_OF_PREVIOUS_MONTH = 'START_OF_PREVIOUS_MONTH';
case END_OF_MONTH = 'END_OF_MONTH';
case END_OF_PREVIOUS_MONTH = 'END_OF_PREVIOUS_MONTH';
case START_OF_YEAR = 'START_OF_YEAR';
case START_OF_PREVIOUS_YEAR = 'START_OF_PREVIOUS_YEAR';
case END_OF_YEAR = 'END_OF_YEAR';
case END_OF_PREVIOUS_YEAR = 'END_OF_PREVIOUS_YEAR';
}
30 changes: 30 additions & 0 deletions src/Dto/Search/Query/Facet/AbsoluteDateRangeFacet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Atoolo\Search\Dto\Search\Query\Facet;

use DateInterval;
use DateTime;

/**
* @codeCoverageIgnore
*/
class AbsoluteDateRangeFacet extends Facet
{
/**
* @param string[] $excludeFilter
*/
public function __construct(
string $key,
public readonly DateTime $from,
public readonly DateTime $to,
public readonly ?DateInterval $gap,
array $excludeFilter = []
) {
parent::__construct(
$key,
$excludeFilter
);
}
}
17 changes: 1 addition & 16 deletions src/Dto/Search/Query/Facet/CategoryFacet.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,6 @@
/**
* @codeCoverageIgnore
*/
class CategoryFacet extends FacetField
class CategoryFacet extends FieldFacet
{
/**
* @param string[] $categories
*/
public function __construct(
string $key,
array $categories,
?string $excludeFilter = null
) {
parent::__construct(
$key,
'sp_category_path',
$categories,
$excludeFilter
);
}
}
17 changes: 1 addition & 16 deletions src/Dto/Search/Query/Facet/ContentSectionTypeFacet.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,6 @@
/**
* @codeCoverageIgnore
*/
class ContentSectionTypeFacet extends FacetField
class ContentSectionTypeFacet extends FieldFacet
{
/**
* @param string[] $contentSectionTypes
*/
public function __construct(
string $key,
array $contentSectionTypes,
?string $excludeFilter = null
) {
parent::__construct(
$key,
'sp_contenttype',
$contentSectionTypes,
$excludeFilter
);
}
}
5 changes: 4 additions & 1 deletion src/Dto/Search/Query/Facet/Facet.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@
*/
abstract class Facet
{
/**
* @param string[] $excludeFilter
*/
public function __construct(
public readonly string $key,
public readonly ?string $excludeFilter = null
public readonly array $excludeFilter = []
) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
/**
* @codeCoverageIgnore
*/
class FacetField extends Facet
class FieldFacet extends Facet
{
/**
* @param string[] $terms
* @param string[] $excludeFilter
*/
public function __construct(
string $key,
public readonly string $field,
public readonly array $terms,
?string $excludeFilter = null
array $excludeFilter = []
) {
parent::__construct($key, $excludeFilter);
}
Expand Down
17 changes: 1 addition & 16 deletions src/Dto/Search/Query/Facet/GroupFacet.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,6 @@
/**
* @codeCoverageIgnore
*/
class GroupFacet extends FacetField
class GroupFacet extends FieldFacet
{
/**
* @param string[] $groups
*/
public function __construct(
string $key,
public readonly array $groups,
?string $excludeFilter = null
) {
parent::__construct(
$key,
'sp_group_path',
$groups,
$excludeFilter
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
/**
* @codeCoverageIgnore
*/
class FacetMultiQuery extends Facet
class MultiQueryFacet extends Facet
{
/**
* @param FacetQuery[] $queries
* @param string|null $excludeFilter
* @param QueryFacet[] $queries
* @param string[] $excludeFilter
*/
public function __construct(
string $key,
public readonly array $queries,
?string $excludeFilter = null
array $excludeFilter = []
) {
parent::__construct($key, $excludeFilter);
}
Expand Down
17 changes: 1 addition & 16 deletions src/Dto/Search/Query/Facet/ObjectTypeFacet.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,6 @@
/**
* @codeCoverageIgnore
*/
class ObjectTypeFacet extends FacetField
class ObjectTypeFacet extends FieldFacet
{
/**
* @param string[] $objectTypes
*/
public function __construct(
string $key,
public readonly array $objectTypes,
?string $excludeFilter = null
) {
parent::__construct(
$key,
'sp_objecttype',
$objectTypes,
$excludeFilter
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
/**
* @codeCoverageIgnore
*/
class FacetQuery extends Facet
class QueryFacet extends Facet
{
/**
* @param string[] $excludeFilter
*/
public function __construct(
string $key,
public readonly string $query,
?string $excludeFilter = null
array $excludeFilter = []
) {
parent::__construct($key, $excludeFilter);
}
Expand Down
33 changes: 33 additions & 0 deletions src/Dto/Search/Query/Facet/RelativeDateRangeFacet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Atoolo\Search\Dto\Search\Query\Facet;

use Atoolo\Search\Dto\Search\Query\DateRangeRound;
use DateInterval;

/**
* @codeCoverageIgnore
*/
class RelativeDateRangeFacet extends Facet
{
/**
* @param string[] $excludeFilter
*/
public function __construct(
string $key,
public readonly ?\DateTime $base,
public readonly ?DateInterval $before,
public readonly ?DateInterval $after,
public readonly ?DateInterval $gap,
public readonly ?DateRangeRound $roundStart,
public readonly ?DateRangeRound $roundEnd,
array $excludeFilter = []
) {
parent::__construct(
$key,
$excludeFilter
);
}
}
17 changes: 1 addition & 16 deletions src/Dto/Search/Query/Facet/SiteFacet.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,6 @@
/**
* @codeCoverageIgnore
*/
class SiteFacet extends FacetField
class SiteFacet extends FieldFacet
{
/**
* @param string[] $sites
*/
public function __construct(
string $key,
public readonly array $sites,
?string $excludeFilter = null
) {
parent::__construct(
$key,
'sp_site',
$sites,
$excludeFilter
);
}
}
29 changes: 7 additions & 22 deletions src/Dto/Search/Query/Filter/AbsoluteDateRangeFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,23 @@
namespace Atoolo\Search\Dto\Search\Query\Filter;

use DateTime;
use InvalidArgumentException;

class AbsoluteDateRangeFilter extends Filter
{
public function __construct(
private readonly ?DateTime $from,
private readonly ?DateTime $to,
public readonly ?DateTime $from,
public readonly ?DateTime $to,
?string $key = null
) {
parent::__construct(
$key,
$key !== null ? [$key] : []
);
}

public function getQuery(): string
{
return 'sp_date_list:' .
'[' .
$this->formatDate($this->from) .
' TO ' .
$this->formatDate($this->to) .
']';
}

private function formatDate(?DateTime $date): string
{
if ($date === null) {
return '*';
if ($this->from === null && $this->to === null) {
throw new InvalidArgumentException(
'At least `from` or `to` must be specified'
);
}

$formatter = clone $date;
$formatter->setTimezone(new \DateTimeZone('UTC'));
return $formatter->format('Y-m-d\TH:i:s\Z');
}
}
15 changes: 4 additions & 11 deletions src/Dto/Search/Query/Filter/AndFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,19 @@

namespace Atoolo\Search\Dto\Search\Query\Filter;

/**
* @codeCoverageIgnore
*/
class AndFilter extends Filter
{
/**
* @param Filter[] $filter
*/
public function __construct(
private readonly array $filter,
public readonly array $filter,
?string $key = null,
array $tags = []
) {
parent::__construct($key, $tags);
}

public function getQuery(): string
{
$query = [];
foreach ($this->filter as $filter) {
$query[] = $filter->getQuery();
}

return '(' . implode(' AND ', $query) . ')';
}
}
8 changes: 3 additions & 5 deletions src/Dto/Search/Query/Filter/ArchiveFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace Atoolo\Search\Dto\Search\Query\Filter;

/**
* @codeCoverageIgnore
*/
class ArchiveFilter extends Filter
{
public function __construct()
Expand All @@ -12,9 +15,4 @@ public function __construct()
'archive'
);
}

public function getQuery(): string
{
return '-sp_archive:true';
}
}
Loading

0 comments on commit 3643bfe

Please sign in to comment.