├── .github └── workflows │ └── tests.yml ├── CHANGELOG.md ├── CONTRIBUTING.md ├── LICENSE.md ├── README.md ├── composer.json └── src ├── PostgresEngine.php ├── PostgresEngineServiceProvider.php └── TsQuery ├── BaseTsQueryable.php ├── PhraseToTsQuery.php ├── PlainToTsQuery.php ├── ToTsQuery.php ├── TsQueryable.php └── WebSearchToTsQuery.php /.github/workflows/tests.yml: -------------------------------------------------------------------------------- 1 | name: tests 2 | 3 | on: 4 | push: 5 | branches: 6 | - master 7 | pull_request: 8 | 9 | jobs: 10 | tests: 11 | 12 | runs-on: ubuntu-latest 13 | strategy: 14 | fail-fast: true 15 | matrix: 16 | php: [7.2, 7.3, 7.4] 17 | laravel: [^6.0, ^7.0, ^8.0] 18 | scout: [^7.0, ^8.0] 19 | exclude: 20 | - php: 7.2 21 | laravel: ^8.0 22 | - laravel: ^8.0 23 | scout: ^7.0 24 | include: 25 | - php: 8.0 26 | laravel: ^8.0 27 | scout: ^8.0 28 | 29 | name: Test PHP ${{ matrix.php }} - Laravel ${{ matrix.laravel }} - Scout ${{ matrix.scout }} 30 | 31 | steps: 32 | - name: Checkout code 33 | uses: actions/checkout@v2 34 | 35 | - name: Setup PHP 36 | uses: shivammathur/setup-php@v2 37 | with: 38 | php-version: ${{ matrix.php }} 39 | extensions: dom, curl, libxml, mbstring, zip 40 | tools: composer:v2 41 | coverage: none 42 | 43 | - name: Install dependencies 44 | run: | 45 | composer require "illuminate/contracts=${{ matrix.laravel }}" --no-update 46 | composer require "illuminate/database=${{ matrix.laravel }}" --no-update 47 | composer require "illuminate/support=${{ matrix.laravel }}" --no-update 48 | composer require "laravel/scout=${{ matrix.scout }}" --no-update 49 | composer update --prefer-dist --no-interaction --no-progress 50 | 51 | - name: Run tests 52 | run: vendor/bin/phpunit --verbose -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | ## [7.3.0](https://github.com/pmatseykanets/laravel-scout-postgres/releases/tag/v7.3.0) - 2021-01-06 4 | 5 | ### Changed 6 | 7 | - Added PHP 8 support 8 | 9 | ## [7.2.0](https://github.com/pmatseykanets/laravel-scout-postgres/releases/tag/v7.2.0) - 2020-09-24 10 | 11 | ### Changed 12 | 13 | - Reinstated support for Scout 7 14 | 15 | ## [7.1.0](https://github.com/pmatseykanets/laravel-scout-postgres/releases/tag/v7.1.0) - 2020-09-14 16 | 17 | ### Added 18 | 19 | - Added support for Laravel 8 20 | 21 | ## [7.0.0](https://github.com/pmatseykanets/laravel-scout-postgres/releases/tag/v7.0.0) - 2020-03-08 22 | 23 | ### Added 24 | 25 | - Added support for Laravel 7 26 | 27 | ### Removed 28 | 29 | - Removed support for Laravel 5 30 | 31 | ## [6.0.0](https://github.com/pmatseykanets/laravel-scout-postgres/releases/tag/v6.0.0) - 2019-09-19 32 | 33 | ### Added 34 | 35 | - Added support for Laravel 6 36 | 37 | ## [5.0.0](https://github.com/pmatseykanets/laravel-scout-postgres/releases/tag/v5.0.0) - 2019-03-13 38 | 39 | ### Added 40 | 41 | - Added support for Scout 7 42 | 43 | ## [4.0.0](https://github.com/pmatseykanets/laravel-scout-postgres/releases/tag/v4.0.0) - 2018-11-15 44 | 45 | ### Added 46 | 47 | - Added support for Scout 6 48 | 49 | ## [3.1.0](https://github.com/pmatseykanets/laravel-scout-postgres/releases/tag/v3.1.0) - 2018-10-20 50 | 51 | ### Added 52 | 53 | - Added support for `websearch_to_tsquery()` introduced in PostgreSQL 11 54 | 55 | ## [3.0.1](https://github.com/pmatseykanets/laravel-scout-postgres/releases/tag/v3.0.1) - 2018-09-02 56 | 57 | ### Changed 58 | 59 | - Fix `composer.json` 60 | 61 | ## [3.0.0](https://github.com/pmatseykanets/laravel-scout-postgres/releases/tag/v3.0.0) - 2018-09-02 62 | 63 | ### Changed 64 | 65 | - Switched to Scout 5 66 | 67 | ## [2.3.0](https://github.com/pmatseykanets/laravel-scout-postgres/releases/tag/v2.3.0) - 2018-03-31 68 | 69 | ### Added 70 | 71 | - Allow to choose a `tsquery` producing function 72 | 73 | ## [2.2.0](https://github.com/pmatseykanets/laravel-scout-postgres/releases/tag/v2.2.0) - 2018-02-26 74 | 75 | ### Fixed 76 | 77 | - Downgraded dependency versions to support php 7.0+ and Laravel 5.4+ 78 | 79 | ## [2.1.0](https://github.com/pmatseykanets/laravel-scout-postgres/releases/tag/v2.1.0) - 2018-02-23 80 | 81 | ### Added 82 | 83 | - Added support for applying `ORDER BY` clauses set on the builder instance 84 | 85 | ## [2.0.0](https://github.com/pmatseykanets/laravel-scout-postgres/releases/tag/v2.0.0) - 2018-02-09 86 | 87 | ### Changed 88 | 89 | - Switched to Scout 4 (Laravel 5.6) and PHPUnit 7 90 | 91 | ## [1.0.0](https://github.com/pmatseykanets/laravel-scout-postgres/releases/tag/v1.0.0) - 2017-09-03 92 | 93 | ### Added 94 | 95 | - Added Laravel 5.5 support including package auto discovery 96 | 97 | ## [0.5.0](https://github.com/pmatseykanets/laravel-scout-postgres/releases/tag/v0.5.0) - 2017-01-30 98 | 99 | ### Changed 100 | 101 | - Updated dependencies to support Laravel 5.4 and Scout 3.0 102 | 103 | ### Fixed 104 | 105 | - Fall back to phpunit 4.8 106 | 107 | ## [0.4.1](https://github.com/pmatseykanets/laravel-scout-postgres/releases/tag/v0.4.1) - 2017-01-22 108 | 109 | ### Fixed 110 | 111 | - Fixed #7. No longer uses `resolve()` helper to better support Laravel Lumen 112 | 113 | ## [0.4.0](https://github.com/pmatseykanets/laravel-scout-postgres/releases/tag/v0.4.0) - 2017-01-16 114 | 115 | ### Added 116 | 117 | - Made it possible to specify PostgreSQL search config both globally in scout.php or on per model basis 118 | 119 | ### Changed 120 | 121 | - Fixed #6. Check for models that no longer exist but still present in the index (i.e. soft-deleted models) 122 | 123 | ## [0.3.0](https://github.com/pmatseykanets/laravel-scout-postgres/releases/tag/v0.3.0) - 2017-01-04 124 | 125 | ### Changed 126 | 127 | - Updated to Scout 2.0 128 | - Fixed an issue with order by clause when performing a search 129 | 130 | ## [0.2.1](https://github.com/pmatseykanets/laravel-scout-postgres/releases/tag/v0.2.1) - 2016-12-23 131 | 132 | ### Changed 133 | 134 | - Fixed #2. Cast nulls to empty strings in `toVector()` 135 | 136 | ## [0.2.0](https://github.com/pmatseykanets/laravel-scout-postgres/releases/tag/v0.2.0) - 2016-10-07 137 | 138 | ### Added 139 | 140 | - Implemented `getTotalCount()` method to support length aware pagination 141 | 142 | ### Changed 143 | 144 | - Updated README.md 145 | 146 | ## [0.1.1](https://github.com/pmatseykanets/laravel-scout-postgres/releases/tag/v0.1.1) - 2016-10-07 147 | 148 | ### Changed 149 | 150 | - Updated composer dependencies 151 | 152 | ## [0.1.0](https://github.com/pmatseykanets/laravel-scout-postgres/releases/tag/v0.1.0) - 2016-09-02 153 | 154 | Experimental release 155 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | Contributions are **welcome** and will be fully **credited**. 4 | 5 | Please read and understand the contribution guide before creating an issue or pull request. 6 | 7 | ## Etiquette 8 | 9 | This project is open source, and as such, the maintainers give their free time to build and maintain the source code 10 | held within. They make the code freely available in the hope that it will be of use to other developers. It would be 11 | extremely unfair for them to suffer abuse or anger for their hard work. 12 | 13 | Please be considerate towards maintainers when raising issues or presenting pull requests. Let's show the 14 | world that developers are civilized and selfless people. 15 | 16 | It's the duty of the maintainer to ensure that all submissions to the project are of sufficient 17 | quality to benefit the project. Many developers have different skillsets, strengths, and weaknesses. Respect the maintainer's decision, and do not be upset or abusive if your submission is not used. 18 | 19 | ## Viability 20 | 21 | When requesting or submitting new features, first consider whether it might be useful to others. Open 22 | source projects are used by many developers, who may have entirely different needs to your own. Think about 23 | whether or not your feature is likely to be used by other users of the project. 24 | 25 | ## Procedure 26 | 27 | Before filing an issue: 28 | 29 | - Attempt to replicate the problem, to ensure that it wasn't a coincidental incident. 30 | - Check to make sure your feature suggestion isn't already present within the project. 31 | - Check the pull requests tab to ensure that the bug doesn't have a fix in progress. 32 | - Check the pull requests tab to ensure that the feature isn't already in progress. 33 | 34 | Before submitting a pull request: 35 | 36 | - Check the codebase to ensure that your feature doesn't already exist. 37 | - Check the pull requests to ensure that another person hasn't already submitted the feature or fix. 38 | 39 | ## Requirements 40 | 41 | - **[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). 42 | 43 | - **Add tests!** - Your patch won't be accepted if it doesn't have tests. 44 | 45 | - **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date. 46 | 47 | - **Consider our release cycle** - We try to follow [SemVer v2.0.0](http://semver.org/). Randomly breaking public APIs is not an option. 48 | 49 | - **One pull request per feature** - If you want to do more than one thing, send multiple pull requests. 50 | 51 | - **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. 52 | 53 | **Happy coding**! 54 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # The MIT License (MIT) 2 | 3 | Copyright (c) Peter Matseykanets 4 | 5 | > Permission is hereby granted, free of charge, to any person obtaining a copy 6 | > of this software and associated documentation files (the "Software"), to deal 7 | > in the Software without restriction, including without limitation the rights 8 | > to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | > copies of the Software, and to permit persons to whom the Software is 10 | > furnished to do so, subject to the following conditions: 11 | > 12 | > The above copyright notice and this permission notice shall be included in 13 | > all copies or substantial portions of the Software. 14 | > 15 | > THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | > IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | > FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | > AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | > LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | > OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | > THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PostgreSQL Full Text Search Engine for Laravel Scout 2 | 3 | [![Latest Version on Packagist](https://img.shields.io/packagist/v/pmatseykanets/laravel-scout-postgres.svg?style=flat-square)](https://packagist.org/packages/pmatseykanets/laravel-scout-postgres) 4 | [![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md) 5 | ![tests](https://github.com/pmatseykanets/laravel-scout-postgres/workflows/tests/badge.svg) 6 | [![StyleCI](https://styleci.io/repos/67233265/shield)](https://styleci.io/repos/67233265) 7 | [![Total Downloads](https://img.shields.io/packagist/dt/pmatseykanets/laravel-scout-postgres.svg?style=flat-square)](https://packagist.org/packages/pmatseykanets/laravel-scout-postgres) 8 | [![License](https://poser.pugx.org/pmatseykanets/laravel-scout-postgres/license)](https://github.com/pmatseykanets/laravel-scout-postgres/blob/master/LICENSE.md) 9 | 10 | This package makes it easy to use native PostgreSQL Full Text Search capabilities with [Laravel Scout](http://laravel.com/docs/master/scout). 11 | 12 | If you find this package usefull, please consider bying me a coffee. 13 | 14 | Buy Me a Coffee at ko-fi.com 15 | 16 | ## Contents 17 | 18 | - [Installation](#installation) 19 | - [Laravel](#laravel) 20 | - [Lumen](#lumen) 21 | - [Configuration](#configuration) 22 | - [Configuring the Engine](#configuring-the-engine) 23 | - [Configuring PostgreSQL](#configuring-postgresql) 24 | - [Prepare the Schema](#prepare-the-schema) 25 | - [Configuring Searchable Data](#configuring-searchable-data) 26 | - [Configuring the Model](#configuring-the-model) 27 | - [Usage](#usage) 28 | - [Testing](#testing) 29 | - [Security](#security) 30 | - [Changelog](#changelog) 31 | - [Contributing](#contributing) 32 | - [Credits](#credits) 33 | - [License](#license) 34 | 35 | ## Installation 36 | 37 | You can install the package via composer: 38 | 39 | ``` bash 40 | composer require pmatseykanets/laravel-scout-postgres 41 | ``` 42 | 43 | ### Laravel 44 | 45 | If you're using Laravel < 5.5 or if you have package auto-discovery turned off you have to manually register the service provider: 46 | 47 | ```php 48 | // config/app.php 49 | 'providers' => [ 50 | ... 51 | ScoutEngines\Postgres\PostgresEngineServiceProvider::class, 52 | ], 53 | ``` 54 | 55 | ### Lumen 56 | 57 | Scout service provider uses `config_path` helper that is not included in Lumen. 58 | To fix this include the following snippet either directly in `bootstrap.app` or in your autoloaded helpers file i.e. `app/helpers.php`. 59 | 60 | ```php 61 | if (! function_exists('config_path')) { 62 | /** 63 | * Get the configuration path. 64 | * 65 | * @param string $path 66 | * @return string 67 | */ 68 | function config_path($path = '') 69 | { 70 | return app()->basePath() . '/config'.($path ? DIRECTORY_SEPARATOR.$path : $path); 71 | } 72 | } 73 | ``` 74 | 75 | Create the `scout.php` config file in `app/config` folder with the following contents 76 | 77 | ```php 78 | env('SCOUT_DRIVER', 'pgsql'), 82 | 'prefix' => env('SCOUT_PREFIX', ''), 83 | 'queue' => false, 84 | 'pgsql' => [ 85 | 'connection' => 'pgsql', 86 | 'maintain_index' => true, 87 | 'config' => 'english', 88 | ], 89 | ]; 90 | ``` 91 | 92 | Register service providers: 93 | 94 | ```php 95 | // bootstrap/app.php 96 | $app->register(Laravel\Scout\ScoutServiceProvider::class); 97 | $app->configure('scout'); 98 | $app->register(ScoutEngines\Postgres\PostgresEngineServiceProvider::class); 99 | ``` 100 | 101 | ## Configuration 102 | 103 | ### Configuring the Engine 104 | 105 | Specify the database connection that should be used to access indexed documents in the Laravel Scout configuration file `config/scout.php`: 106 | 107 | ```php 108 | // config/scout.php 109 | ... 110 | 'pgsql' => [ 111 | // Connection to use. See config/database.php 112 | 'connection' => env('DB_CONNECTION', 'pgsql'), 113 | // You may want to update index documents directly in PostgreSQL (i.e. via triggers). 114 | // In this case you can set this value to false. 115 | 'maintain_index' => true, 116 | // You can explicitly specify what PostgreSQL text search config to use by scout. 117 | // Use \dF in psql to see all available configurations in your database. 118 | 'config' => 'english', 119 | // You may set the default querying method 120 | // Possible values: plainquery, phrasequery, tsquery 121 | // plainquery is used if this option is omitted. 122 | 'search_using' => 'tsquery' 123 | ], 124 | ... 125 | ``` 126 | 127 | ### Configuring PostgreSQL 128 | 129 | Make sure that an appropriate [default text search configuration](https://www.postgresql.org/docs/9.5/static/runtime-config-client.html#GUC-DEFAULT-TEXT-SEARCH-CONFIG) is set globbaly (in `postgresql.conf`), for a particular database (`ALTER DATABASE ... SET default_text_search_config TO ...`) or alternatively set `default_text_search_config` in each session. 130 | 131 | To check the current value 132 | 133 | ```sql 134 | SHOW default_text_search_config; 135 | ``` 136 | 137 | ### Prepare the Schema 138 | 139 | By default the engine expects that parsed documents (model data) are stored in the same table as the Model in a column `searchable` of type `tsvector`. You'd need to create this column and an index in your schema. You can choose between `GIN` and `GiST` indexes in PostgreSQL. 140 | 141 | ```php 142 | class CreatePostsTable extends Migration 143 | { 144 | public function up() 145 | { 146 | Schema::create('posts', function (Blueprint $table) { 147 | $table->increments('id'); 148 | $table->text('title'); 149 | $table->text('content')->nullable(); 150 | $table->integer('user_id'); 151 | $table->timestamps(); 152 | }); 153 | 154 | DB::statement('ALTER TABLE posts ADD searchable tsvector NULL'); 155 | DB::statement('CREATE INDEX posts_searchable_index ON posts USING GIN (searchable)'); 156 | // Or alternatively 157 | // DB::statement('CREATE INDEX posts_searchable_index ON posts USING GIST (searchable)'); 158 | } 159 | 160 | public function down() 161 | { 162 | Schema::drop('posts'); 163 | } 164 | } 165 | ``` 166 | 167 | ### Configuring Searchable Data 168 | 169 | In addition to Model's attributes you can bring other any other data to the index document. I.e. a list of Tags for a Post. 170 | 171 | ```php 172 | public function toSearchableArray() 173 | { 174 | return [ 175 | 'title' => $this->title, 176 | 'content' => $this->content, 177 | 'author' => $this->user->name, 178 | 'tags' => $this->tags->pluck('tag')->implode(' '), 179 | ]; 180 | } 181 | ``` 182 | 183 | ### Configuring the Model 184 | 185 | You may fine tune the engine behavior for a particular Model by implemeting `searchableOptions()` in your Model. 186 | 187 | ```php 188 | class Post extends Model 189 | { 190 | use Searchable; 191 | 192 | // ... 193 | public function searchableOptions() 194 | { 195 | return [ 196 | // You may wish to change the default name of the column 197 | // that holds parsed documents 198 | 'column' => 'indexable', 199 | // You may want to store the index outside of the Model table 200 | // In that case let the engine know by setting this parameter to true. 201 | 'external' => true, 202 | // If you don't want scout to maintain the index for you 203 | // You can turn it off either for a Model or globally 204 | 'maintain_index' => true, 205 | // Ranking groups that will be assigned to fields 206 | // when document is being parsed. 207 | // Available groups: A, B, C and D. 208 | 'rank' => [ 209 | 'fields' => [ 210 | 'title' => 'A', 211 | 'content' => 'B', 212 | 'author' => 'D', 213 | 'tags' => 'C', 214 | ], 215 | // Ranking weights for searches. 216 | // [D-weight, C-weight, B-weight, A-weight]. 217 | // Default [0.1, 0.2, 0.4, 1.0]. 218 | 'weights' => [0.1, 0.2, 0.4, 1.0], 219 | // Ranking function [ts_rank | ts_rank_cd]. Default ts_rank. 220 | 'function' => 'ts_rank_cd', 221 | // Normalization index. Default 0. 222 | 'normalization' => 32, 223 | ], 224 | // You can explicitly specify a PostgreSQL text search configuration for the model. 225 | // Use \dF in psql to see all available configurationsin your database. 226 | 'config' => 'simple', 227 | ]; 228 | } 229 | } 230 | ... 231 | ``` 232 | 233 | If you decide to keep your Model's index outside of the Model's table you can let engine know that you want to push additional fields in the index table that you can then use to filter the result set by applying `where()` with the Scout `Builder`. In this case you'd need to implement `searchableAdditionalArray()` on your Model. Of course the schema for the external table should include these additional columns. 234 | 235 | ```php 236 | public function searchableAdditionalArray() 237 | { 238 | return [ 239 | 'user_id' => $this->user_id, 240 | ]; 241 | } 242 | ``` 243 | 244 | You may want to make your searchable column hidden so it's not standing in your way 245 | 246 | ```php 247 | protected $hidden = [ 248 | 'searchable', 249 | ]; 250 | ``` 251 | 252 | ## Usage 253 | 254 | ```php 255 | // plainto_tsquery() 256 | $posts = App\Post::search('cat rat') 257 | ->usingPlainQuery()->get() 258 | 259 | // phraseto_tsquery() 260 | $posts = App\Post::search('cat rat') 261 | ->usingPhraseQuery()->get() 262 | 263 | // to_tsquery() 264 | $posts = App\Post::search('fat & (cat | rat)') 265 | ->usingTsQuery()->get() 266 | 267 | // websearch_to_tsquery() 268 | // uses web search syntax 269 | $posts = App\Post::search('"sad cat" or "fat rat" -mouse') 270 | ->usingWebSearchQuery()->get() 271 | 272 | // DIY using a callback 273 | use ScoutEngines\Postgres\TsQuery\ToTsQuery; 274 | 275 | $results = App\Post::search('fat & (cat | rat)', function ($builder, $config) { 276 | return new ToTsQuery($builder->query, $config); 277 | })->get(); 278 | ``` 279 | 280 | Please see the [official documentation](http://laravel.com/docs/master/scout) on how to use Laravel Scout. 281 | 282 | ## Testing 283 | 284 | ``` bash 285 | composer test 286 | ``` 287 | 288 | ## Security 289 | 290 | If you discover any security related issues, please email pmatseykanets@gmail.com instead of using the issue tracker. 291 | 292 | ## Changelog 293 | 294 | Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently. 295 | 296 | ## Contributing 297 | 298 | Please see [CONTRIBUTING](CONTRIBUTING.md) for details. 299 | 300 | ## Credits 301 | 302 | - [Peter Matseykanets](https://github.com/pmatseykanets) 303 | - [All Contributors](../../contributors) 304 | 305 | ## License 306 | 307 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 308 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "pmatseykanets/laravel-scout-postgres", 3 | "description": "PostgreSQL Full Text Search Driver for Laravel Scout", 4 | "keywords": [ 5 | "laravel", 6 | "laravel scout", 7 | "search", 8 | "postgresql", 9 | "full text search", 10 | "FTS" 11 | ], 12 | "homepage": "https://github.com/pmatseykanets/laravel-scout-postgres", 13 | "license": "MIT", 14 | "support": { 15 | "issues": "https://github.com/pmatseykanets/laravel-scout-postgres/issues", 16 | "source": "https://github.com/pmatseykanets/laravel-scout-postgres" 17 | }, 18 | "authors": [ 19 | { 20 | "name": "Peter Matseykanets", 21 | "email": "pmatseykanets@gmail.com", 22 | "homepage": "https://github.com/pmatseykanets" 23 | } 24 | ], 25 | "require": { 26 | "php": "^7.2|^8.0", 27 | "illuminate/contracts": "~6.0|~7.0|~8.0", 28 | "illuminate/database": "~6.0|~7.0|~8.0", 29 | "illuminate/support": "~6.0|~7.0|~8.0", 30 | "laravel/scout": "~7.0|~8.0" 31 | }, 32 | "require-dev": { 33 | "phpunit/phpunit": "^8.3", 34 | "mockery/mockery": "^1.2.3" 35 | }, 36 | "autoload": { 37 | "psr-4": { 38 | "ScoutEngines\\Postgres\\": "src" 39 | } 40 | }, 41 | "autoload-dev": { 42 | "psr-4": { 43 | "ScoutEngines\\Postgres\\Test\\": "tests" 44 | } 45 | }, 46 | "scripts": { 47 | "test": "vendor/bin/phpunit" 48 | }, 49 | "extra": { 50 | "laravel": { 51 | "providers": [ 52 | "ScoutEngines\\Postgres\\PostgresEngineServiceProvider" 53 | ] 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/PostgresEngine.php: -------------------------------------------------------------------------------- 1 | resolver = $resolver; 52 | $this->config = $config; 53 | 54 | $this->connect(); 55 | } 56 | 57 | /** 58 | * Update the given models in the index. 59 | * 60 | * @param \Illuminate\Database\Eloquent\Collection $models 61 | * @return void 62 | */ 63 | public function update($models) 64 | { 65 | if (! $this->shouldMaintainIndex($models->first())) { 66 | return; 67 | } 68 | 69 | foreach ($models as $model) { 70 | $this->performUpdate($model); 71 | } 72 | } 73 | 74 | /** 75 | * Perform update of the given model. 76 | * 77 | * @param \Illuminate\Database\Eloquent\Model $model 78 | * @return bool 79 | */ 80 | protected function performUpdate(Model $model) 81 | { 82 | $data = collect([$this->getIndexColumn($model) => $this->toVector($model)]); 83 | 84 | $query = $this->database 85 | ->table($model->searchableAs()) 86 | ->where($model->getKeyName(), '=', $model->getKey()); 87 | 88 | if (method_exists($model, 'searchableAdditionalArray')) { 89 | $data = $data->merge($model->searchableAdditionalArray() ?: []); 90 | } 91 | 92 | if (! $this->isExternalIndex($model) || $query->exists()) { 93 | return $query->update($data->all()); 94 | } 95 | 96 | return $query->insert( 97 | $data->merge([ 98 | $model->getKeyName() => $model->getKey(), 99 | ])->all() 100 | ); 101 | } 102 | 103 | /** 104 | * Get the indexed value for a given model. 105 | * 106 | * @param \Illuminate\Database\Eloquent\Model $model 107 | * @return string 108 | */ 109 | protected function toVector(Model $model) 110 | { 111 | $fields = collect($model->toSearchableArray()) 112 | ->map(function ($value) { 113 | return $value === null ? '' : $value; 114 | }); 115 | 116 | $bindings = collect([]); 117 | 118 | // The choices of parser, dictionaries and which types of tokens to index are determined 119 | // by the selected text search configuration which can be set globally in config/scout.php 120 | // file or individually for each model in searchableOptions() 121 | // See https://www.postgresql.org/docs/current/static/textsearch-controls.html 122 | $vector = 'to_tsvector(COALESCE(?, get_current_ts_config()), ?)'; 123 | 124 | $select = $fields->map(function ($value, $key) use ($model, $vector, $bindings) { 125 | $bindings->push($this->searchConfig($model) ?: null) 126 | ->push($value); 127 | 128 | // Set a field weight if it was specified in Model's searchableOptions() 129 | if ($label = $this->rankFieldWeightLabel($model, $key)) { 130 | $vector = "setweight($vector, ?)"; 131 | $bindings->push($label); 132 | } 133 | 134 | return $vector; 135 | })->implode(' || '); 136 | 137 | return $this->database 138 | ->query() 139 | ->selectRaw("$select AS tsvector", $bindings->all()) 140 | ->value('tsvector'); 141 | } 142 | 143 | /** 144 | * Remove the given model from the index. 145 | * 146 | * @param \Illuminate\Database\Eloquent\Collection $models 147 | * @return void 148 | */ 149 | public function delete($models) 150 | { 151 | $model = $models->first(); 152 | 153 | if (! $this->shouldMaintainIndex($model)) { 154 | return; 155 | } 156 | 157 | $indexColumn = $this->getIndexColumn($model); 158 | $key = $model->getKeyName(); 159 | 160 | $ids = $models->pluck($key)->all(); 161 | 162 | $this->database 163 | ->table($model->searchableAs()) 164 | ->whereIn($key, $ids) 165 | ->update([$indexColumn => null]); 166 | } 167 | 168 | /** 169 | * Perform the given search on the engine. 170 | * 171 | * @param \Laravel\Scout\Builder $builder 172 | * @return mixed 173 | */ 174 | public function search(Builder $builder) 175 | { 176 | return $this->performSearch($builder, $builder->limit); 177 | } 178 | 179 | /** 180 | * Perform the given search on the engine. 181 | * 182 | * @param \Laravel\Scout\Builder $builder 183 | * @param int $perPage 184 | * @param int $page 185 | * @return mixed 186 | */ 187 | public function paginate(Builder $builder, $perPage, $page) 188 | { 189 | return $this->performSearch($builder, $perPage, $page); 190 | } 191 | 192 | /** 193 | * Get the total count from a raw result returned by the engine. 194 | * 195 | * @param mixed $results 196 | * @return int 197 | */ 198 | public function getTotalCount($results) 199 | { 200 | if (empty($results)) { 201 | return 0; 202 | } 203 | 204 | return (int) Arr::first($results) 205 | ->total_count; 206 | } 207 | 208 | /** 209 | * Perform the given search on the engine. 210 | * 211 | * @param \Laravel\Scout\Builder $builder 212 | * @param int|null $perPage 213 | * @param int $page 214 | * @return array 215 | */ 216 | protected function performSearch(Builder $builder, $perPage = 0, $page = 1) 217 | { 218 | // We have to preserve the model in order to allow for 219 | // correct behavior of mapIds() method which currently 220 | // does not receive a model instance 221 | $this->preserveModel($builder->model); 222 | 223 | $indexColumn = $this->getIndexColumn($builder->model); 224 | 225 | // Build the SQL query 226 | $query = $this->database 227 | ->table($builder->index ?: $builder->model->searchableAs()) 228 | ->select($builder->model->getKeyName()) 229 | ->selectRaw("{$this->rankingExpression($builder->model, $indexColumn)} AS rank") 230 | ->selectRaw('COUNT(*) OVER () AS total_count') 231 | ->whereRaw("$indexColumn @@ \"tsquery\""); 232 | 233 | // Apply where clauses that were set on the builder instance if any 234 | foreach ($builder->wheres as $key => $value) { 235 | $query->where($key, $value); 236 | } 237 | 238 | // If parsed documents are being stored in the model's table 239 | if (! $this->isExternalIndex($builder->model)) { 240 | // and the model uses soft deletes we need to exclude trashed rows 241 | if ($this->usesSoftDeletes($builder->model)) { 242 | $query->whereNull($builder->model->getDeletedAtColumn()); 243 | } 244 | } 245 | 246 | // Apply order by clauses that were set on the builder instance if any 247 | foreach ($builder->orders as $order) { 248 | $query->orderBy($order['column'], $order['direction']); 249 | } 250 | 251 | // Apply default order by clauses (rank and id) 252 | if (empty($builder->orders)) { 253 | $query->orderBy('rank', 'desc') 254 | ->orderBy($builder->model->getKeyName()); 255 | } 256 | 257 | if ($perPage > 0) { 258 | $query->skip(($page - 1) * $perPage) 259 | ->limit($perPage); 260 | } 261 | 262 | // The choices of parser, dictionaries and which types of tokens to index are determined 263 | // by the selected text search configuration which can be set globally in config/scout.php 264 | // file or individually for each model in searchableOptions() 265 | // See https://www.postgresql.org/docs/current/static/textsearch-controls.html 266 | $tsQuery = $builder->callback 267 | ? call_user_func($builder->callback, $builder, $this->searchConfig($builder->model), $query) 268 | : $this->defaultQueryMethod($builder->query, $this->searchConfig($builder->model)); 269 | 270 | $query->crossJoin($this->database->raw($tsQuery->sql().' AS "tsquery"')); 271 | // Add TS bindings to the query 272 | $query->addBinding($tsQuery->bindings(), 'join'); 273 | 274 | return $this->database 275 | ->select($query->toSql(), $query->getBindings()); 276 | } 277 | 278 | /** 279 | * Returns the default query method. 280 | * 281 | * @param string $query 282 | * @param string $config 283 | * @return \ScoutEngines\Postgres\TsQuery\TsQueryable 284 | */ 285 | public function defaultQueryMethod($query, $config) 286 | { 287 | switch (strtolower($this->config('search_using', 'plain'))) { 288 | case 'tsquery': 289 | return new ToTsQuery($query, $config); 290 | case 'phrasequery': 291 | return new PhraseToTsQuery($query, $config); 292 | case 'plainquery': 293 | default: 294 | return new PlainToTsQuery($query, $config); 295 | } 296 | } 297 | 298 | /** 299 | * Pluck and return the primary keys of the given results. 300 | * 301 | * @param mixed $results 302 | * @return \Illuminate\Support\Collection 303 | */ 304 | public function mapIds($results) 305 | { 306 | $keyName = $this->model ? $this->model->getKeyName() : 'id'; 307 | 308 | return collect($results) 309 | ->pluck($keyName) 310 | ->values(); 311 | } 312 | 313 | /** 314 | * Map the given results to instances of the given model. 315 | * 316 | * @param \Laravel\Scout\Builder $builder 317 | * @param mixed $results 318 | * @param \Illuminate\Database\Eloquent\Model $model 319 | * @return \Illuminate\Database\Eloquent\Collection 320 | */ 321 | public function map(Builder $builder, $results, $model) 322 | { 323 | if (empty($results)) { 324 | return Collection::make(); 325 | } 326 | 327 | $keys = $this->mapIds($results); 328 | 329 | $results = collect($results); 330 | 331 | $models = $model->whereIn($model->getKeyName(), $keys->all()) 332 | ->get() 333 | ->keyBy($model->getKeyName()); 334 | 335 | return $results->pluck($model->getKeyName()) 336 | ->intersect($models->keys()) // Filter out no longer existing models (i.e. soft deleted) 337 | ->map(function ($key) use ($models) { 338 | return $models[$key]; 339 | }); 340 | } 341 | 342 | /** 343 | * Connect to the database. 344 | */ 345 | protected function connect() 346 | { 347 | // Already connected 348 | if ($this->database !== null) { 349 | return; 350 | } 351 | 352 | $connection = $this->resolver 353 | ->connection($this->config('connection')); 354 | 355 | if ($connection->getDriverName() !== 'pgsql') { 356 | throw new \InvalidArgumentException('Connection should use pgsql driver.'); 357 | } 358 | 359 | $this->database = $connection; 360 | } 361 | 362 | /** 363 | * Build ranking expression that will be used in a search. 364 | * ts_rank([ weights, ] vector, query [, normalization ]) 365 | * ts_rank_cd([ weights, ] vector, query [, normalization ]). 366 | * 367 | * @param \Illuminate\Database\Eloquent\Model $model 368 | * @param string $indexColumn 369 | * @return string 370 | */ 371 | protected function rankingExpression(Model $model, $indexColumn) 372 | { 373 | $args = collect([$indexColumn, '"tsquery"']); 374 | 375 | if ($weights = $this->rankWeights($model)) { 376 | $args->prepend("'$weights'"); 377 | } 378 | 379 | if ($norm = $this->rankNormalization($model)) { 380 | $args->push($norm); 381 | } 382 | 383 | $fn = $this->rankFunction($model); 384 | 385 | return "$fn({$args->implode(',')})"; 386 | } 387 | 388 | /** 389 | * Get rank function. 390 | * 391 | * @param \Illuminate\Database\Eloquent\Model $model 392 | * @return int 393 | */ 394 | protected function rankFunction(Model $model) 395 | { 396 | $default = 'ts_rank'; 397 | 398 | $function = $this->option($model, 'rank.function', $default); 399 | 400 | return collect(['ts_rank', 'ts_rank_cd'])->contains($function) ? $function : $default; 401 | } 402 | 403 | /** 404 | * Get the rank weight label for a given field. 405 | * 406 | * @param \Illuminate\Database\Eloquent\Model $model 407 | * @param string $field 408 | * @return string 409 | */ 410 | protected function rankFieldWeightLabel(Model $model, $field) 411 | { 412 | $label = $this->option($model, "rank.fields.$field"); 413 | 414 | return collect(['A', 'B', 'C', 'D']) 415 | ->contains($label) ? $label : ''; 416 | } 417 | 418 | /** 419 | * Get rank weights. 420 | * 421 | * @param \Illuminate\Database\Eloquent\Model $model 422 | * @return string 423 | */ 424 | protected function rankWeights(Model $model) 425 | { 426 | $weights = $this->option($model, 'rank.weights'); 427 | 428 | if (! is_array($weights) || count($weights) !== 4) { 429 | return ''; 430 | } 431 | 432 | return '{'.implode(',', $weights).'}'; 433 | } 434 | 435 | /** 436 | * Get rank normalization. 437 | * 438 | * @param \Illuminate\Database\Eloquent\Model $model 439 | * @return int 440 | */ 441 | protected function rankNormalization(Model $model) 442 | { 443 | return $this->option($model, 'rank.normalization', 0); 444 | } 445 | 446 | /** 447 | * See if the index should be maintained for a given model. 448 | * 449 | * @param \Illuminate\Database\Eloquent\Model|null $model 450 | * @return bool 451 | */ 452 | protected function shouldMaintainIndex(Model $model = null) 453 | { 454 | if ((bool) $this->config('maintain_index', true) === false) { 455 | return false; 456 | } 457 | 458 | if ($model !== null) { 459 | return $this->option($model, 'maintain_index', true); 460 | } 461 | } 462 | 463 | /** 464 | * Get the name of the column that holds indexed documents. 465 | * 466 | * @param \Illuminate\Database\Eloquent\Model $model 467 | * @return string 468 | */ 469 | protected function getIndexColumn(Model $model) 470 | { 471 | return $this->option($model, 'column', 'searchable'); 472 | } 473 | 474 | /** 475 | * See if indexed documents are stored in a external table. 476 | * 477 | * @param \Illuminate\Database\Eloquent\Model $model 478 | * @return mixed 479 | */ 480 | protected function isExternalIndex(Model $model) 481 | { 482 | return $this->option($model, 'external', false); 483 | } 484 | 485 | /** 486 | * Get the model specific option value or a default. 487 | * 488 | * @param \Illuminate\Database\Eloquent\Model $model 489 | * @param string $key 490 | * @param mixed $default 491 | * @return mixed 492 | */ 493 | protected function option(Model $model, $key, $default = null) 494 | { 495 | if (! method_exists($model, 'searchableOptions')) { 496 | return $default; 497 | } 498 | 499 | $options = $model->searchableOptions() ?: []; 500 | 501 | return Arr::get($options, $key, $default); 502 | } 503 | 504 | /** 505 | * Get the config value or a default. 506 | * 507 | * @param string $key 508 | * @param mixed $default 509 | * @return mixed 510 | */ 511 | protected function config($key, $default = null) 512 | { 513 | return Arr::get($this->config, $key, $default); 514 | } 515 | 516 | /** 517 | * @param \Illuminate\Database\Eloquent\Model $model 518 | */ 519 | protected function preserveModel(Model $model) 520 | { 521 | $this->model = $model; 522 | } 523 | 524 | /** 525 | * Returns a search config name for a model. 526 | * 527 | * @param \Illuminate\Database\Eloquent\Model $model 528 | * @return string 529 | */ 530 | protected function searchConfig(Model $model) 531 | { 532 | return $this->option($model, 'config', $this->config('config', '')) ?: null; 533 | } 534 | 535 | /** 536 | * Checks if the model uses the SoftDeletes trait. 537 | * 538 | * @param \Illuminate\Database\Eloquent\Model $model 539 | * @return bool 540 | */ 541 | protected function usesSoftDeletes(Model $model) 542 | { 543 | return method_exists($model, 'getDeletedAtColumn'); 544 | } 545 | 546 | /** 547 | * Flush all of the model's records from the engine. 548 | * 549 | * @param \Illuminate\Database\Eloquent\Model $model 550 | * @return void 551 | */ 552 | public function flush($model) 553 | { 554 | if (! $this->shouldMaintainIndex($model)) { 555 | return; 556 | } 557 | 558 | $indexColumn = $this->getIndexColumn($model); 559 | 560 | $this->database 561 | ->table($model->searchableAs()) 562 | ->update([$indexColumn => null]); 563 | } 564 | } 565 | -------------------------------------------------------------------------------- /src/PostgresEngineServiceProvider.php: -------------------------------------------------------------------------------- 1 | PhraseToTsQuery::class, 19 | 'usingPlainQuery' => PlainToTsQuery::class, 20 | 'usingTsQuery' => ToTsQuery::class, 21 | 'usingWebSearchQuery' => WebSearchToTsQuery::class, 22 | ]; 23 | } 24 | 25 | public function boot() 26 | { 27 | $this->app->make(EngineManager::class)->extend('pgsql', function () { 28 | return new PostgresEngine( 29 | $this->app->get('db'), 30 | $this->app->get('config')->get('scout.pgsql', []) 31 | ); 32 | }); 33 | 34 | foreach (self::builderMacros() as $macro => $class) { 35 | $this->registerBuilderMacro($macro, $class); 36 | } 37 | } 38 | 39 | protected function registerBuilderMacro($name, $class) 40 | { 41 | if (! Builder::hasMacro($name)) { 42 | Builder::macro($name, function () use ($class) { 43 | $this->callback = function ($builder, $config) use ($class) { 44 | return new $class($builder->query, $config); 45 | }; 46 | 47 | return $this; 48 | }); 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/TsQuery/BaseTsQueryable.php: -------------------------------------------------------------------------------- 1 | query = $query; 37 | $this->config = $config; 38 | } 39 | 40 | /** 41 | * Render the SQL representation. 42 | * 43 | * @return string 44 | */ 45 | public function sql() 46 | { 47 | return sprintf('%s(COALESCE(?, get_current_ts_config()), ?)', $this->tsFunction); 48 | } 49 | 50 | /** 51 | * Return value bindings for the SQL representation. 52 | * 53 | * @return array 54 | */ 55 | public function bindings() 56 | { 57 | return [$this->config, $this->query]; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/TsQuery/PhraseToTsQuery.php: -------------------------------------------------------------------------------- 1 |