diff --git a/CHANGELOG.md b/CHANGELOG.md index c1e9c1a..1bef572 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [1.2.3] - 2022-12-13 +### Changed +- Find by scopes (multiple sizes) + ## [1.2.2] - 2022-09-26 ### Fixed - Randomize order of banners with the same score @@ -82,7 +86,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [0.1.0] - 2019-04-19 Last python version -[Unreleased]: https://github.com/adshares/adselect/compare/v1.2.2...develop +[Unreleased]: https://github.com/adshares/adselect/compare/v1.2.3...develop +[1.2.3]: https://github.com/adshares/adselect/compare/v1.2.2...v1.2.3 [1.2.2]: https://github.com/adshares/adselect/compare/v1.2.1...v1.2.2 [1.2.1]: https://github.com/adshares/adselect/compare/v1.2.0...v1.2.1 [1.2.0]: https://github.com/adshares/adselect/compare/v1.1.2...v1.2.0 diff --git a/src/Application/Dto/QueryDto.php b/src/Application/Dto/QueryDto.php index 43c91bd..1b7b921 100644 --- a/src/Application/Dto/QueryDto.php +++ b/src/Application/Dto/QueryDto.php @@ -15,7 +15,7 @@ final class QueryDto private Id $siteId; private Id $zoneId; private Id $userId; - private Size $size; + private array $scopes; private array $requireFilters; private array $excludeFilters; private array $keywords; @@ -28,7 +28,7 @@ public function __construct( Id $zoneId, Id $userId, Id $trackingId, - Size $size, + array $scopes, array $zone_options = [], array $filters = [], array $keywords = [] @@ -38,7 +38,7 @@ public function __construct( $this->zoneId = $zoneId; $this->userId = $userId; $this->trackingId = $trackingId; - $this->size = $size; + $this->scopes = $scopes; $this->requireFilters = $filters['require'] ?? []; $this->excludeFilters = $filters['exclude'] ?? []; $this->keywords = $keywords; @@ -90,9 +90,9 @@ public function getTrackingId(): string return $this->trackingId->toString(); } - public function getSize(): string + public function getScopes(): array { - return $this->size->toString(); + return $this->scopes; } public static function fromArray(array $input): self @@ -117,8 +117,11 @@ public static function fromArray(array $input): self throw new ValidationDtoException('Field `tracking_id` is required.'); } - if (!isset($input['banner_size'])) { - throw new ValidationDtoException('Field `banner_size` is required.'); + if (!isset($input['scopes'])) { + if (!isset($input['banner_size'])) { + throw new ValidationDtoException('Field `scopes` is required.'); + } + $input['scopes'] = [$input['banner_size']]; } if (!isset($input['keywords'])) { @@ -136,7 +139,7 @@ public static function fromArray(array $input): self new Id($input['zone_id']), new Id($input['user_id']), new Id($input['tracking_id']), - new Size($input['banner_size']), + $input['scopes'], $input['zone_options'] ?? [], $input['banner_filters'], $input['keywords'] diff --git a/src/Application/Service/BannerFinder.php b/src/Application/Service/BannerFinder.php index 0b074be..fe2c3a5 100644 --- a/src/Application/Service/BannerFinder.php +++ b/src/Application/Service/BannerFinder.php @@ -9,5 +9,5 @@ interface BannerFinder { - public function find(QueryDto $queryDto, int $size = 1): FoundBannersCollection; + public function find(QueryDto $queryDto, int $resultCount = 1): FoundBannersCollection; } diff --git a/src/Infrastructure/ElasticSearch/QueryBuilder/BaseQuery.php b/src/Infrastructure/ElasticSearch/QueryBuilder/BaseQuery.php index ff5591b..2f5af62 100644 --- a/src/Infrastructure/ElasticSearch/QueryBuilder/BaseQuery.php +++ b/src/Infrastructure/ElasticSearch/QueryBuilder/BaseQuery.php @@ -42,7 +42,7 @@ public function build(): array ); $excludes = KeywordsToExclude::build(self::PREFIX_FILTER_EXCLUDE, $this->bannerFinderDto->getKeywords()); - $sizeFilter = FilterClause::build('banner.size', [$this->bannerFinderDto->getSize()]); + $sizeFilter = FilterClause::build('banner.size', $this->bannerFinderDto->getScopes()); $requireFilter = FilterToBanner::build( self::PREFIX_BANNER_REQUIRE, @@ -141,7 +141,6 @@ public function build(): array ] ], "boost_mode" => "replace", - //"score_mode" => "max", ], ], "score_mode" => "max", diff --git a/src/Infrastructure/ElasticSearch/Service/BannerFinder.php b/src/Infrastructure/ElasticSearch/Service/BannerFinder.php index e534b87..1d5f096 100644 --- a/src/Infrastructure/ElasticSearch/Service/BannerFinder.php +++ b/src/Infrastructure/ElasticSearch/Service/BannerFinder.php @@ -44,7 +44,7 @@ public function __construct( public function find( QueryDto $queryDto, - int $size = 1 + int $resultCount = 1 ): FoundBannersCollection { $userHistory = $this->loadUserHistory($queryDto); $defined = $this->getDefinedRequireKeywords(); @@ -52,7 +52,7 @@ public function find( $params = [ 'index' => BannerIndex::name(), - 'size' => $size, + 'size' => $resultCount, 'client' => [ 'timeout' => 0.5, 'connect_timeout' => 0.2 @@ -104,12 +104,18 @@ public function find( } foreach ($response['hits']['hits'] as $hit) { + $size = $hit['fields']['banner.size'][0]; + foreach ($queryDto->getScopes() as $scope) { + if (in_array($scope, $hit['fields']['banner.size'], true)) { + $size = $scope; + break; + } + } $collection->add( new FoundBanner( $hit['fields']['campaign_id'][0], $hit['_id'], - in_array($queryDto->getSize(), $hit['fields']['banner.size'], true) - ? $queryDto->getSize() : $hit['fields']['banner.size'][0], + $size, fmod(floor($hit['_score']), 100_000) / 100 ) ); diff --git a/tests/Unit/Infrastructure/ElasticSearch/QueryBuilder/QueryBuilderTest.php b/tests/Unit/Infrastructure/ElasticSearch/QueryBuilder/QueryBuilderTest.php index 4cf34fb..1469364 100644 --- a/tests/Unit/Infrastructure/ElasticSearch/QueryBuilder/QueryBuilderTest.php +++ b/tests/Unit/Infrastructure/ElasticSearch/QueryBuilder/QueryBuilderTest.php @@ -26,7 +26,8 @@ public function testWhenKeywordsAndFiltersAreEmpty(): void $siteId = new Id('43c567e1396b4cadb52223a51796fd04'); $zoneId = new Id('43c567e1396b4cadb52223a51796fd03'); $trackingId = new Id('43c567e1396b4cadb52223a51796fd02'); - $dto = new QueryDto($publisherId, $siteId, $zoneId, $userId, $trackingId, new Size("200x100")); + $scopes = ["200x100"]; + $dto = new QueryDto($publisherId, $siteId, $zoneId, $userId, $trackingId, $scopes); $defined = [ 'one', 'two', @@ -126,6 +127,7 @@ public function testWhenFiltersExist(): void $zoneId = new Id('43c567e1396b4cadb52223a51796fd03'); $userId = new Id('85f115636b384744949300571aad2a4f'); $trackingId = new Id('85f115636b384744949300571aad2a4d'); + $scopes = ["160x600"]; $keywords = [ 'device:type' => ['mobile'], @@ -154,7 +156,7 @@ public function testWhenFiltersExist(): void $zoneId, $userId, $trackingId, - new Size("160x600"), + $scopes, $filters, $keywords );