Skip to content

Commit 713f4e6

Browse files
authored
Respect boolean operator preceding subquery. (#10)
* Respect boolean operator preceding subquery (#9) * Respect boolean operator preceding subquery * Add test for negating subqueries * pull #9 ensure fromArray brings in bool operator as well and update doc block and changelog
1 parent 654bb28 commit 713f4e6

File tree

4 files changed

+59
-7
lines changed

4 files changed

+59
-7
lines changed

CHANGELOG-0.x.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
This changelog references the relevant changes done in 0.x versions.
33

44

5+
## v0.2.1
6+
* pull #9: Respect boolean operator preceding subquery.
7+
8+
59
## v0.2.0
610
__BREAKING CHANGES__
711

src/Node/Subquery.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Gdbots\QueryParser\Node;
44

55
use Gdbots\QueryParser\Builder\QueryBuilder;
6+
use Gdbots\QueryParser\Enum\BoolOperator;
67

78
final class Subquery extends Node
89
{
@@ -16,14 +17,19 @@ final class Subquery extends Node
1617
* Subquery constructor.
1718
*
1819
* @param Node[] $nodes
20+
* @param BoolOperator $boolOperator
1921
* @param bool $useBoost
2022
* @param float|mixed $boost
2123
*
2224
* @throws \LogicException
2325
*/
24-
public function __construct(array $nodes, $useBoost = false, $boost = self::DEFAULT_BOOST)
25-
{
26-
parent::__construct(null, null, $useBoost, $boost);
26+
public function __construct(
27+
array $nodes,
28+
BoolOperator $boolOperator = null,
29+
$useBoost = false,
30+
$boost = self::DEFAULT_BOOST
31+
) {
32+
parent::__construct(null, $boolOperator, $useBoost, $boost);
2733
$this->nodes = $nodes;
2834

2935
foreach ($this->nodes as $node) {
@@ -49,7 +55,13 @@ public static function fromArray(array $data = [])
4955
}
5056
}
5157

52-
return new self($nodes, $useBoost, $boost);
58+
try {
59+
$boolOperator = isset($data['bool_operator']) ? BoolOperator::create($data['bool_operator']) : null;
60+
} catch (\Exception $e) {
61+
$boolOperator = null;
62+
}
63+
64+
return new self($nodes, $boolOperator, $useBoost, $boost);
5365
}
5466

5567
/**

src/QueryParser.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ protected function handleFieldWithRange($fieldName, BoolOperator $boolOperator)
264264
return new Field($fieldName, $nodes[0], $boolOperator, $m['use_boost'], $m['boost']);
265265
}
266266

267-
$subquery = new Subquery($nodes, $m['use_boost'], $m['boost']);
267+
$subquery = new Subquery($nodes, null, $m['use_boost'], $m['boost']);
268268
return new Field($fieldName, $subquery, $boolOperator, $m['use_boost'], $m['boost']);
269269
}
270270

@@ -374,7 +374,7 @@ protected function handleSubquery(BoolOperator $queryBoolOperator)
374374
return $nodes[0]::fromArray($data);
375375
}
376376

377-
return new Subquery($nodes, $m['use_boost'], $m['boost']);
377+
return new Subquery($nodes, $queryBoolOperator, $m['use_boost'], $m['boost']);
378378
}
379379

380380
/**

tests/Fixtures/test-queries.php

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1426,6 +1426,42 @@
14261426
new Subquery([new Word('word:a'), new Word('hashtag:b')]),
14271427
]
14281428
],
1429+
1430+
[
1431+
'name' => 'booleans before and in subqueries',
1432+
'input' => '"ipad pro" AND (gold OR silver)',
1433+
'expected_tokens' => [
1434+
[T::T_PHRASE, 'ipad pro'],
1435+
T::T_AND,
1436+
T::T_SUBQUERY_START,
1437+
[T::T_WORD, 'gold'],
1438+
T::T_OR,
1439+
[T::T_WORD, 'silver'],
1440+
T::T_SUBQUERY_END,
1441+
],
1442+
'expected_nodes' => [
1443+
new Phrase('ipad pro', BoolOperator::REQUIRED()),
1444+
new Subquery([new Word('gold'), new Word('silver')], BoolOperator::REQUIRED()),
1445+
]
1446+
],
1447+
1448+
[
1449+
'name' => 'booleans before and in subqueries 2',
1450+
'input' => '"iphone 7" -(16gb OR 32gb)',
1451+
'expected_tokens' => [
1452+
[T::T_PHRASE, 'iphone 7'],
1453+
T::T_PROHIBITED,
1454+
T::T_SUBQUERY_START,
1455+
[T::T_WORD, '16gb'],
1456+
T::T_OR,
1457+
[T::T_WORD, '32gb'],
1458+
T::T_SUBQUERY_END,
1459+
],
1460+
'expected_nodes' => [
1461+
new Phrase('iphone 7'),
1462+
new Subquery([new Word('16gb'), new Word('32gb')], BoolOperator::PROHIBITED()),
1463+
]
1464+
],
14291465
/*
14301466
* END: SUBQUERIES
14311467
*/
@@ -1569,7 +1605,7 @@
15691605
],
15701606
'expected_nodes' => [
15711607
new Phrase('john smith', null, true, 2.0),
1572-
new Subquery([new Word('foo'), new Word('bar')], true, 4.0),
1608+
new Subquery([new Word('foo'), new Word('bar')], null, true, 4.0),
15731609
]
15741610
],
15751611

0 commit comments

Comments
 (0)