From b674dfde7f8dae36cdf41fed09794e2556120d06 Mon Sep 17 00:00:00 2001 From: Duncan McClean <19637309+duncanmcclean@users.noreply.github.com> Date: Mon, 16 Oct 2023 15:27:50 +0100 Subject: [PATCH 1/3] Rad Pack-ify (#21) * Rad Pack-ify * Disable testing workflow if we have no tests * Setup Pint * wip * Remove config file * wip * wip --- .github/ISSUE_TEMPLATE/bug_report.md | 2 +- .github/ISSUE_TEMPLATE/config.yml | 4 +- .github/workflows/php-cs-fixer.yml | 23 ----------- .github/workflows/pint-fix.yml | 28 ++++++++++++++ .github/workflows/pint-lint.yml | 20 ++++++++++ .github/workflows/run-tests.yml | 3 +- .php-cs-fixer.dist.php | 43 --------------------- CHANGELOG.md | 33 ++++++++-------- CONTRIBUTING.md | 37 +++++++----------- LICENSE.md | 12 +++--- README.md | 28 +++++++++----- composer.json | 27 ++++++------- config/meilisearch.php | 7 ---- pint.json | 11 ++++++ src/{MeiliSearch => Mellisearch}/Index.php | 3 +- src/{MeiliSearch => Mellisearch}/Query.php | 2 +- src/ServiceProvider.php | 23 +++++++++++ src/StatamicMeiliSearchServiceProvider.php | 45 ---------------------- tests/TestCase.php | 6 +-- 19 files changed, 158 insertions(+), 199 deletions(-) delete mode 100644 .github/workflows/php-cs-fixer.yml create mode 100644 .github/workflows/pint-fix.yml create mode 100644 .github/workflows/pint-lint.yml delete mode 100644 .php-cs-fixer.dist.php delete mode 100644 config/meilisearch.php create mode 100644 pint.json rename src/{MeiliSearch => Mellisearch}/Index.php (98%) rename src/{MeiliSearch => Mellisearch}/Query.php (78%) create mode 100644 src/ServiceProvider.php delete mode 100644 src/StatamicMeiliSearchServiceProvider.php diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md index 10e5a9d..ebbf64d 100644 --- a/.github/ISSUE_TEMPLATE/bug_report.md +++ b/.github/ISSUE_TEMPLATE/bug_report.md @@ -12,7 +12,7 @@ A clear and concise description of what the bug is. **Versions** You can use `composer show` to get the version numbers of: -- elvenstar/statamic-meilisearch package version: +- statamic-rad-pack/mellisearch package version: PHP version: Laravel version: diff --git a/.github/ISSUE_TEMPLATE/config.yml b/.github/ISSUE_TEMPLATE/config.yml index 577aff6..78f21ac 100644 --- a/.github/ISSUE_TEMPLATE/config.yml +++ b/.github/ISSUE_TEMPLATE/config.yml @@ -1,8 +1,8 @@ blank_issues_enabled: false contact_links: - name: Ask a Question - url: https://github.com/elvenstar/statamic-meilisearch/discussions/new?category=q-a + url: https://github.com/statamic-rad-pack/mellisearch/discussions/new?category=q-a about: Ask the community for help - name: Feature Request - url: https://github.com/elvenstar/statamic-meilisearch/discussions/new?category=ideas + url: https://github.com/statamic-rad-pack/mellisearch/discussions/new?category=ideas about: Share ideas for new features diff --git a/.github/workflows/php-cs-fixer.yml b/.github/workflows/php-cs-fixer.yml deleted file mode 100644 index 502dbe7..0000000 --- a/.github/workflows/php-cs-fixer.yml +++ /dev/null @@ -1,23 +0,0 @@ -name: Check & fix styling - -on: [push] - -jobs: - php-cs-fixer: - runs-on: ubuntu-latest - - steps: - - name: Checkout code - uses: actions/checkout@v2 - with: - ref: ${{ github.head_ref }} - - - name: Run PHP CS Fixer - uses: docker://oskarstark/php-cs-fixer-ga - with: - args: --config=.php-cs-fixer.dist.php --allow-risky=yes - - - name: Commit changes - uses: stefanzweifel/git-auto-commit-action@v4 - with: - commit_message: Fix styling diff --git a/.github/workflows/pint-fix.yml b/.github/workflows/pint-fix.yml new file mode 100644 index 0000000..0420249 --- /dev/null +++ b/.github/workflows/pint-fix.yml @@ -0,0 +1,28 @@ +name: Fix PHP code style issues + +on: + push: + paths: + - '**.php' + +permissions: + contents: write + +jobs: + fix-php-code-styling: + runs-on: ubuntu-latest + if: github.repository_owner == 'statamic-rad-pack' + + steps: + - name: Checkout code + uses: actions/checkout@v3 + with: + ref: ${{ github.head_ref }} + + - name: Fix PHP code style issues + uses: aglipanci/laravel-pint-action@1.0.0 + + - name: Commit changes + uses: stefanzweifel/git-auto-commit-action@v4 + with: + commit_message: Fix styling diff --git a/.github/workflows/pint-lint.yml b/.github/workflows/pint-lint.yml new file mode 100644 index 0000000..87a91b0 --- /dev/null +++ b/.github/workflows/pint-lint.yml @@ -0,0 +1,20 @@ +name: Lint PHP code style issues + +on: + pull_request: + paths: + - '**.php' + +jobs: + lint-php-code-styling: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Check PHP code style issues + uses: aglipanci/laravel-pint-action@1.0.0 + with: + testMode: true + verboseMode: true diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 1f01134..917ef33 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -1,6 +1,7 @@ name: Tests -on: [push, pull_request] +# on: [push, pull_request] +on: [] concurrency: group: ${{ github.workflow }}-${{ github.ref }} diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php deleted file mode 100644 index ca68de3..0000000 --- a/.php-cs-fixer.dist.php +++ /dev/null @@ -1,43 +0,0 @@ -notPath('bootstrap/*') - ->notPath('storage/*') - ->notPath('resources/view/mail/*') - ->in([ - __DIR__ . '/src', - __DIR__ . '/tests', - ]) - ->name('*.php') - ->notName('*.blade.php') - ->ignoreDotFiles(true) - ->ignoreVCS(true); - -return (new PhpCsFixer\Config) - ->setRules([ - '@PSR12' => true, - 'array_syntax' => ['syntax' => 'short'], - 'ordered_imports' => ['sort_algorithm' => 'alpha'], - 'no_unused_imports' => true, - 'not_operator_with_successor_space' => true, - 'trailing_comma_in_multiline' => true, - 'phpdoc_scalar' => true, - 'unary_operator_spaces' => true, - 'binary_operator_spaces' => true, - 'blank_line_before_statement' => [ - 'statements' => ['break', 'continue', 'declare', 'return', 'throw', 'try'], - ], - 'phpdoc_single_line_var_spacing' => true, - 'phpdoc_var_without_name' => true, - 'class_attributes_separation' => [ - 'elements' => [ - 'method' => 'one' - ], - ], - 'method_argument_space' => [ - 'on_multiline' => 'ensure_fully_multiline', - 'keep_multiple_spaces_after_comma' => true, - ], - 'single_trait_insert_per_statement' => true, - ]) - ->setFinder($finder); diff --git a/CHANGELOG.md b/CHANGELOG.md index 605e87d..ac2f06d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,32 +2,29 @@ All notable changes to this project will be documented in this file. -The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), -and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). - ## 2.0.1 - 2023-08-23 ### What's Changed -- Remove paragraph about versioning system from readme by @lakkes-ra in https://github.com/elvenstar/statamic-meilisearch/pull/15 +- Remove paragraph about versioning system from readme by @lakkes-ra in https://github.com/statamic-rad-pack/mellisearch/pull/15 - Add support for Statamic 3.3 -**Full Changelog**: https://github.com/elvenstar/statamic-meilisearch/compare/2.0.0...2.0.1 +**Full Changelog**: https://github.com/statamic-rad-pack/mellisearch/compare/2.0.0...2.0.1 ## 2.0.0 - 2023-06-20 ### What's Changed -- Allow new version of `meilisearch/meilisearch-php` by @Z3d0X in https://github.com/elvenstar/statamic-meilisearch/pull/10 -- Update for MS v1.0, Statamic 4.0 and Laravel 10 by @lakkes-ra in https://github.com/elvenstar/statamic-meilisearch/pull/13 -- Update Authorization Header in README by @lakkes-ra in https://github.com/elvenstar/statamic-meilisearch/pull/12 +- Allow new version of `meilisearch/meilisearch-php` by @Z3d0X in https://github.com/statamic-rad-pack/mellisearch/pull/10 +- Update for MS v1.0, Statamic 4.0 and Laravel 10 by @lakkes-ra in https://github.com/statamic-rad-pack/mellisearch/pull/13 +- Update Authorization Header in README by @lakkes-ra in https://github.com/statamic-rad-pack/mellisearch/pull/12 ### New Contributors -- @Z3d0X made their first contribution in https://github.com/elvenstar/statamic-meilisearch/pull/10 -- @lakkes-ra made their first contribution in https://github.com/elvenstar/statamic-meilisearch/pull/13 +- @Z3d0X made their first contribution in https://github.com/statamic-rad-pack/mellisearch/pull/10 +- @lakkes-ra made their first contribution in https://github.com/statamic-rad-pack/mellisearch/pull/13 -**Full Changelog**: https://github.com/elvenstar/statamic-meilisearch/compare/1.1.0...2.0.0 +**Full Changelog**: https://github.com/statamic-rad-pack/mellisearch/compare/1.1.0...2.0.0 ## Allow latest Meilisearch client - 2022-08-02 @@ -37,7 +34,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 **Meilisearch 0.28 has a lot of breaking changes in its api. You may need to require an older Meilisearch Client if you use an older Version of Meilisearch.** -**Full Changelog**: https://github.com/elvenstar/statamic-meilisearch/compare/1.0.6...1.1.0 +**Full Changelog**: https://github.com/statamic-rad-pack/mellisearch/compare/1.0.6...1.1.0 ## 1.0.6 - 2022-05-09 @@ -45,7 +42,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Fix missing type hint for `Statamic\Taxonomies\Term` -**Full Changelog**: https://github.com/elvenstar/statamic-meilisearch/compare/1.0.5...1.0.6 +**Full Changelog**: https://github.com/statamic-rad-pack/mellisearch/compare/1.0.5...1.0.6 ## 1.0.5 - 2022-05-09 @@ -53,7 +50,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 Nothing! Just a new version to release changes for real (I forgot to push before I created the previous release 🤦‍♂️) -**Full Changelog**: https://github.com/elvenstar/statamic-meilisearch/compare/1.0.4...1.0.5 +**Full Changelog**: https://github.com/statamic-rad-pack/mellisearch/compare/1.0.4...1.0.5 ## 1.0.4 - 2022-05-09 @@ -61,7 +58,7 @@ Nothing! Just a new version to release changes for real (I forgot to push before - Pass `reference` and not whole object to delete method. -**Full Changelog**: https://github.com/elvenstar/statamic-meilisearch/compare/1.0.3...1.0.4 +**Full Changelog**: https://github.com/statamic-rad-pack/mellisearch/compare/1.0.3...1.0.4 ## 1.0.3 - 2022-05-09 @@ -75,7 +72,7 @@ Add compare url to new changelog updates Use correct target branch for tag. -**Full Changelog**: https://github.com/elvenstar/statamic-meilisearch/compare/1.0.0...1.0.2 +**Full Changelog**: https://github.com/statamic-rad-pack/mellisearch/compare/1.0.0...1.0.2 ## 1.0.1 - 2022-05-09 @@ -96,7 +93,7 @@ In consultation with @tao, I have revised the adapter, and we present the first ### Changed - We no longer follow the MeiliSearch release cycle (the search client is used so superficially that we do not expect any breaking changes here). -- The ID of the search documents will now be transformed according to the MeiliSearch rules https://github.com/elvenstar/statamic-meilisearch/pull/5 . +- The ID of the search documents will now be transformed according to the MeiliSearch rules https://github.com/statamic-rad-pack/mellisearch/pull/5 . - The property `collection` is no longer indexed by default. If you still need it, add this transformer to your search configuration: ```php @@ -119,7 +116,7 @@ In consultation with @tao, I have revised the adapter, and we present the first ``` -- The Package Service Provider has been renamed to `StatamicMeiliSearchServiceProvider`. If you have problems updating, you may have to remove the package with `composer remove elvenstar/statamic-meilisearch` and add it again with `composer require elvenstar/statamic-meilisearch`. +- The Package Service Provider has been renamed to `StatamicMeiliSearchServiceProvider`. If you have problems updating, you may have to remove the package with `composer remove statamic-rad-pack/mellisearch` and add it again with `composer require statamic-rad-pack/mellisearch`. ### Removed diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 9edf78f..1977b6e 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -2,42 +2,31 @@ Contributions are **welcome** and will be fully **credited**. -Please read and understand the contribution guide before creating an issue or pull request. +We accept contributions via Pull Requests on [Github](https://github.com/statamic-rad-pack/statamic-mellisearch). -## Viability -When requesting or submitting new features, first consider whether it might be useful to others. Open -source projects are used by many developers, who may have entirely different needs to your own. Think about -whether or not your feature is likely to be used by other users of the project. +## Pull Requests -## Procedure +- **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](http://pear.php.net/package/PHP_CodeSniffer). -Before filing an issue: - -- Attempt to replicate the problem, to ensure that it wasn't a coincidental incident. -- Check to make sure your feature suggestion isn't already present within the project. -- Check the pull requests tab to ensure that the bug doesn't have a fix in progress. -- Check the pull requests tab to ensure that the feature isn't already in progress. - -Before submitting a pull request: +- **Add tests!** - Your patch won't be accepted if it doesn't have tests. -- Check the codebase to ensure that your feature doesn't already exist. -- Check the pull requests to ensure that another person hasn't already submitted the feature or fix. +- **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date. -## Requirements +- **Consider our release cycle** - I try to follow [SemVer v2.0.0](http://semver.org/). Randomly breaking public APIs is not an option. -If the project maintainer has any additional requirements, you will find them listed here. +- **Create feature branches** - Don't ask me to pull from your master branch. -- **[PSR-12 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-12-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](http://pear.php.net/package/PHP_CodeSniffer). +- **One pull request per feature** - If you want to do more than one thing, send multiple pull requests. -- **Add tests!** - Your patch won't be accepted if it doesn't have tests. +- **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](http://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting. -- **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date. -- **Consider our release cycle** - We try to follow [SemVer v2.0.0](http://semver.org/). Randomly breaking public APIs is not an option. +## Running Tests -- **One pull request per feature** - If you want to do more than one thing, send multiple pull requests. +``` bash +vendor/bin/phpunit +``` -- **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](http://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting. **Happy coding**! diff --git a/LICENSE.md b/LICENSE.md index 035ce47..61d7517 100644 --- a/LICENSE.md +++ b/LICENSE.md @@ -1,6 +1,6 @@ -The MIT License (MIT) +MIT License -Copyright (c) Daniel Shields +Copyright (c) 2023 Statamic Rad Pack Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -9,13 +9,13 @@ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md index 6e97046..a9398ed 100644 --- a/README.md +++ b/README.md @@ -1,20 +1,17 @@ -# Statamic MeiliSearch Driver +# Statamic Meilisearch Driver -### Few words about Document IDs in MeiliSearch - -When you index your Statamic Entries, the driver will always transform the ID. This is required because MeiliSearch only allows `id` to be a string containing alphanumeric characters (a-Z, 0-9), hyphens (-) and underscores (_). -You can read more about this in the [MeiliSearch documentation](https://docs.meilisearch.com/reference/api/error_codes.html#invalid-document-id) - -As an Entry, Asset, User or Taxonomy reference is a combination of the type, handle/container and ID separated with a `::` (e.g. assets::heros/human01.jpg, categories::cats) this could not be indexed by MeiliSearch. +This addon provides a [Mellisearch](https://www.meilisearch.com/) search driver for Statamic sites. -As a Workaround, we take care add reference while indexing your entries automatically 🎉. +## Requirements -Internally Statamic will use `\Statamic\Facades\Data::find($reference)` to resolve the corresponding Statamic Entry, Asset, User or Taxonomy. +* PHP 8.1+ +* Laravel 9+ +* Statamic 4 ### Installation ```bash -composer require elvenstar/statamic-meilisearch +composer require statamic-rad-pack/mellisearch ``` Add the following variables to your env file: @@ -67,6 +64,17 @@ window.meilisearch = new MeiliSearch({ ``` +### Few words about Document IDs in MeiliSearch + +When you index your Statamic Entries, the driver will always transform the ID. This is required because MeiliSearch only allows `id` to be a string containing alphanumeric characters (a-Z, 0-9), hyphens (-) and underscores (_). +You can read more about this in the [MeiliSearch documentation](https://docs.meilisearch.com/reference/api/error_codes.html#invalid-document-id) + +As an Entry, Asset, User or Taxonomy reference is a combination of the type, handle/container and ID separated with a `::` (e.g. assets::heros/human01.jpg, categories::cats) this could not be indexed by MeiliSearch. + +As a Workaround, we take care add reference while indexing your entries automatically 🎉. + +Internally Statamic will use `\Statamic\Facades\Data::find($reference)` to resolve the corresponding Statamic Entry, Asset, User or Taxonomy. + ### Search Settings Any additional settings you want to define per index can be included in the `statamic/search.php` config file. The settings will be updated when the index is created. diff --git a/composer.json b/composer.json index d10bb41..4d996f2 100644 --- a/composer.json +++ b/composer.json @@ -1,11 +1,12 @@ { - "name": "elvenstar/statamic-meilisearch", - "description": "Statamic MeiliSearch addon", + "name": "statamic-rad-pack/mellisearch", + "description": "MelliSearch search driver for Statamic", "keywords": [ - "spatie", - "laravel-ray" + "statamic", + "mellisearch", + "search" ], - "homepage": "https://github.com/elvenstar/statamic-meilisearch", + "homepage": "https://github.com/statamic-rad-pack/mellisearch", "license": "MIT", "authors": [ { @@ -22,25 +23,25 @@ } ], "require": { - "php": "^8.1|^8.2", + "php": "^8.1", "meilisearch/meilisearch-php": "^1.0", "guzzlehttp/guzzle": "^7.3", "http-interop/http-factory-guzzle": "^1.0", "illuminate/support": "^9.0|^10.0", - "statamic/cms": "^3.2|^3.3|^3.4|^4.0" + "statamic/cms": "^4.0" }, "require-dev": { - "orchestra/testbench-core": "^6.0|^7.0|^8.0", + "orchestra/testbench": "^7.0 || ^8.0", "phpunit/phpunit": "^9.3" }, "autoload": { "psr-4": { - "Elvenstar\\StatamicMeiliSearch\\": "src" + "StatamicRadPack\\Mellisearch\\": "src" } }, "autoload-dev": { "psr-4": { - "Elvenstar\\StatamicMeiliSearch\\Tests\\": "tests" + "StatamicRadPack\\Mellisearch\\Tests\\": "tests" } }, "scripts": { @@ -56,12 +57,12 @@ }, "extra": { "statamic": { - "name": "Statamic Meilisearch", - "description": "Statamic MeiliSearch addon" + "name": "Meilisearch", + "description": "MelliSearch search driver for Statamic" }, "laravel": { "providers": [ - "Elvenstar\\StatamicMeiliSearch\\StatamicMeiliSearchServiceProvider" + "StatamicRadPack\\Mellisearch\\ServiceProvider" ] } } diff --git a/config/meilisearch.php b/config/meilisearch.php deleted file mode 100644 index ddcce3d..0000000 --- a/config/meilisearch.php +++ /dev/null @@ -1,7 +0,0 @@ -bootAddonConfig(); - - $this->bootSearchClient(); - } - - protected function bootAddonConfig() - { - $this->mergeConfigFrom(__DIR__.'/../config/meilisearch.php', 'meilisearch'); - - if ($this->app->runningInConsole()) { - $this->publishes([ - __DIR__.'/../config/meilisearch.php' => config_path('meilisearch.php'), - ], 'statamic-meilisearch'); - } - - return $this; - } - - protected function bootSearchClient() - { - Search::extend('meilisearch', function ($app, array $config, $name) { - $credentials = $config['credentials']; - $url = $credentials['url']; - $masterKey = $credentials['secret']; - - $client = new Client($url, $masterKey); - - return new MeiliSearch\Index($client, $name, $config); - }); - } -} diff --git a/tests/TestCase.php b/tests/TestCase.php index 679d7e7..cef0aff 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -1,9 +1,9 @@ Date: Mon, 16 Oct 2023 15:29:54 +0100 Subject: [PATCH 2/3] Fix indentation --- README.md | 143 ++++++++++++++++++++++++++---------------------------- 1 file changed, 70 insertions(+), 73 deletions(-) diff --git a/README.md b/README.md index a9398ed..32ad65d 100644 --- a/README.md +++ b/README.md @@ -39,18 +39,18 @@ curl \ Add the new driver to the `statamic/search.php` config file: ```php - 'drivers' => [ +'drivers' => [ - // other drivers + // other drivers - 'meilisearch' => [ - 'credentials' => [ - 'url' => env('MEILISEARCH_HOST', 'http://localhost:7700'), - 'secret' => env('MEILISEARCH_KEY', ''), - // 'search_api_key' => env('MEILISEARCH_SEARCH_KEY') - ], + 'meilisearch' => [ + 'credentials' => [ + 'url' => env('MEILISEARCH_HOST', 'http://localhost:7700'), + 'secret' => env('MEILISEARCH_KEY', ''), + // 'search_api_key' => env('MEILISEARCH_SEARCH_KEY') ], ], +], ``` You can optionally add `search_api_key` which makes it easier to call the key on the frontend javascript code: @@ -124,11 +124,8 @@ Add `client_max_body_size` to the http section on `/etc/nginx/nginx.conf`: ``` http { - client_max_body_size 100M; - // other settings - } ``` @@ -187,89 +184,89 @@ If you need a lot more fine-grained control, and need to break content down into ```php private function parseAll() - { - // disable search - Articles::withoutSyncingToSearch(function () { - // process all - $transcripts = Entry::query() - ->where('collection', 'articles') - ->where('published', true) - ->get() - ->each(function ($entry) { - // push individual paragraphs or sentences to a collection - $segments = $entries->customSplitMethod(); - - $segments->each(function ($data) { - try { - $article = new Article($data); - $article->save(); - } catch (\Illuminate\Database\QueryException $e) { - dd($e); - } - }); +{ + // disable search + Articles::withoutSyncingToSearch(function () { + // process all + $transcripts = Entry::query() + ->where('collection', 'articles') + ->where('published', true) + ->get() + ->each(function ($entry) { + // push individual paragraphs or sentences to a collection + $segments = $entries->customSplitMethod(); + + $segments->each(function ($data) { + try { + $article = new Article($data); + $article->save(); + } catch (\Illuminate\Database\QueryException $e) { + dd($e); + } }); - }); + }); + }); - $total = Article::all()->count(); - $this->info("Imported {$total} entries into the articles index."); + $total = Article::all()->count(); + $this->info("Imported {$total} entries into the articles index."); - $this->info("Bulk import the records with: "); - $this->info("php artisan scout:import \"App\Models\Article\" --chunk=100"); - } + $this->info("Bulk import the records with: "); + $this->info("php artisan scout:import \"App\Models\Article\" --chunk=100"); +} ``` 4. Add some Listeners to the EventServiceProvider to watch for update or delete events on the collection (to keep it in sync) ```php - protected $listen = [ - 'Statamic\Events\EntrySaved' => [ - 'App\Listeners\ScoutArticleUpdated', - ], - 'Statamic\Events\EntryDeleted' => [ - 'App\Listeners\ScoutArticleDeleted', - ], - ]; +protected $listen = [ + 'Statamic\Events\EntrySaved' => [ + 'App\Listeners\ScoutArticleUpdated', + ], + 'Statamic\Events\EntryDeleted' => [ + 'App\Listeners\ScoutArticleDeleted', + ], +]; ``` 4. Create the Event Listeners, for example: ```php - public function handle(EntryDeleted $event) - { - if ($event->entry->collectionHandle() !== 'articles') return; +public function handle(EntryDeleted $event) +{ + if ($event->entry->collectionHandle() !== 'articles') return; - // get the ID of the original transcript - $id = $event->entry->id(); + // get the ID of the original transcript + $id = $event->entry->id(); - // delete all from Scout with this origin ID - $paragraphs = Article::where('origin', $id); - $paragraphs->unsearchable(); - $paragraphs->delete(); - } + // delete all from Scout with this origin ID + $paragraphs = Article::where('origin', $id); + $paragraphs->unsearchable(); + $paragraphs->delete(); +} - public function handle(EntrySaved $event) - { - // ... same as above ... +public function handle(EntrySaved $event) +{ + // ... same as above ... - // if state:published - if (!$event->entry->published()) return; + // if state:published + if (!$event->entry->published()) return; - // TODO: split $event->entry into paragraphs again and save them to the database, - // they will re-sync automatically with the Searchables Trait. - } + // TODO: split $event->entry into paragraphs again and save them to the database, + // they will re-sync automatically with the Searchables Trait. +} ``` 5. Create a placeholder, or empty index into the search config so you can create the index on MeiliSearch before importing the existing entries ```php - // required as a placeholder where we store the paragraphs later - 'articles' => [ - 'driver' => 'meilisearch', - 'searchables' => [], // empty - 'settings' => [ - 'filterableAttributes' => ['type', 'entity', 'locale'], - 'distinctAttribute' => 'origin', // if you only want to return one result per entry - // any search settings - ], - ], +// required as a placeholder where we store the paragraphs later +'articles' => [ + 'driver' => 'meilisearch', + 'searchables' => [], // empty + 'settings' => [ + 'filterableAttributes' => ['type', 'entity', 'locale'], + 'distinctAttribute' => 'origin', // if you only want to return one result per entry + // any search settings + ], + ], ``` From 1ad24e3b8d98d5366acfa2b115740c9f47aa5fe8 Mon Sep 17 00:00:00 2001 From: Duncan McClean Date: Mon, 16 Oct 2023 15:30:12 +0100 Subject: [PATCH 3/3] wip --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 32ad65d..bcf025f 100644 --- a/README.md +++ b/README.md @@ -268,5 +268,5 @@ public function handle(EntrySaved $event) 'distinctAttribute' => 'origin', // if you only want to return one result per entry // any search settings ], - ], +], ```