From 96d4f5ac0a917d5cd3226e8a24e0c2ca17dc9486 Mon Sep 17 00:00:00 2001 From: Haydar Kulekci Date: Sat, 7 Oct 2023 21:10:04 +0300 Subject: [PATCH] Knn functional tests elasticsearch version changed as 8.4.0 for github ci/cd --- .github/workflows/test-application.yaml | 2 +- src/SearchEndpoint/KnnEndpoint.php | 16 +++- .../AbstractElasticsearchTestCase.php | 21 ++--- tests/Functional/Knn/KnnTest.php | 85 +++++++++++++++++++ 4 files changed, 109 insertions(+), 15 deletions(-) create mode 100644 tests/Functional/Knn/KnnTest.php diff --git a/.github/workflows/test-application.yaml b/.github/workflows/test-application.yaml index 7c93b42e..607dbae9 100644 --- a/.github/workflows/test-application.yaml +++ b/.github/workflows/test-application.yaml @@ -50,7 +50,7 @@ jobs: services: elasticsearch: - image: elasticsearch:8.0.0 + image: elasticsearch:8.4.0 ports: - 9200:9200 env: diff --git a/src/SearchEndpoint/KnnEndpoint.php b/src/SearchEndpoint/KnnEndpoint.php index 4bd89c00..0cc55676 100644 --- a/src/SearchEndpoint/KnnEndpoint.php +++ b/src/SearchEndpoint/KnnEndpoint.php @@ -42,14 +42,22 @@ public function normalize( $format = null, array $context = [] ): array|string|int|float|bool { - $output = []; - if (count($this->getAll()) > 0) { + $knns = $this->getAll(); + if (count($knns) === 1) { /** @var Knn $knn */ - foreach ($this->getAll() as $knn) { + $knn = array_values($knns)[0]; + return $knn->toArray(); + } + + if (count($knns) > 1) { + $output = []; + /** @var Knn $knn */ + foreach ($knns as $knn) { $output[] = $knn->toArray(); } + return $output; } - return $output; + return []; } } diff --git a/tests/Functional/AbstractElasticsearchTestCase.php b/tests/Functional/AbstractElasticsearchTestCase.php index 6fa1a107..67b175e7 100644 --- a/tests/Functional/AbstractElasticsearchTestCase.php +++ b/tests/Functional/AbstractElasticsearchTestCase.php @@ -21,7 +21,7 @@ abstract class AbstractElasticsearchTestCase extends TestCase /** * Test index name in the elasticsearch. */ - const INDEX_NAME = 'elasticsaerch-dsl-test'; + const INDEX_NAME = 'elasticsearch-dsl-test'; /** * @var Client @@ -35,17 +35,18 @@ protected function setUp(): void { parent::setUp(); - $this->client = ClientBuilder::create()->build(); + $this->client = ClientBuilder::create() + ->build(); $this->deleteIndex(); - $this->client->indices()->create( - array_filter( - [ - 'index' => self::INDEX_NAME, - 'mapping' => $this->getMapping() - ] - ) - ); + $params = [ + 'index' => self::INDEX_NAME + ]; + if ($this->getMapping()) { + $params['body']['mappings'] = $this->getMapping(); + } + + $response = $this->client->indices()->create($params); $bulkBody = []; diff --git a/tests/Functional/Knn/KnnTest.php b/tests/Functional/Knn/KnnTest.php new file mode 100644 index 00000000..8eae63fc --- /dev/null +++ b/tests/Functional/Knn/KnnTest.php @@ -0,0 +1,85 @@ + + */ + +namespace ONGR\ElasticsearchDSL\Tests\Functional\Knn; + +use ONGR\ElasticsearchDSL\Aggregation\Bucketing\DateHistogramAggregation; +use ONGR\ElasticsearchDSL\Knn\Knn; +use ONGR\ElasticsearchDSL\Query\TermLevel\TermQuery; +use ONGR\ElasticsearchDSL\Search; +use ONGR\ElasticsearchDSL\Tests\Functional\AbstractElasticsearchTestCase; + +class KnnTest extends AbstractElasticsearchTestCase +{ + /** + * {@inheritdoc} + */ + protected function getDataArray(): array + { + return [ + 'knn_data' => [ + 'doc_1' => [ + 'label' => 1, + 'vector_field' => [1, 2, 3], + ], + 'doc_2' => [ + 'label' => 1, + 'vector_field' => [1, 2, 4], + ], + 'doc_3' => [ + 'label' => 2, + 'vector_field' => [1, 2, 30], + ], + ] + ]; + } + + protected function getMapping(): array + { + return [ + 'properties' => [ + 'label' => [ + 'type' => 'long' + ], + 'vector_field' => [ + 'type' => 'dense_vector', + 'similarity' => 'cosine', + 'index' => true, + 'dims' => 3 + ] + ] + ]; + } + + /** + * Match all test + */ + public function testKnnSearch(): void + { + $knn = new Knn('vector_field', [1, 2, 3], 1, 1); + + $search = new Search(); + $search->addKnn($knn); + $results = $this->executeSearch($search, true); + $this->assertCount(1, $results['hits']['hits']); + $this->assertEquals('doc_1', $results['hits']['hits'][0]['_id']); + } + + /** + * Match all test + */ + public function testKnnSearchWithFilter(): void + { + $knn = new Knn('vector_field', [1, 2, 3], 1, 1); + $knn->setFilter(new TermQuery('label', 2)); + + $search = new Search(); + $search->addKnn($knn); + $results = $this->executeSearch($search, true); + $this->assertCount(1, $results['hits']['hits']); + $this->assertEquals('doc_3', $results['hits']['hits'][0]['_id']); + } +}