Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
18 / 18 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
| ParsedBooleanNode | |
100.00% |
18 / 18 |
|
100.00% |
4 / 4 |
4 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| toArray | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
1 | |||
| getClauses | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| accept | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace CirrusSearch\Parser\AST; |
| 4 | |
| 5 | use CirrusSearch\Parser\AST\Visitor\Visitor; |
| 6 | |
| 7 | /** |
| 8 | * A boolean expression. |
| 9 | * |
| 10 | * This "boolean expression" is suited for matching document in search. It's not really |
| 11 | * describing a binary tree as one would use to a compute a boolean algebra. |
| 12 | * It is a flat list of clauses where the clauses are not connected to each others but |
| 13 | * rather describe how individual clause must occur in a given document: |
| 14 | * - MUST |
| 15 | * - MUST_NOT |
| 16 | * - SHOULD |
| 17 | * |
| 18 | * There is a direct relationship between boolean algebra and this representation: |
| 19 | * - A AND B: [ MUST:A, MUST:B ] |
| 20 | * - A OR B: [ SHOULD:A, SHOULD:B ] |
| 21 | * - A AND NOT B: [ MUST:A, MUST_NOT: B ] |
| 22 | * |
| 23 | * But it supports describing something that is impossible to describe in a boolean algebra: |
| 24 | * - [ MUST:A, SHOULD:B ]: here the boolean algebra is not suited since SHOULD:B is pointless |
| 25 | * but for search it can be considered as a "scoring hint", i.e. give me all docs matching A but |
| 26 | * it's "better" to have docs matching B as well. When filtering the search backend can silently |
| 27 | * ignore the SHOULD:B but when scoring it will be taken into account. |
| 28 | * |
| 29 | * In the end this representation makes it possible to support a syntax like: |
| 30 | * word1 +word2 -word3: [ SHOULD:word1, MUST:word2, MUST_NOT:word3 ] |
| 31 | * which would not be possible with a binary expression tree |
| 32 | */ |
| 33 | class ParsedBooleanNode extends ParsedNode { |
| 34 | /** |
| 35 | * @var BooleanClause[] |
| 36 | */ |
| 37 | private $clauses; |
| 38 | |
| 39 | /** |
| 40 | * @param int $startOffset |
| 41 | * @param int $endOffset |
| 42 | * @param BooleanClause[] $children |
| 43 | */ |
| 44 | public function __construct( $startOffset, $endOffset, array $children ) { |
| 45 | parent::__construct( $startOffset, $endOffset ); |
| 46 | $this->clauses = $children; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * @return array |
| 51 | */ |
| 52 | public function toArray() { |
| 53 | return [ "bool" => array_merge( |
| 54 | parent::baseParams(), |
| 55 | [ |
| 56 | 'clauses' => array_map( |
| 57 | static function ( BooleanClause $clause ) { |
| 58 | return [ |
| 59 | $clause->getOccur() => $clause->getNode()->toArray(), |
| 60 | "explicit" => $clause->isExplicit(), |
| 61 | ]; |
| 62 | }, |
| 63 | $this->clauses |
| 64 | ) |
| 65 | ] |
| 66 | ) ]; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * @return BooleanClause[] |
| 71 | */ |
| 72 | public function getClauses() { |
| 73 | return $this->clauses; |
| 74 | } |
| 75 | |
| 76 | public function accept( Visitor $visitor ) { |
| 77 | $visitor->visitParsedBooleanNode( $this ); |
| 78 | } |
| 79 | } |