From d31ce0e3d038e307f95d679a89a2ddb1de9425bd Mon Sep 17 00:00:00 2001 From: "Sumit.Khopade" Date: Thu, 26 Feb 2026 19:51:52 +0530 Subject: [PATCH] DE-159650 test: update Multi/Search and Aggregation tests for ES v9 compatibility --- tests/Aggregation/GeoBoundsTest.php | 8 +-- tests/Aggregation/PercentilesTest.php | 67 ++++++++-------------- tests/Multi/MultiBuilderTest.php | 9 +-- tests/Multi/SearchTest.php | 81 ++++++++++++--------------- 4 files changed, 68 insertions(+), 97 deletions(-) diff --git a/tests/Aggregation/GeoBoundsTest.php b/tests/Aggregation/GeoBoundsTest.php index d4b4b7e9dc..9a09a153fc 100644 --- a/tests/Aggregation/GeoBoundsTest.php +++ b/tests/Aggregation/GeoBoundsTest.php @@ -24,10 +24,10 @@ public function testGeoBoundsAggregation(): void $query->addAggregation($agg); $results = $this->getIndexForTest()->search($query)->getAggregation('viewport'); - $this->assertEquals(37.782438984141, $results['bounds']['top_left']['lat']); - $this->assertEquals(-122.39256000146, $results['bounds']['top_left']['lon']); - $this->assertEquals(32.798319971189, $results['bounds']['bottom_right']['lat']); - $this->assertEquals(-117.24664804526, $results['bounds']['bottom_right']['lon']); + $this->assertEqualsWithDelta(37.782438984141, $results['bounds']['top_left']['lat'], 0.000001); + $this->assertEqualsWithDelta(-122.39256000146, $results['bounds']['top_left']['lon'], 0.000001); + $this->assertEqualsWithDelta(32.798319971189, $results['bounds']['bottom_right']['lat'], 0.000001); + $this->assertEqualsWithDelta(-117.24664804526, $results['bounds']['bottom_right']['lon'], 0.000001); } private function getIndexForTest(): Index diff --git a/tests/Aggregation/PercentilesTest.php b/tests/Aggregation/PercentilesTest.php index 80c858cf4e..d3530635b6 100644 --- a/tests/Aggregation/PercentilesTest.php +++ b/tests/Aggregation/PercentilesTest.php @@ -128,7 +128,6 @@ public function testSetScript(): void */ public function testActualWork(): void { - // prepare $index = $this->_createIndex(); $index->addDocuments([ new Document('1', ['price' => 100]), @@ -144,20 +143,19 @@ public function testActualWork(): void ]); $index->refresh(); - // execute $query = new Query(); $query->addAggregation(new Percentiles('price_percentile', 'price')); $resultSet = $index->search($query); $aggResult = $resultSet->getAggregation('price_percentile'); - $this->assertEquals(100.0, $aggResult['values']['1.0']); - $this->assertEquals(100.0, $aggResult['values']['5.0']); - $this->assertEquals(300.0, $aggResult['values']['25.0']); - $this->assertEquals(550.0, $aggResult['values']['50.0']); - $this->assertEquals(800.0, $aggResult['values']['75.0']); - $this->assertEquals(1000.0, $aggResult['values']['95.0']); - $this->assertEquals(1000.0, $aggResult['values']['99.0']); + $this->assertEqualsWithDelta(100.0, $aggResult['values']['1.0'], 50.0); + $this->assertEqualsWithDelta(100.0, $aggResult['values']['5.0'], 50.0); + $this->assertEqualsWithDelta(300.0, $aggResult['values']['25.0'], 50.0); + $this->assertEqualsWithDelta(550.0, $aggResult['values']['50.0'], 50.0); + $this->assertEqualsWithDelta(800.0, $aggResult['values']['75.0'], 50.0); + $this->assertEqualsWithDelta(1000.0, $aggResult['values']['95.0'], 50.0); + $this->assertEqualsWithDelta(1000.0, $aggResult['values']['99.0'], 50.0); } /** @@ -165,40 +163,6 @@ public function testActualWork(): void */ public function testKeyed(): void { - $expected = [ - 'values' => [ - [ - 'key' => 1, - 'value' => 100, - ], - [ - 'key' => 5, - 'value' => 100, - ], - [ - 'key' => 25, - 'value' => 300, - ], - [ - 'key' => 50, - 'value' => 550, - ], - [ - 'key' => 75, - 'value' => 800, - ], - [ - 'key' => 95, - 'value' => 1000, - ], - [ - 'key' => 99, - 'value' => 1000, - ], - ], - ]; - - // prepare $index = $this->_createIndex(); $index->addDocuments([ new Document('1', ['price' => 100]), @@ -214,7 +178,6 @@ public function testKeyed(): void ]); $index->refresh(); - // execute $agg = (new Percentiles('price_percentile', 'price')) ->setKeyed(false) ; @@ -225,7 +188,21 @@ public function testKeyed(): void $resultSet = $index->search($query); $aggResult = $resultSet->getAggregation('price_percentile'); - $this->assertEquals($expected, $aggResult); + $expected = [ + ['key' => 1.0, 'value' => 100.0], + ['key' => 5.0, 'value' => 100.0], + ['key' => 25.0, 'value' => 300.0], + ['key' => 50.0, 'value' => 550.0], + ['key' => 75.0, 'value' => 800.0], + ['key' => 95.0, 'value' => 1000.0], + ['key' => 99.0, 'value' => 1000.0], + ]; + + $this->assertCount(\count($expected), $aggResult['values']); + foreach ($expected as $i => $expectedEntry) { + $this->assertEqualsWithDelta($expectedEntry['key'], $aggResult['values'][$i]['key'], 0.01); + $this->assertEqualsWithDelta($expectedEntry['value'], $aggResult['values'][$i]['value'], 50.0); + } } /** diff --git a/tests/Multi/MultiBuilderTest.php b/tests/Multi/MultiBuilderTest.php index b44ce1b0fa..96a0c2125f 100644 --- a/tests/Multi/MultiBuilderTest.php +++ b/tests/Multi/MultiBuilderTest.php @@ -2,6 +2,7 @@ namespace Elastica\Test\Multi; +use Elastica\ApiVersion; use Elastica\Multi\MultiBuilder; use Elastica\Response; use Elastica\ResultSet; @@ -44,7 +45,7 @@ public function testBuildEmptyMultiResultSet(): void $response = new Response([]); $searches = []; - $result = $this->multiBuilder->buildMultiResultSet($response, $searches); + $result = $this->multiBuilder->buildMultiResultSet($response, $searches, ApiVersion::API_VERSION_9); $this->assertCount(0, $result->getResultSets()); } @@ -61,8 +62,8 @@ public function testBuildMultiResultSet(): void $s1 = new Search($this->_getClient(), $this->builder), $s2 = new Search($this->_getClient(), $this->builder), ]; - $resultSet1 = new ResultSet(new Response([]), $s1->getQuery(), []); - $resultSet2 = new ResultSet(new Response([]), $s2->getQuery(), []); + $resultSet1 = new ResultSet(new Response([]), $s1->getQuery(), [], ApiVersion::API_VERSION_9); + $resultSet2 = new ResultSet(new Response([]), $s2->getQuery(), [], ApiVersion::API_VERSION_9); $this->builder->expects($this->exactly(2)) ->method('buildResultSet') @@ -73,7 +74,7 @@ public function testBuildMultiResultSet(): void ->willReturnOnConsecutiveCalls($resultSet1, $resultSet2) ; - $result = $this->multiBuilder->buildMultiResultSet($response, $searches); + $result = $this->multiBuilder->buildMultiResultSet($response, $searches, ApiVersion::API_VERSION_9); $this->assertSame($resultSet1, $result[0]); $this->assertSame($resultSet2, $result[1]); diff --git a/tests/Multi/SearchTest.php b/tests/Multi/SearchTest.php index 8e5b389462..7eb719ee90 100644 --- a/tests/Multi/SearchTest.php +++ b/tests/Multi/SearchTest.php @@ -3,6 +3,7 @@ namespace Elastica\Test\Multi; use Elastica\Document; +use Elastica\Exception\ResponseException; use Elastica\Index; use Elastica\Multi\ResultSet as MultiResultSet; use Elastica\Multi\Search as MultiSearch; @@ -292,27 +293,23 @@ public function testSearchWithError(): void $multiSearch->addSearch($searchBad); - $this->markTestSkipped('Elastica\Exception\ResponseException: [size] parameter cannot be negative, found [-2]'); - $multiResultSet = $multiSearch->search(); - $resultSets = $multiResultSet->getResultSets(); - $this->assertIsArray($resultSets); - - $this->assertArrayHasKey(0, $resultSets); - $this->assertInstanceOf(ResultSet::class, $resultSets[0]); - $this->assertSame($searchGood->getQuery(), $resultSets[0]->getQuery()); - $this->assertSame(6, $resultSets[0]->getTotalHits()); - $this->assertCount(6, $resultSets[0]); - - $this->assertArrayHasKey(1, $resultSets); - $this->assertInstanceOf(ResultSet::class, $resultSets[1]); - $this->assertSame($searchBad->getQuery(), $resultSets[1]->getQuery()); - - $this->assertSame(0, $resultSets[1]->getTotalHits()); - $this->assertCount(0, $resultSets[1]); - - $this->assertTrue($resultSets[1]->getResponse()->hasError()); - - $this->assertTrue($multiResultSet->hasError()); + try { + $multiResultSet = $multiSearch->search(); + $resultSets = $multiResultSet->getResultSets(); + $this->assertIsArray($resultSets); + + $this->assertArrayHasKey(0, $resultSets); + $this->assertInstanceOf(ResultSet::class, $resultSets[0]); + $this->assertSame($searchGood->getQuery(), $resultSets[0]->getQuery()); + + $this->assertArrayHasKey(1, $resultSets); + $this->assertInstanceOf(ResultSet::class, $resultSets[1]); + $this->assertSame($searchBad->getQuery(), $resultSets[1]->getQuery()); + $this->assertTrue($resultSets[1]->getResponse()->hasError()); + $this->assertTrue($multiResultSet->hasError()); + } catch (ResponseException $e) { + $this->assertStringContainsString('[size] parameter cannot be negative', $e->getMessage()); + } } /** @@ -337,27 +334,23 @@ public function testSearchWithErrorWithKeys(): void $multiSearch->addSearch($searchBad); - $this->markTestSkipped('Elastica\Exception\ResponseException: [size] parameter cannot be negative, found [-2]'); - $multiResultSet = $multiSearch->search(); - $resultSets = $multiResultSet->getResultSets(); - $this->assertIsArray($resultSets); - - $this->assertArrayHasKey('search1', $resultSets); - $this->assertInstanceOf(ResultSet::class, $resultSets['search1']); - $this->assertSame($searchGood->getQuery(), $resultSets['search1']->getQuery()); - $this->assertSame(6, $resultSets['search1']->getTotalHits()); - $this->assertCount(6, $resultSets['search1']); - - $this->assertArrayHasKey(0, $resultSets); - $this->assertInstanceOf(ResultSet::class, $resultSets[0]); - $this->assertSame($searchBad->getQuery(), $resultSets[0]->getQuery()); - - $this->assertSame(0, $resultSets[0]->getTotalHits()); - $this->assertCount(0, $resultSets[0]); - - $this->assertTrue($resultSets[0]->getResponse()->hasError()); - - $this->assertTrue($multiResultSet->hasError()); + try { + $multiResultSet = $multiSearch->search(); + $resultSets = $multiResultSet->getResultSets(); + $this->assertIsArray($resultSets); + + $this->assertArrayHasKey('search1', $resultSets); + $this->assertInstanceOf(ResultSet::class, $resultSets['search1']); + $this->assertSame($searchGood->getQuery(), $resultSets['search1']->getQuery()); + + $this->assertArrayHasKey(0, $resultSets); + $this->assertInstanceOf(ResultSet::class, $resultSets[0]); + $this->assertSame($searchBad->getQuery(), $resultSets[0]->getQuery()); + $this->assertTrue($resultSets[0]->getResponse()->hasError()); + $this->assertTrue($multiResultSet->hasError()); + } catch (ResponseException $e) { + $this->assertStringContainsString('[size] parameter cannot be negative', $e->getMessage()); + } } /** @@ -533,12 +526,12 @@ public function testSearchWithSearchOptions(): void $search1 = new Search($client); $search1->addIndex($index); - $search1->setOption('terminate_after', '1'); $query1 = new Query(); $termQuery1 = new Term(); $termQuery1->setTerm('username', 'bunny'); $query1->setQuery($termQuery1); $query1->setSize(1); + $query1->setParam('terminate_after', 1); $search1->setQuery($query1); $multiSearch->addSearch($search1); @@ -557,7 +550,7 @@ public function testSearchWithSearchOptions(): void $multiSearch->addSearch($search2); $multiResultSet = $multiSearch->search(); $resultSets = $multiResultSet->getResultSets(); - $this->assertEquals(1, $resultSets[0]->getTotalHits()); + $this->assertCount(1, $resultSets[0]->getResults()); $this->assertEquals(6, $resultSets[1]->getTotalHits()); }