diff --git a/.github/workflows/test-application.yaml b/.github/workflows/test-application.yaml index 722848bb..ecac1598 100644 --- a/.github/workflows/test-application.yaml +++ b/.github/workflows/test-application.yaml @@ -1,163 +1,177 @@ name: Test application on: - pull_request: - push: - branches: - - '[0-9]+.[0-9]+' - - '[0-9]+.x' + pull_request: + push: + branches: + - '[0-9]+.[0-9]+' + - '[0-9]+.x' jobs: - code-style: - runs-on: ubuntu-latest - - steps: - - name: Checkout project - uses: actions/checkout@v3 - - - name: Install and configure PHP - uses: shivammathur/setup-php@v2 - with: - coverage: none - php-version: 8.1 - - - name: Get composer cache directory - id: composer-cache-dir - run: echo "::set-output name=dir::$(composer config cache-files-dir)" - - - name: Cache dependencies - uses: actions/cache@v2 - id: composer-cache - with: - path: ${{ steps.composer-cache-dir.outputs.dir }} - key: ${{ runner.os }}-composer-${{ hashFiles('composer.lock') }} - restore-keys: | - ${{ runner.os }}-composer- - - - name: Install dependencies - run: composer install --no-interaction --prefer-dist - - - name: Check code style - run: vendor/bin/ecs - - phpstan: - runs-on: ubuntu-latest - - steps: - - name: Checkout project - uses: actions/checkout@v3 - - - name: Install and configure PHP - uses: shivammathur/setup-php@v2 - with: - coverage: none - php-version: 8.1 - - - name: Get composer cache directory - id: composer-cache-dir - run: echo "::set-output name=dir::$(composer config cache-files-dir)" - - - name: Cache dependencies - uses: actions/cache@v2 - id: composer-cache - with: - path: ${{ steps.composer-cache-dir.outputs.dir }} - key: ${{ runner.os }}-composer-${{ hashFiles('composer.lock') }} - restore-keys: | - ${{ runner.os }}-composer- - - - name: Install dependencies - run: composer install --no-interaction --prefer-dist - - - name: Run PHPStan - run: vendor/bin/phpstan analyse - - unit-test: - name: 'Run unit tests with php ${{ matrix.php-version }} with symfony ${{ matrix.symfony-version}}' - runs-on: ubuntu-latest - - strategy: - fail-fast: false - matrix: - include: - - php-version: '8.0' - symfony-version: '5.4.*' - - - php-version: '8.1' - symfony-version: '5.4.*' - - - php-version: '8.1' - symfony-version: '6.0.*' - - - php-version: '8.1' - symfony-version: '6.1.*' - - steps: - - name: Checkout project - uses: actions/checkout@v3 - - - name: Install and configure PHP - uses: shivammathur/setup-php@v2 - with: - coverage: pcov - php-version: ${{ matrix.php-version }} - - - name: Get composer cache directory - id: composer-cache-dir - run: echo "::set-output name=dir::$(composer config cache-files-dir)" - - - name: Cache dependencies - uses: actions/cache@v2 - id: composer-cache - with: - path: ${{ steps.composer-cache-dir.outputs.dir }} - key: ${{ runner.os }}-composer-${{ hashFiles('composer.lock') }} - restore-keys: | - ${{ runner.os }}-composer- - - - name: Install dependencies - run: | - composer validate --strict - composer require --no-update symfony/serializer:${{ matrix.symfony-version }} - composer install --no-interaction --prefer-dist - - - name: Run tests - run: vendor/bin/phpunit --testsuite=unit --coverage-clover=coverage.xml - - functional-test: - name: 'Run functional tests' - runs-on: ubuntu-latest - - steps: - - name: Checkout project - uses: actions/checkout@v3 - - - name: Install and configure PHP - uses: shivammathur/setup-php@v2 - with: - coverage: none - php-version: 8.1 - - - name: Get composer cache directory - id: composer-cache-dir - run: echo "::set-output name=dir::$(composer config cache-files-dir)" - - - name: Cache dependencies - uses: actions/cache@v2 - id: composer-cache - with: - path: ${{ steps.composer-cache-dir.outputs.dir }} - key: ${{ runner.os }}-composer-${{ hashFiles('composer.lock') }} - restore-keys: | - ${{ runner.os }}-composer- - - - name: Install dependencies - run: composer install --no-interaction --prefer-dist - - - name: Setup OpenSearch - uses: ankane/setup-opensearch@v1 - with: - opensearch-version: '1.2' - - - name: Run tests - run: vendor/bin/phpunit --testsuite=functional + code-style: + runs-on: ubuntu-latest + + steps: + - name: Checkout project + uses: actions/checkout@v3 + + - name: Install and configure PHP + uses: shivammathur/setup-php@v2 + with: + coverage: none + php-version: 8.1 + + - name: Get composer cache directory + id: composer-cache-dir + run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT + + - name: Cache dependencies + uses: actions/cache@v3 + id: composer-cache + with: + path: ${{ steps.composer-cache-dir.outputs.dir }} + key: ${{ runner.os }}-composer-${{ hashFiles('composer.lock') }} + restore-keys: | + ${{ runner.os }}-composer- + + - name: Install dependencies + run: composer install --no-interaction --prefer-dist + + - name: Check code style + run: vendor/bin/ecs + + phpstan: + runs-on: ubuntu-latest + + steps: + - name: Checkout project + uses: actions/checkout@v3 + + - name: Install and configure PHP + uses: shivammathur/setup-php@v2 + with: + coverage: none + php-version: 8.1 + + - name: Get composer cache directory + id: composer-cache-dir + run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT + + - name: Cache dependencies + uses: actions/cache@v3 + id: composer-cache + with: + path: ${{ steps.composer-cache-dir.outputs.dir }} + key: ${{ runner.os }}-composer-${{ hashFiles('composer.lock') }} + restore-keys: | + ${{ runner.os }}-composer- + + - name: Install dependencies + run: composer install --no-interaction --prefer-dist + + - name: Run PHPStan + run: vendor/bin/phpstan analyse + + unit-test: + name: 'Run unit tests with php ${{ matrix.php-version }} with symfony ${{ matrix.symfony-version}}' + runs-on: ubuntu-latest + + strategy: + fail-fast: false + matrix: + include: + - php-version: '8.0' + symfony-version: '5.4.*' + + - php-version: '8.1' + symfony-version: '5.4.*' + + - php-version: '8.1' + symfony-version: '6.0.*' + + - php-version: '8.1' + symfony-version: '6.1.*' + + - php-version: '8.1' + symfony-version: '6.2.*' + + steps: + - name: Checkout project + uses: actions/checkout@v3 + + - name: Install and configure PHP + uses: shivammathur/setup-php@v2 + with: + coverage: pcov + php-version: ${{ matrix.php-version }} + + - name: Get composer cache directory + id: composer-cache-dir + run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT + + - name: Cache dependencies + uses: actions/cache@v3 + id: composer-cache + with: + path: ${{ steps.composer-cache-dir.outputs.dir }} + key: ${{ runner.os }}-composer-${{ hashFiles('composer.lock') }} + restore-keys: | + ${{ runner.os }}-composer- + + - name: Install dependencies + run: | + composer validate --strict + composer require --no-update symfony/serializer:${{ matrix.symfony-version }} + composer install --no-interaction --prefer-dist + + - name: Run tests + run: vendor/bin/phpunit --testsuite=unit --coverage-clover=coverage.xml + + functional-test: + name: 'Run functional tests with OS ${{ matrix.opensearch-version }}' + runs-on: ubuntu-latest + + strategy: + fail-fast: false + matrix: + include: + - opensearch-version: '1.3' + opensearch-composer-version: '^1' + - opensearch-version: '2.4' + opensearch-composer-version: '^2' + + steps: + - name: Checkout project + uses: actions/checkout@v3 + + - name: Install and configure PHP + uses: shivammathur/setup-php@v2 + with: + coverage: none + php-version: 8.1 + + - name: Get composer cache directory + id: composer-cache-dir + run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT + + - name: Cache dependencies + uses: actions/cache@v3 + id: composer-cache + with: + path: ${{ steps.composer-cache-dir.outputs.dir }} + key: ${{ runner.os }}-composer-${{ hashFiles('composer.lock') }} + restore-keys: | + ${{ runner.os }}-composer- + + - name: Install dependencies + run: | + composer require --no-update opensearch-project/opensearch-php:${{ matrix.opensearch-composer-version }} + composer install --no-interaction --prefer-dist + + - name: Setup OpenSearch + uses: ankane/setup-opensearch@v1 + with: + opensearch-version: ${{ matrix.opensearch-version }} + + - name: Run tests + run: vendor/bin/phpunit --testsuite=functional diff --git a/README.md b/README.md index a88488c9..e51daf77 100644 --- a/README.md +++ b/README.md @@ -3,9 +3,9 @@ This is a fork of the [ongr/elasticsearch-dsl](https://github.com/ongr-io/elasticsearchbundle). With some basic changes to support wider range of Symfony Versions. -| Version | Supported OpenSearch Version | Supported Symfony Version | -|---------|------------------------------|---------------------------| -| `1.x` | `^1.2` | `5.4.*`, `6.0.*`, `6.1.*` | +| Version | Supported OpenSearch Version | Supported Symfony Version | +|---------|------------------------------|------------------------------------| +| `1.x` | `^1.2`, `^2.0` | `5.4.*`, `6.0.*`, `6.1.*`, `6.2.*` | ## Documentation diff --git a/composer.json b/composer.json index 80022959..0f537626 100644 --- a/composer.json +++ b/composer.json @@ -20,13 +20,13 @@ ], "require": { "php": "8.0.* || 8.1.*", - "symfony/serializer": "5.4.* || 6.0.* || 6.1.*", - "opensearch-project/opensearch-php": "^1.0" + "symfony/serializer": "5.4.* || 6.0.* || 6.1.* || 6.2.*", + "opensearch-project/opensearch-php": "^1.0 || ^2.0" }, "require-dev": { "phpunit/phpunit": "^9.5", - "phpstan/phpstan": "^1.8", - "phpstan/phpstan-phpunit": "^1.1", + "phpstan/phpstan": "1.8.11", + "phpstan/phpstan-phpunit": "1.1.3", "rector/rector": "^0.14.5", "symplify/easy-coding-standard": "^11.1" }, diff --git a/phpunit.xml.dist b/phpunit.xml.dist index da9511f2..4b20532d 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -16,13 +16,13 @@ - + ./tests/Unit/ - + ./tests/Functional/ - + ./tests/ diff --git a/src/Aggregation/Metric/TopHitsAggregation.php b/src/Aggregation/Metric/TopHitsAggregation.php index 8e3d46a1..ba19951f 100644 --- a/src/Aggregation/Metric/TopHitsAggregation.php +++ b/src/Aggregation/Metric/TopHitsAggregation.php @@ -49,9 +49,13 @@ class TopHitsAggregation extends AbstractAggregation public function __construct(string $name, ?int $size = null, ?int $from = null, ?BuilderInterface $sort = null) { parent::__construct($name); - $this->setFrom($from); + $this->setSize($size); - $this->addSort($sort); + $this->setFrom($from); + + if ($sort !== null) { + $this->addSort($sort); + } } public function getFrom(): ?int @@ -120,11 +124,14 @@ public function getArray(): array|stdClass $sortsOutput = null; } - $output = array_filter([ - 'sort' => $sortsOutput, - 'size' => $this->getSize(), - 'from' => $this->getFrom(), - ]); + $output = array_filter( + [ + 'sort' => $sortsOutput, + 'size' => $this->getSize(), + 'from' => $this->getFrom(), + ], + static fn ($value) => $value !== null + ); return empty($output) ? new stdClass() : $output; } diff --git a/src/SearchEndpoint/HighlightEndpoint.php b/src/SearchEndpoint/HighlightEndpoint.php index d7e61b94..697a6fb0 100644 --- a/src/SearchEndpoint/HighlightEndpoint.php +++ b/src/SearchEndpoint/HighlightEndpoint.php @@ -50,6 +50,10 @@ public function add(BuilderInterface $builder, ?string $key = null): string throw new OverflowException('Only one highlight can be set'); } + if (! $key) { + $key = bin2hex(random_bytes(30)); + } + $this->key = $key; $this->highlight = $builder; diff --git a/tests/Functional/AbstractOpenSearchTestCase.php b/tests/Functional/AbstractOpenSearchTestCase.php index e1174599..4982dc4b 100644 --- a/tests/Functional/AbstractOpenSearchTestCase.php +++ b/tests/Functional/AbstractOpenSearchTestCase.php @@ -33,23 +33,22 @@ protected function setUp(): void $this->client = ClientBuilder::create()->build(); $this->deleteIndex(); - $this->client->indices()->create( - array_filter( - [ - 'index' => self::INDEX_NAME, - 'mapping' => $this->getMapping(), - ] - ) - ); - $bulkBody = []; - foreach ($this->getDataArray() as $type => $documents) { + $index = self::INDEX_NAME . '_' . $type; + $this->client->indices()->create( + array_filter( + [ + 'index' => $index, + 'mapping' => $this->getMapping(), + ] + ) + ); + foreach ($documents as $id => $document) { $bulkBody[] = [ 'index' => [ - '_index' => self::INDEX_NAME, - '_type' => $type, + '_index' => $index, '_id' => $id, ], ]; @@ -111,16 +110,15 @@ protected function tearDown(): void * Execute search to the opensearch and handle results. * * @param Search $search Search object. - * @param null $type Types to search. Can be several types split by comma. + * @param string $type Type to search. * @param bool $returnRaw Return raw response from the client. * @return array */ - protected function executeSearch(Search $search, $type = null, $returnRaw = false) + protected function executeSearch(Search $search, string $type, bool $returnRaw = false) { $response = $this->client->search( array_filter([ - 'index' => self::INDEX_NAME, - 'type' => $type, + 'index' => self::INDEX_NAME . '_' . $type, 'body' => $search->toArray(), ]) ); @@ -149,7 +147,7 @@ private function deleteIndex() { try { $this->client->indices()->delete([ - 'index' => self::INDEX_NAME, + 'index' => self::INDEX_NAME . '*', ]); } catch (Exception) { // Do nothing. diff --git a/tests/Functional/Query/FunctionScoreQueryTest.php b/tests/Functional/Query/FunctionScoreQueryTest.php index 385ada99..ce03337d 100644 --- a/tests/Functional/Query/FunctionScoreQueryTest.php +++ b/tests/Functional/Query/FunctionScoreQueryTest.php @@ -49,7 +49,7 @@ public function testRandomScore() $search = new Search(); $search->addQuery($fquery); - $results = $this->executeSearch($search); + $results = $this->executeSearch($search, 'product'); $this->assertCount( is_countable($this->getDataArray()['product']) ? count($this->getDataArray()['product']) : 0, @@ -76,7 +76,7 @@ public function testScriptScore() $search = new Search(); $search->addQuery($fquery); - $results = $this->executeSearch($search); + $results = $this->executeSearch($search, 'product'); foreach ($results as $document) { $this->assertLessThanOrEqual(20, $document['price']); diff --git a/tests/Functional/Query/MatchAllQueryTest.php b/tests/Functional/Query/MatchAllQueryTest.php index 0c3e7570..05efd8a8 100644 --- a/tests/Functional/Query/MatchAllQueryTest.php +++ b/tests/Functional/Query/MatchAllQueryTest.php @@ -41,7 +41,7 @@ public function testMatchAll() $search->addQuery($matchAll); $q = $search->getQueries(); - $results = $this->executeSearch($search); + $results = $this->executeSearch($search, 'product'); $this->assertEquals($this->getDataArray()['product'], $results); } diff --git a/tests/Unit/Aggregation/Metric/CardinalityAggregationTest.php b/tests/Unit/Aggregation/Metric/CardinalityAggregationTest.php index f013f76b..d901fad3 100644 --- a/tests/Unit/Aggregation/Metric/CardinalityAggregationTest.php +++ b/tests/Unit/Aggregation/Metric/CardinalityAggregationTest.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace OpenSearchDSL\Tests\Unit\Metric\Aggregation; +namespace OpenSearchDSL\Tests\Unit\Aggregation\Metric; use LogicException; use OpenSearchDSL\Aggregation\Metric\CardinalityAggregation; diff --git a/tests/Unit/Aggregation/Metric/GeoBoundsAggregationTest.php b/tests/Unit/Aggregation/Metric/GeoBoundsAggregationTest.php index 1dac29b3..dcee0c10 100644 --- a/tests/Unit/Aggregation/Metric/GeoBoundsAggregationTest.php +++ b/tests/Unit/Aggregation/Metric/GeoBoundsAggregationTest.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace OpenSearchDSL\Tests\Unit\Metric\Aggregation; +namespace OpenSearchDSL\Tests\Unit\Aggregation\Metric; use LogicException; use OpenSearchDSL\Aggregation\Metric\GeoBoundsAggregation; diff --git a/tests/Unit/Aggregation/Metric/GeoCentroidAggregationTest.php b/tests/Unit/Aggregation/Metric/GeoCentroidAggregationTest.php index 210a2748..e3fd471a 100644 --- a/tests/Unit/Aggregation/Metric/GeoCentroidAggregationTest.php +++ b/tests/Unit/Aggregation/Metric/GeoCentroidAggregationTest.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace OpenSearchDSL\Tests\Unit\Metric\Aggregation; +namespace OpenSearchDSL\Tests\Unit\Aggregation\Metric; use LogicException; use OpenSearchDSL\Aggregation\Metric\GeoCentroidAggregation; diff --git a/tests/Unit/Aggregation/Metric/PercentileRanksAggregationTest.php b/tests/Unit/Aggregation/Metric/PercentileRanksAggregationTest.php index 0c6ab44e..3faffb5b 100644 --- a/tests/Unit/Aggregation/Metric/PercentileRanksAggregationTest.php +++ b/tests/Unit/Aggregation/Metric/PercentileRanksAggregationTest.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace OpenSearchDSL\Tests\Unit\Metric\Aggregation; +namespace OpenSearchDSL\Tests\Unit\Aggregation\Metric; use LogicException; use OpenSearchDSL\Aggregation\Metric\PercentileRanksAggregation; diff --git a/tests/Unit/Aggregation/Metric/PercentilesAggregationTest.php b/tests/Unit/Aggregation/Metric/PercentilesAggregationTest.php index 147a69a2..7b20946e 100644 --- a/tests/Unit/Aggregation/Metric/PercentilesAggregationTest.php +++ b/tests/Unit/Aggregation/Metric/PercentilesAggregationTest.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace OpenSearchDSL\Tests\Unit\Metric\Aggregation; +namespace OpenSearchDSL\Tests\Unit\Aggregation\Metric; use LogicException; use OpenSearchDSL\Aggregation\Metric\PercentilesAggregation; diff --git a/tests/Unit/Aggregation/Metric/StatsAggregationTest.php b/tests/Unit/Aggregation/Metric/StatsAggregationTest.php index 703dd299..8d6eb18f 100644 --- a/tests/Unit/Aggregation/Metric/StatsAggregationTest.php +++ b/tests/Unit/Aggregation/Metric/StatsAggregationTest.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace OpenSearchDSL\Tests\Unit\Metric\Aggregation; +namespace OpenSearchDSL\Tests\Unit\Aggregation\Metric; use OpenSearchDSL\Aggregation\Metric\StatsAggregation; use PHPUnit\Framework\TestCase; diff --git a/tests/Unit/Aggregation/Metric/TopHitsAggregationTest.php b/tests/Unit/Aggregation/Metric/TopHitsAggregationTest.php index df818a97..50f3f930 100644 --- a/tests/Unit/Aggregation/Metric/TopHitsAggregationTest.php +++ b/tests/Unit/Aggregation/Metric/TopHitsAggregationTest.php @@ -9,7 +9,7 @@ * file that was distributed with this source code. */ -namespace OpenSearchDSL\Tests\Unit\Metric\Aggregation; +namespace OpenSearchDSL\Tests\Unit\Aggregation\Metric; use OpenSearchDSL\Aggregation\Metric\TopHitsAggregation; use OpenSearchDSL\Sort\FieldSort;