Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
96.15% |
25 / 26 |
|
83.33% |
5 / 6 |
CRAP | |
0.00% |
0 / 1 |
| MSearchRequests | |
96.15% |
25 / 26 |
|
83.33% |
5 / 6 |
8 | |
0.00% |
0 / 1 |
| build | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| addRequest | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getRequests | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| toMSearchResponses | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
2 | |||
| failure | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| dumpQuery | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace CirrusSearch\Search; |
| 4 | |
| 5 | use ArrayIterator; |
| 6 | use Elastica\Search; |
| 7 | use MediaWiki\Status\Status; |
| 8 | use MultipleIterator; |
| 9 | use Wikimedia\Assert\Assert; |
| 10 | |
| 11 | /** |
| 12 | * Holds the Elastic search requests meant to be sent to the _msearch endpoint. |
| 13 | * Users attach a search request identified by a key and after the search requests have been submitted |
| 14 | * to the backend the response can later then be retrieved using that same key. |
| 15 | * @see MSearchResponses |
| 16 | */ |
| 17 | class MSearchRequests { |
| 18 | /** |
| 19 | * @var Search[] |
| 20 | */ |
| 21 | private $requests = []; |
| 22 | |
| 23 | /** |
| 24 | * @param string $key |
| 25 | * @param Search $search |
| 26 | * @return self |
| 27 | */ |
| 28 | public static function build( $key, Search $search ): self { |
| 29 | $self = new self(); |
| 30 | $self->addRequest( $key, $search ); |
| 31 | return $self; |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * @param string $key |
| 36 | * @param Search $search |
| 37 | */ |
| 38 | public function addRequest( $key, Search $search ) { |
| 39 | Assert::parameter( !isset( $this->requests[$key] ), '$key', 'duplicated key' ); |
| 40 | $this->requests[$key] = $search; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * @return Search[] |
| 45 | */ |
| 46 | public function getRequests(): array { |
| 47 | return $this->requests; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * @param \Elastica\ResultSet[] $resultSets |
| 52 | * @return MSearchResponses |
| 53 | */ |
| 54 | public function toMSearchResponses( array $resultSets ): MSearchResponses { |
| 55 | Assert::parameter( count( $resultSets ) === count( $this->requests ), '$responses', |
| 56 | 'must have as many responses as requests (wanted ' . count( $this->requests ) . ' received ' . count( $resultSets ) . ')' ); |
| 57 | $mi = new MultipleIterator( MultipleIterator::MIT_NEED_ALL ); |
| 58 | $mi->attachIterator( new ArrayIterator( $this->requests ) ); |
| 59 | $mi->attachIterator( new ArrayIterator( $resultSets ) ); |
| 60 | $resultSetsWithKeys = []; |
| 61 | foreach ( $mi as $k => $v ) { |
| 62 | $resultSetsWithKeys[$k[0]] = $v[1]; |
| 63 | } |
| 64 | |
| 65 | return new MSearchResponses( $resultSetsWithKeys, $this->requests ); |
| 66 | } |
| 67 | |
| 68 | public function failure( Status $status ): MSearchResponses { |
| 69 | return new MSearchResponses( [], [], $status ); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * @param string $description |
| 74 | * @return Status |
| 75 | */ |
| 76 | public function dumpQuery( $description ): Status { |
| 77 | $retval = []; |
| 78 | foreach ( $this->requests as $k => $search ) { |
| 79 | $retval[$k] = [ |
| 80 | 'description' => $description, |
| 81 | 'path' => $search->getPath(), |
| 82 | 'params' => $search->getOptions(), |
| 83 | 'query' => $search->getQuery()->toArray(), |
| 84 | 'options' => $search->getOptions(), |
| 85 | ]; |
| 86 | } |
| 87 | return Status::newGood( $retval ); |
| 88 | } |
| 89 | } |