|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace SpameriTests\ElasticQuery; |
| 4 | + |
| 5 | +require_once __DIR__ . '/../bootstrap.php'; |
| 6 | + |
| 7 | +class ElasticQuery extends \Tester\TestCase |
| 8 | +{ |
| 9 | + |
| 10 | + private const INDEX = 'spameri_test_elastic_query'; |
| 11 | + |
| 12 | + |
| 13 | + public function setUp() : void |
| 14 | + { |
| 15 | + $ch = \curl_init(); |
| 16 | + \curl_setopt($ch, CURLOPT_URL, 'localhost:9200/' . self::INDEX); |
| 17 | + \curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
| 18 | + \curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); |
| 19 | + \curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']); |
| 20 | + |
| 21 | + \curl_exec($ch); |
| 22 | + \curl_close($ch); |
| 23 | + |
| 24 | + /// === |
| 25 | + |
| 26 | + $ch = \curl_init(); |
| 27 | + \curl_setopt($ch, \CURLOPT_URL, 'localhost:9200/' . self::INDEX . '/_mapping'); |
| 28 | + \curl_setopt($ch, \CURLOPT_RETURNTRANSFER, 1); |
| 29 | + \curl_setopt($ch, \CURLOPT_CUSTOMREQUEST, 'PUT'); |
| 30 | + \curl_setopt($ch, \CURLOPT_HTTPHEADER, ['Content-Type: application/json']); |
| 31 | + \curl_setopt( |
| 32 | + $ch, \CURLOPT_POSTFIELDS, |
| 33 | + \json_encode([ |
| 34 | + 'properties' => [ |
| 35 | + 'name' => [ |
| 36 | + 'type' => \Spameri\ElasticQuery\Mapping\AllowedValues::TYPE_TEXT, |
| 37 | + ], |
| 38 | + 'year' => [ |
| 39 | + 'type' => \Spameri\ElasticQuery\Mapping\AllowedValues::TYPE_INTEGER, |
| 40 | + ] |
| 41 | + ], |
| 42 | + ]) |
| 43 | + ); |
| 44 | + |
| 45 | + \curl_exec($ch); |
| 46 | + \curl_close($ch); |
| 47 | + } |
| 48 | + |
| 49 | + |
| 50 | + public function testCreate() : void |
| 51 | + { |
| 52 | + $match = new \Spameri\ElasticQuery\Query\Match( |
| 53 | + 'name', |
| 54 | + 'Avengers', |
| 55 | + 1.0, |
| 56 | + new \Spameri\ElasticQuery\Query\Match\Fuzziness( |
| 57 | + \Spameri\ElasticQuery\Query\Match\Fuzziness::AUTO |
| 58 | + ), |
| 59 | + 2, |
| 60 | + \Spameri\ElasticQuery\Query\Match\Operator::OR |
| 61 | + ); |
| 62 | + $term = new \Spameri\ElasticQuery\Query\Term( |
| 63 | + 'name', |
| 64 | + 'Avengers', |
| 65 | + 1.0 |
| 66 | + ); |
| 67 | + |
| 68 | + $document = new \Spameri\ElasticQuery\Document( |
| 69 | + self::INDEX, |
| 70 | + new \Spameri\ElasticQuery\Document\Body\Plain( |
| 71 | + ( |
| 72 | + new \Spameri\ElasticQuery\ElasticQuery( |
| 73 | + new \Spameri\ElasticQuery\Query\QueryCollection( |
| 74 | + new \Spameri\ElasticQuery\Query\MustCollection( |
| 75 | + $match |
| 76 | + ) |
| 77 | + ), |
| 78 | + new \Spameri\ElasticQuery\Filter\FilterCollection( |
| 79 | + new \Spameri\ElasticQuery\Query\MustCollection( |
| 80 | + $term |
| 81 | + ) |
| 82 | + ), |
| 83 | + new \Spameri\ElasticQuery\Options\SortCollection( |
| 84 | + new \Spameri\ElasticQuery\Options\Sort( |
| 85 | + 'year', |
| 86 | + \Spameri\ElasticQuery\Options\Sort::ASC |
| 87 | + ) |
| 88 | + ), |
| 89 | + new \Spameri\ElasticQuery\Aggregation\AggregationCollection( |
| 90 | + new \Spameri\ElasticQuery\Filter\FilterCollection( |
| 91 | + new \Spameri\ElasticQuery\Query\MustCollection( |
| 92 | + $term |
| 93 | + ) |
| 94 | + ), |
| 95 | + new \Spameri\ElasticQuery\Aggregation\LeafAggregationCollection( |
| 96 | + 'year_agg', |
| 97 | + NULL, |
| 98 | + new \Spameri\ElasticQuery\Aggregation\Term( |
| 99 | + 'year' |
| 100 | + ) |
| 101 | + ) |
| 102 | + ), |
| 103 | + NULL, |
| 104 | + NULL, |
| 105 | + new \Spameri\ElasticQuery\Options( |
| 106 | + 10, |
| 107 | + 1 |
| 108 | + ) |
| 109 | + ) |
| 110 | + )->toArray() |
| 111 | + ) |
| 112 | + ); |
| 113 | + |
| 114 | + $ch = \curl_init(); |
| 115 | + \curl_setopt($ch, \CURLOPT_URL, 'localhost:9200/' . $document->index() . '/_search'); |
| 116 | + \curl_setopt($ch, \CURLOPT_RETURNTRANSFER, 1); |
| 117 | + \curl_setopt($ch, \CURLOPT_CUSTOMREQUEST, 'GET'); |
| 118 | + \curl_setopt($ch, \CURLOPT_HTTPHEADER, ['Content-Type: application/json']); |
| 119 | + \curl_setopt( |
| 120 | + $ch, \CURLOPT_POSTFIELDS, |
| 121 | + \json_encode($document->toArray()['body']) |
| 122 | + ); |
| 123 | + |
| 124 | + \Tester\Assert::noError(static function () use ($ch) { |
| 125 | + $response = \curl_exec($ch); |
| 126 | + $resultMapper = new \Spameri\ElasticQuery\Response\ResultMapper(); |
| 127 | + /** @var \Spameri\ElasticQuery\Response\ResultSearch $result */ |
| 128 | + $result = $resultMapper->map(\json_decode($response, TRUE)); |
| 129 | + \Tester\Assert::type('int', $result->stats()->total()); |
| 130 | + }); |
| 131 | + |
| 132 | + \curl_close($ch); |
| 133 | + } |
| 134 | + |
| 135 | + |
| 136 | + public function tearDown() : void |
| 137 | + { |
| 138 | + $ch = \curl_init(); |
| 139 | + \curl_setopt($ch, \CURLOPT_URL, 'localhost:9200/' . self::INDEX); |
| 140 | + \curl_setopt($ch, \CURLOPT_RETURNTRANSFER, 1); |
| 141 | + \curl_setopt($ch, \CURLOPT_CUSTOMREQUEST, 'DELETE'); |
| 142 | + \curl_setopt($ch, \CURLOPT_HTTPHEADER, ['Content-Type: application/json']); |
| 143 | + |
| 144 | + \curl_exec($ch); |
| 145 | + \curl_close($ch); |
| 146 | + } |
| 147 | + |
| 148 | +} |
| 149 | + |
| 150 | +(new ElasticQuery())->run(); |
0 commit comments