{"id":29181933,"url":"https://github.com/pmatseykanets/laravel-scout-postgres","last_synced_at":"2025-07-01T20:05:01.803Z","repository":{"id":44882382,"uuid":"67233265","full_name":"pmatseykanets/laravel-scout-postgres","owner":"pmatseykanets","description":"PostgreSQL Full Text Search Engine for Laravel Scout","archived":false,"fork":false,"pushed_at":"2023-04-28T15:32:04.000Z","size":119,"stargazers_count":159,"open_issues_count":18,"forks_count":35,"subscribers_count":5,"default_branch":"master","last_synced_at":"2024-06-18T15:31:32.561Z","etag":null,"topics":["database","full-text-search","laravel","laravel-package","laravel-scout","postgres","postgresql"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pmatseykanets.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-09-02T15:18:51.000Z","updated_at":"2024-06-12T03:33:17.000Z","dependencies_parsed_at":"2024-06-18T15:39:06.264Z","dependency_job_id":null,"html_url":"https://github.com/pmatseykanets/laravel-scout-postgres","commit_stats":{"total_commits":101,"total_committers":9,"mean_commits":"11.222222222222221","dds":"0.29702970297029707","last_synced_commit":"4f998208716fb83ec694430a2fbbe900f5fbcb31"},"previous_names":[],"tags_count":23,"template":false,"template_full_name":null,"purl":"pkg:github/pmatseykanets/laravel-scout-postgres","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pmatseykanets%2Flaravel-scout-postgres","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pmatseykanets%2Flaravel-scout-postgres/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pmatseykanets%2Flaravel-scout-postgres/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pmatseykanets%2Flaravel-scout-postgres/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pmatseykanets","download_url":"https://codeload.github.com/pmatseykanets/laravel-scout-postgres/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pmatseykanets%2Flaravel-scout-postgres/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":263029205,"owners_count":23402354,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["database","full-text-search","laravel","laravel-package","laravel-scout","postgres","postgresql"],"created_at":"2025-07-01T20:02:02.715Z","updated_at":"2025-07-01T20:05:01.785Z","avatar_url":"https://github.com/pmatseykanets.png","language":"PHP","funding_links":["https://ko-fi.com/V7V43MXAO'"],"categories":[],"sub_categories":[],"readme":"# PostgreSQL Full Text Search Engine for Laravel Scout\n\n[![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)\n[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md)\n![tests](https://github.com/pmatseykanets/laravel-scout-postgres/workflows/tests/badge.svg)\n[![StyleCI](https://styleci.io/repos/67233265/shield)](https://styleci.io/repos/67233265)\n[![Total Downloads](https://img.shields.io/packagist/dt/pmatseykanets/laravel-scout-postgres.svg?style=flat-square)](https://packagist.org/packages/pmatseykanets/laravel-scout-postgres)\n[![License](https://poser.pugx.org/pmatseykanets/laravel-scout-postgres/license)](https://github.com/pmatseykanets/laravel-scout-postgres/blob/master/LICENSE.md)\n\nThis package makes it easy to use native PostgreSQL Full Text Search capabilities with [Laravel Scout](http://laravel.com/docs/master/scout).\n\nIf you find this package usefull, please consider bying me a coffee.\n\n\u003ca href='https://ko-fi.com/V7V43MXAO' target='_blank'\u003e\u003cimg height='28' style='border:0px;height:28px;' src='https://cdn.ko-fi.com/cdn/kofi2.png?v=2' border='0' alt='Buy Me a Coffee at ko-fi.com' /\u003e\u003c/a\u003e\n\n## Contents\n\n- [Installation](#installation)\n  - [Laravel](#laravel)\n  - [Lumen](#lumen)\n- [Configuration](#configuration)\n  - [Configuring the Engine](#configuring-the-engine)\n  - [Configuring PostgreSQL](#configuring-postgresql)\n  - [Prepare the Schema](#prepare-the-schema)\n  - [Configuring Searchable Data](#configuring-searchable-data)\n  - [Configuring the Model](#configuring-the-model)\n- [Usage](#usage)\n- [Testing](#testing)\n- [Security](#security)\n- [Changelog](#changelog)\n- [Contributing](#contributing)\n- [Credits](#credits)\n- [License](#license)\n\n## Installation\n\nYou can install the package via composer:\n\n``` bash\ncomposer require pmatseykanets/laravel-scout-postgres\n```\n\n### Laravel\n\nIf you're using Laravel \u003c 5.5 or if you have package auto-discovery turned off you have to manually register the service provider:\n\n```php\n// config/app.php\n'providers' =\u003e [\n    ...\n    ScoutEngines\\Postgres\\PostgresEngineServiceProvider::class,\n],\n```\n\n### Lumen\n\nScout service provider uses `config_path` helper that is not included in Lumen.\nTo fix this include the following snippet either directly in `bootstrap.app` or in your autoloaded helpers file i.e. `app/helpers.php`.\n\n```php\nif (! function_exists('config_path')) {\n    /**\n     * Get the configuration path.\n     *\n     * @param  string  $path\n     * @return string\n     */\n    function config_path($path = '')\n    {\n        return app()-\u003ebasePath() . '/config'.($path ? DIRECTORY_SEPARATOR.$path : $path);\n    }\n}\n```\n\nCreate the `scout.php` config file in `app/config` folder with the following contents\n\n```php\n\u003c?php\n\nreturn [\n    'driver' =\u003e env('SCOUT_DRIVER', 'pgsql'),\n    'prefix' =\u003e env('SCOUT_PREFIX', ''),\n    'queue' =\u003e false,\n    'pgsql' =\u003e [\n        'connection' =\u003e 'pgsql',\n        'maintain_index' =\u003e true,\n        'config' =\u003e 'english',\n    ],\n];\n```\n\nRegister service providers:\n\n```php\n// bootstrap/app.php\n$app-\u003eregister(Laravel\\Scout\\ScoutServiceProvider::class);\n$app-\u003econfigure('scout');\n$app-\u003eregister(ScoutEngines\\Postgres\\PostgresEngineServiceProvider::class);\n```\n\n## Configuration\n\n### Configuring the Engine\n\nSpecify the database connection that should be used to access indexed documents in the Laravel Scout configuration file `config/scout.php`:\n\n```php\n// config/scout.php\n...\n'pgsql' =\u003e [\n    // Connection to use. See config/database.php\n    'connection' =\u003e env('DB_CONNECTION', 'pgsql'),\n    // You may want to update index documents directly in PostgreSQL (i.e. via triggers).\n    // In this case you can set this value to false.\n    'maintain_index' =\u003e true,\n    // You can explicitly specify what PostgreSQL text search config to use by scout.\n    // Use \\dF in psql to see all available configurations in your database.\n    'config' =\u003e 'english',\n    // You may set the default querying method\n    // Possible values: plainquery, phrasequery, tsquery\n    // plainquery is used if this option is omitted.\n    'search_using' =\u003e 'tsquery'\n],\n...\n```\n\n### Configuring PostgreSQL\n\nMake 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.\n\nTo check the current value\n\n```sql\nSHOW default_text_search_config;\n```\n\n### Prepare the Schema\n\nBy 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.\n\n```php\nclass CreatePostsTable extends Migration\n{\n    public function up()\n    {\n        Schema::create('posts', function (Blueprint $table) {\n            $table-\u003eincrements('id');\n            $table-\u003etext('title');\n            $table-\u003etext('content')-\u003enullable();\n            $table-\u003einteger('user_id');\n            $table-\u003etimestamps();\n        });\n\n        DB::statement('ALTER TABLE posts ADD searchable tsvector NULL');\n        DB::statement('CREATE INDEX posts_searchable_index ON posts USING GIN (searchable)');\n        // Or alternatively\n        // DB::statement('CREATE INDEX posts_searchable_index ON posts USING GIST (searchable)');\n    }\n\n    public function down()\n    {\n        Schema::drop('posts');\n    }\n}\n```\n\n### Configuring Searchable Data\n\nIn 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.\n\n```php\npublic function toSearchableArray()\n{\n    return [\n        'title' =\u003e $this-\u003etitle,\n        'content' =\u003e $this-\u003econtent,\n        'author' =\u003e $this-\u003euser-\u003ename,\n        'tags' =\u003e $this-\u003etags-\u003epluck('tag')-\u003eimplode(' '),\n    ];\n}\n```\n\n### Configuring the Model\n\nYou may fine tune the engine behavior for a particular Model by implemeting `searchableOptions()` in your Model.\n\n```php\nclass Post extends Model\n{\n    use Searchable;\n\n    // ...\n    public function searchableOptions()\n    {\n        return [\n            // You may wish to change the default name of the column\n            // that holds parsed documents\n            'column' =\u003e 'indexable',\n            // You may want to store the index outside of the Model table\n            // In that case let the engine know by setting this parameter to true.\n            'external' =\u003e true,\n            // If you don't want scout to maintain the index for you\n            // You can turn it off either for a Model or globally\n            'maintain_index' =\u003e true,\n            // Ranking groups that will be assigned to fields\n            // when document is being parsed.\n            // Available groups: A, B, C and D.\n            'rank' =\u003e [\n                'fields' =\u003e [\n                    'title' =\u003e 'A',\n                    'content' =\u003e 'B',\n                    'author' =\u003e 'D',\n                    'tags' =\u003e 'C',\n                ],\n                // Ranking weights for searches.\n                // [D-weight, C-weight, B-weight, A-weight].\n                // Default [0.1, 0.2, 0.4, 1.0].\n                'weights' =\u003e [0.1, 0.2, 0.4, 1.0],\n                // Ranking function [ts_rank | ts_rank_cd]. Default ts_rank.\n                'function' =\u003e 'ts_rank_cd',\n                // Normalization index. Default 0.\n                'normalization' =\u003e 32,\n            ],\n            // You can explicitly specify a PostgreSQL text search configuration for the model.\n            // Use \\dF in psql to see all available configurationsin your database.\n            'config' =\u003e 'simple',\n        ];\n    }\n}\n...\n```\n\nIf 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.\n\n```php\npublic function searchableAdditionalArray()\n{\n    return [\n        'user_id' =\u003e $this-\u003euser_id,\n    ];\n}\n```\n\nYou may want to make your searchable column hidden so it's not standing in your way\n\n```php\nprotected $hidden = [\n    'searchable',\n];\n```\n\n## Usage\n\n```php\n// plainto_tsquery()\n$posts = App\\Post::search('cat rat')\n    -\u003eusingPlainQuery()-\u003eget()\n\n// phraseto_tsquery()\n$posts = App\\Post::search('cat rat')\n    -\u003eusingPhraseQuery()-\u003eget()\n\n// to_tsquery()\n$posts = App\\Post::search('fat \u0026 (cat | rat)')\n    -\u003eusingTsQuery()-\u003eget()\n\n// websearch_to_tsquery()\n// uses web search syntax \n$posts = App\\Post::search('\"sad cat\" or \"fat rat\" -mouse')\n    -\u003eusingWebSearchQuery()-\u003eget()\n\n// DIY using a callback\nuse ScoutEngines\\Postgres\\TsQuery\\ToTsQuery;\n\n$results = App\\Post::search('fat \u0026 (cat | rat)', function ($builder, $config) {\n    return new ToTsQuery($builder-\u003equery, $config);\n})-\u003eget();\n```\n\nPlease see the [official documentation](http://laravel.com/docs/master/scout) on how to use Laravel Scout.\n\n## Testing\n\n``` bash\ncomposer test\n```\n\n## Security\n\nIf you discover any security related issues, please email pmatseykanets@gmail.com instead of using the issue tracker.\n\n## Changelog\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.\n\n## Contributing\n\nPlease see [CONTRIBUTING](CONTRIBUTING.md) for details.\n\n## Credits\n\n- [Peter Matseykanets](https://github.com/pmatseykanets)\n- [All Contributors](../../contributors)\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpmatseykanets%2Flaravel-scout-postgres","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpmatseykanets%2Flaravel-scout-postgres","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpmatseykanets%2Flaravel-scout-postgres/lists"}