Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 31 additions & 30 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:

services:
typesense:
image: typesense/typesense:27.0.rc21
image: typesense/typesense:30.1
ports:
- 8108:8108/tcp
volumes:
Expand All @@ -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
7 changes: 4 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.idea
/vendor
.DS_Store
composer.lock
.idea
.php-cs-fixer.cache
.phpunit.cache
build/report.junit.xml
composer.lock
/vendor
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
11 changes: 11 additions & 0 deletions src/Typesense/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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']];
}
}
31 changes: 31 additions & 0 deletions tests/Unit/IndexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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('<mark>Entry</mark> <mark>1</mark>', $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' => '<mark>Entry</mark> <mark>1</mark>'], $augmentedData);
}
}
Loading