Skip to content
This repository has been archived by the owner on Aug 13, 2019. It is now read-only.

Commit

Permalink
III-1986: Add filters for term ids and term labels on offer query
Browse files Browse the repository at this point in the history
  • Loading branch information
bertramakers committed Mar 31, 2017
1 parent abdb4d9 commit 0ca4956
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/Offer/ElasticSearchOfferQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,24 @@ public static function fromSearchParameters(
$boolQuery->add($audienceTypeQuery, BoolQuery::FILTER);
}

if ($searchParameters->hasTermIds()) {
// Use separate term queries instead of a single terms query, because
// a combined terms query uses OR as operator instead of AND.
foreach ($searchParameters->getTermIds() as $termId) {
$termQuery = new TermQuery('terms.id', $termId->toNative());
$boolQuery->add($termQuery, BoolQuery::FILTER);
}
}

if ($searchParameters->hasTermLabels()) {
// Use separate term queries instead of a single terms query, because
// a combined terms query uses OR as operator instead of AND.
foreach ($searchParameters->getTermLabels() as $termLabel) {
$termQuery = new TermQuery('terms.label', $termLabel->toNative());
$boolQuery->add($termQuery, BoolQuery::FILTER);
}
}

self::addLabelsQuery($boolQuery, 'labels', $searchParameters->getLabels());
self::addLabelsQuery($boolQuery, 'location.labels', $searchParameters->getLocationLabels());
self::addLabelsQuery($boolQuery, 'organizer.labels', $searchParameters->getOrganizerLabels());
Expand Down
92 changes: 92 additions & 0 deletions tests/Offer/ElasticSearchOfferQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use CultuurNet\UDB3\Search\ElasticSearch\LuceneQueryString;
use CultuurNet\UDB3\Search\Offer\AudienceType;
use CultuurNet\UDB3\Search\Offer\OfferSearchParameters;
use CultuurNet\UDB3\Search\Offer\TermId;
use CultuurNet\UDB3\Search\Offer\TermLabel;
use CultuurNet\UDB3\Search\Region\RegionId;
use ValueObjects\Number\Natural;
use ValueObjects\StringLiteral\StringLiteral;
Expand Down Expand Up @@ -523,6 +525,96 @@ public function it_can_be_created_with_a_audience_type_query()
$this->assertEquals($expectedQueryArray, $actualQueryArray);
}

/**
* @test
*/
public function it_can_be_created_with_a_term_ids_query()
{
$searchParameters = (new OfferSearchParameters())
->withStart(new Natural(30))
->withLimit(new Natural(10))
->withTermIds(
new TermId('0.12.4.86'),
new TermId('0.13.4.89')
);

$expectedQueryArray = [
'from' => 30,
'size' => 10,
'query' => [
'bool' => [
'must' => [
[
'match_all' => (object) [],
],
],
'filter' => [
[
'term' => [
'terms.id' => '0.12.4.86',
],
],
[
'term' => [
'terms.id' => '0.13.4.89',
],
],
],
],
],
];

$actualQueryArray = ElasticSearchOfferQuery::fromSearchParameters($searchParameters)
->toArray();

$this->assertEquals($expectedQueryArray, $actualQueryArray);
}

/**
* @test
*/
public function it_can_be_created_with_a_term_labels_query()
{
$searchParameters = (new OfferSearchParameters())
->withStart(new Natural(30))
->withLimit(new Natural(10))
->withTermLabels(
new TermLabel('Jeugdhuis'),
new TermLabel('Cultureel Centrum')
);

$expectedQueryArray = [
'from' => 30,
'size' => 10,
'query' => [
'bool' => [
'must' => [
[
'match_all' => (object) [],
],
],
'filter' => [
[
'term' => [
'terms.label' => 'Jeugdhuis',
],
],
[
'term' => [
'terms.label' => 'Cultureel Centrum',
],
],
],
],
],
];

$actualQueryArray = ElasticSearchOfferQuery::fromSearchParameters($searchParameters)
->toArray();

$this->assertEquals($expectedQueryArray, $actualQueryArray);
}

/**
* @test
*/
Expand Down

0 comments on commit 0ca4956

Please sign in to comment.