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

Commit

Permalink
III-1957: Use separate term queries instead of a combined terms query
Browse files Browse the repository at this point in the history
  • Loading branch information
bertramakers committed Mar 14, 2017
1 parent 7f7cb74 commit 6569b23
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 28 deletions.
20 changes: 7 additions & 13 deletions src/Offer/ElasticSearchOfferQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use ONGR\ElasticsearchDSL\Query\FullText\QueryStringQuery;
use ONGR\ElasticsearchDSL\Query\Geo\GeoShapeQuery;
use ONGR\ElasticsearchDSL\Query\MatchAllQuery;
use ONGR\ElasticsearchDSL\Query\TermLevel\TermQuery;
use ONGR\ElasticsearchDSL\Query\TermLevel\TermsQuery;
use ONGR\ElasticsearchDSL\Search;

Expand Down Expand Up @@ -115,19 +116,12 @@ public static function fromSearchParameters(
*/
private static function addLabelsQuery(BoolQuery $boolQuery, $field, array $labelNames)
{
if (empty($labelNames)) {
return;
// Use separate term queries instead of a single terms query, because
// a combined terms query uses OR as operator instead of AND.
foreach ($labelNames as $labelName) {
$label = $labelName->toNative();
$termQuery = new TermQuery($field, $label);
$boolQuery->add($termQuery, BoolQuery::FILTER);
}

$labels = array_map(
function (LabelName $labelName) {
return $labelName->toNative();
},
$labelNames
);

$labelQuery = new TermsQuery($field, $labels);

$boolQuery->add($labelQuery, BoolQuery::FILTER);
}
}
36 changes: 21 additions & 15 deletions tests/Offer/ElasticSearchOfferQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,13 @@ public function it_can_be_created_with_a_labels_query()
],
'filter' => [
[
'terms' => [
'labels' => [
'foo',
'bar',
],
'term' => [
'labels' => 'foo',
],
],
[
'term' => [
'labels' => 'bar',
],
],
],
Expand Down Expand Up @@ -205,11 +207,13 @@ public function it_can_be_created_with_a_location_labels_query()
],
'filter' => [
[
'terms' => [
'location.labels' => [
'foo',
'bar',
],
'term' => [
'location.labels' => 'foo',
],
],
[
'term' => [
'location.labels' => 'bar',
],
],
],
Expand Down Expand Up @@ -248,11 +252,13 @@ public function it_can_be_created_with_an_organizer_labels_query()
],
'filter' => [
[
'terms' => [
'organizer.labels' => [
'foo',
'bar',
],
'term' => [
'organizer.labels' => 'foo',
],
],
[
'term' => [
'organizer.labels' => 'bar',
],
],
],
Expand Down

0 comments on commit 6569b23

Please sign in to comment.