This repository has been archived by the owner on Aug 13, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
382 additions
and
863 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
namespace CultuurNet\UDB3\Search\ElasticSearch\Aggregation; | ||
|
||
use CultuurNet\UDB3\Language; | ||
use CultuurNet\UDB3\Search\Facet\FacetFilter; | ||
use CultuurNet\UDB3\Search\Facet\FacetNode; | ||
use CultuurNet\UDB3\Search\Facet\FacetTreeInterface; | ||
use CultuurNet\UDB3\Search\Offer\FacetName; | ||
use CultuurNet\UDB3\ValueObject\MultilingualString; | ||
use ValueObjects\StringLiteral\StringLiteral; | ||
|
||
class LabelsAggregationTransformer implements AggregationTransformerInterface | ||
{ | ||
/** | ||
* @var FacetName | ||
*/ | ||
private $facetName; | ||
|
||
public function __construct( | ||
FacetName $facetName | ||
) { | ||
$this->facetName = $facetName; | ||
} | ||
|
||
/** | ||
* @param Aggregation $aggregation | ||
* @return bool | ||
*/ | ||
public function supports(Aggregation $aggregation) | ||
{ | ||
return $aggregation->getName()->sameValueAs($this->facetName); | ||
} | ||
|
||
/** | ||
* @param Aggregation $aggregation | ||
* @return FacetTreeInterface | ||
*/ | ||
public function toFacetTree(Aggregation $aggregation) | ||
{ | ||
if (!$this->supports($aggregation)) { | ||
$name = $aggregation->getName()->toNative(); | ||
throw new \LogicException("Aggregation $name not supported for transformation."); | ||
} | ||
|
||
$nodes = []; | ||
foreach ($aggregation->getBuckets() as $bucket) { | ||
if ($bucket->getCount() == 0) { | ||
continue; | ||
} | ||
|
||
$nodes[] = new FacetNode( | ||
$bucket->getKey(), | ||
new MultilingualString( | ||
new Language('nl'), | ||
new StringLiteral($bucket->getKey()) | ||
), | ||
$bucket->getCount() | ||
); | ||
} | ||
|
||
return new FacetFilter($this->facetName->toNative(), $nodes); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
namespace CultuurNet\UDB3\Search\ElasticSearch\Offer; | ||
|
||
use CultuurNet\UDB3\Language; | ||
use CultuurNet\UDB3\Search\ElasticSearch\PredefinedQueryFieldsInterface; | ||
|
||
class OfferPredefinedQueryStringFields implements PredefinedQueryFieldsInterface | ||
{ | ||
public function getPredefinedFields(Language ...$languages) | ||
{ | ||
$fields = [ | ||
'id', | ||
'labels_free_text', | ||
'terms_free_text.id', | ||
'terms_free_text.label', | ||
'performer_free_text.name', | ||
'location.id', | ||
'organizer.id', | ||
]; | ||
|
||
foreach ($languages as $language) { | ||
$langCode = $language->getCode(); | ||
$fields = array_merge( | ||
$fields, | ||
[ | ||
"name.{$langCode}", | ||
"description.{$langCode}", | ||
"address.{$langCode}.addressLocality", | ||
"address.{$langCode}.postalCode", | ||
"address.{$langCode}.streetAddress", | ||
"location.name.{$langCode}", | ||
"organizer.name.{$langCode}", | ||
] | ||
); | ||
} | ||
|
||
return $fields; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace CultuurNet\UDB3\Search\ElasticSearch; | ||
|
||
use CultuurNet\UDB3\Language; | ||
|
||
interface PredefinedQueryFieldsInterface | ||
{ | ||
/** | ||
* @param Language[] $languages | ||
* @return string[] | ||
*/ | ||
public function getPredefinedFields(Language ...$languages); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace CultuurNet\UDB3\Search\ElasticSearch; | ||
|
||
use PHPUnit_Framework_TestCase; | ||
|
||
abstract class AbstractElasticSearchQueryBuilderTest extends PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @param $value | ||
* @return array | ||
*/ | ||
protected function expectedTextQuery($value, array $fields = []) | ||
{ | ||
$textQuery = [ | ||
'query' => $value, | ||
'default_operator' => 'AND', | ||
]; | ||
|
||
if (!empty($fields)) { | ||
$textQuery += ['fields' => $fields]; | ||
} | ||
|
||
return $textQuery; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
<?php | ||
|
||
namespace CultuurNet\UDB3\Search\ElasticSearch\Aggregation; | ||
|
||
use CultuurNet\UDB3\Language; | ||
use CultuurNet\UDB3\Search\Facet\FacetFilter; | ||
use CultuurNet\UDB3\Search\Facet\FacetNode; | ||
use CultuurNet\UDB3\Search\Offer\FacetName; | ||
use CultuurNet\UDB3\ValueObject\MultilingualString; | ||
use ValueObjects\StringLiteral\StringLiteral; | ||
|
||
class LabelsAggregationTransformerTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @var FacetName | ||
*/ | ||
private $facetName; | ||
|
||
/** | ||
* @var LabelsAggregationTransformer | ||
*/ | ||
private $transformer; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function setUp() | ||
{ | ||
$this->facetName = FacetName::LABELS(); | ||
|
||
$this->transformer = new LabelsAggregationTransformer($this->facetName); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function it_only_supports_aggregations_named_after_the_injected_facet_name() | ||
{ | ||
$supported = new Aggregation($this->facetName); | ||
$unsupported = new Aggregation(FacetName::REGIONS()); | ||
|
||
$this->assertTrue($this->transformer->supports($supported)); | ||
$this->assertFalse($this->transformer->supports($unsupported)); | ||
|
||
$this->expectException(\LogicException::class); | ||
$this->expectExceptionMessage("Aggregation regions not supported for transformation."); | ||
|
||
$this->transformer->toFacetTree($unsupported); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function it_returns_a_facet_filter_solely_based_on_the_bucket_data() | ||
{ | ||
$aggregation = new Aggregation( | ||
$this->facetName, | ||
...[ | ||
new Bucket('hiddenLabel1', 11), | ||
new Bucket('hiddenLabel2', 6), | ||
new Bucket('labelA', 2), | ||
] | ||
); | ||
|
||
$expectedFacetTree = new FacetFilter( | ||
$this->facetName->toNative(), | ||
[ | ||
new FacetNode( | ||
'hiddenLabel1', | ||
new MultilingualString( | ||
new Language('nl'), | ||
new StringLiteral('hiddenLabel1') | ||
), | ||
11 | ||
), | ||
new FacetNode( | ||
'hiddenLabel2', | ||
new MultilingualString( | ||
new Language('nl'), | ||
new StringLiteral('hiddenLabel2') | ||
), | ||
6 | ||
), | ||
new FacetNode( | ||
'labelA', | ||
new MultilingualString( | ||
new Language('nl'), | ||
new StringLiteral('labelA') | ||
), | ||
2 | ||
), | ||
] | ||
); | ||
|
||
$this->assertEquals( | ||
$expectedFacetTree, | ||
$this->transformer->toFacetTree($aggregation) | ||
); | ||
} | ||
} |
Oops, something went wrong.