diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index a9da705..e560758 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -12,7 +12,7 @@ jobs: services: typesense: - image: typesense/typesense:27.0.rc21 + image: typesense/typesense:30.1 ports: - 8108:8108/tcp volumes: @@ -23,36 +23,37 @@ jobs: TYPESENSE_ENABLE_CORS: true strategy: - fail-fast: false - matrix: - os: [ubuntu-latest] - php: [8.2, 8.3] - laravel: [10.*, 11.*, 12.*] - statamic: [5.*] - dependency-version: [prefer-stable] + fail-fast: false + matrix: + os: [ubuntu-latest] + php: [8.2, 8.3] + laravel: [10.*, 11.*, 12.*] + statamic: [5.*] + dependency-version: [prefer-stable] name: P${{ matrix.php }} - L${{ matrix.laravel }} - S${{ matrix.statamic }} - ${{ matrix.dependency-version }} - ${{ matrix.os }} steps: - - name: Checkout code - uses: actions/checkout@v3 - - - name: Setup PHP - uses: shivammathur/setup-php@v2 - with: - php-version: ${{ matrix.php }} - extensions: fileinfo, dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick - coverage: none - - - name: Setup Problem Matches - run: | - echo "::add-matcher::${{ runner.tool_cache }}/php.json" - echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json" - - - name: Install dependencies - run: | - composer require "statamic/cms:${{ matrix.statamic }}" "laravel/framework:${{ matrix.laravel }}" --no-interaction --no-update - composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction - - - name: Execute tests - run: vendor/bin/phpunit + - name: Checkout code + uses: actions/checkout@v3 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + extensions: fileinfo, dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick + coverage: none + ini-values: zend.assertions=-1 + + - name: Setup Problem Matches + run: | + echo "::add-matcher::${{ runner.tool_cache }}/php.json" + echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json" + + - name: Install dependencies + run: | + composer require "statamic/cms:${{ matrix.statamic }}" "laravel/framework:${{ matrix.laravel }}" --no-interaction --no-update + composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction + + - name: Execute tests + run: vendor/bin/phpunit diff --git a/.gitignore b/.gitignore index 4ea6620..0743681 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ -.idea -/vendor .DS_Store -composer.lock +.idea .php-cs-fixer.cache +.phpunit.cache build/report.junit.xml +composer.lock +/vendor diff --git a/composer.json b/composer.json index ada4e3f..c7a7f46 100644 --- a/composer.json +++ b/composer.json @@ -27,7 +27,8 @@ "require-dev": { "laravel/pint": "^1.17", "orchestra/testbench": "^8.14 || ^9.0 || ^10.0", - "phpunit/phpunit": "^10.0 || ^11.0" + "phpunit/phpunit": "^10.0 || ^11.0", + "spatie/laravel-ray": "^1.43" }, "autoload": { "psr-4": { diff --git a/src/Typesense/Index.php b/src/Typesense/Index.php index 4adcb18..82c7081 100644 --- a/src/Typesense/Index.php +++ b/src/Typesense/Index.php @@ -7,6 +7,7 @@ use Statamic\Facades\Blink; use Statamic\Search\Documents; use Statamic\Search\Index as BaseIndex; +use Statamic\Search\Result; use Statamic\Support\Arr; use Typesense\Client; use Typesense\Exceptions\ObjectNotFound; @@ -196,4 +197,14 @@ public function client() { return $this->client; } + + public function extraAugmentedResultData(Result $result) + { + + if (is_null($highlight = Arr::get($result->getRawResult(), 'search_highlight')) && ! empty($highlight)) { + return []; + } + + return ['search_highlight' => Arr::first($highlight)['snippet']]; + } } diff --git a/tests/Unit/IndexTest.php b/tests/Unit/IndexTest.php index bce387e..4d320d4 100644 --- a/tests/Unit/IndexTest.php +++ b/tests/Unit/IndexTest.php @@ -132,4 +132,35 @@ public function it_sorts_by_specified_order() $this->assertSame(['Entry 2', 'Entry 1'], collect($results['results'])->pluck('title')->all()); } + + #[Test] + public function it_adds_search_highlight() + { + $entry1 = Facades\Entry::make() + ->id('test-2') + ->collection('pages') + ->data(['title' => 'Entry 1']) + ->save(); + + $results = Facades\Search::index('typesense_index')->searchUsingApi('Entry 1'); + + $this->assertEquals('Entry 1', $results['results'][0]['search_highlight']['title']['snippet']); + } + + #[Test] + public function it_augments_with_search_highlight() + { + $entry1 = Facades\Entry::make() + ->id('test-2') + ->collection('pages') + ->data(['title' => 'Entry 1']) + ->save(); + + $index = Facades\Search::index('typesense_index'); + $result = $index->search('Entry 1')->get()->first(); + + $augmentedData = $index->extraAugmentedResultData($result); + + $this->assertSame(['search_highlight' => 'Entry 1'], $augmentedData); + } }