PHPackages                             devnoiseconsulting/laravel-scout-postgres-tsvector - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Search &amp; Filtering](/categories/search)
4. /
5. devnoiseconsulting/laravel-scout-postgres-tsvector

ActiveLibrary[Search &amp; Filtering](/categories/search)

devnoiseconsulting/laravel-scout-postgres-tsvector
==================================================

PostgreSQL Full Text Search Driver for Laravel Scout

v9.2.0(1y ago)58110.1k↓16%9[3 issues](https://github.com/devNoiseConsulting/laravel-scout-postgres-tsvector/issues)[1 PRs](https://github.com/devNoiseConsulting/laravel-scout-postgres-tsvector/pulls)MITPHPPHP ^8.1|^8.2|^8.3|^8.4CI failing

Since Sep 2Pushed 1y ago1 watchersCompare

[ Source](https://github.com/devNoiseConsulting/laravel-scout-postgres-tsvector)[ Packagist](https://packagist.org/packages/devnoiseconsulting/laravel-scout-postgres-tsvector)[ Docs](https://github.com/devNoiseConsulting/laravel-scout-postgres-tsvector)[ RSS](/packages/devnoiseconsulting-laravel-scout-postgres-tsvector/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (3)Dependencies (10)Versions (40)Used By (0)

PostgreSQL Full Text Search Engine for Laravel Scout
====================================================

[](#postgresql-full-text-search-engine-for-laravel-scout)

[![Build Status](https://github.com/devnoiseconsulting/laravel-scout-postgres-tsvector/workflows/tests/badge.svg)](https://github.com/devnoiseconsulting/laravel-scout-postgres-tsvector/workflows/tests/badge.svg)[![Latest Stable Version](https://camo.githubusercontent.com/0aeb51ace5aa0d7648cd3f657701e336cd8ad85b05a12f2413d4891834dabcec/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6465766e6f697365636f6e73756c74696e672f6c61726176656c2d73636f75742d706f7374677265732d7473766563746f722e737667)](https://packagist.org/packages/devnoiseconsulting/laravel-scout-postgres-tsvector)[![Software License](https://camo.githubusercontent.com/074b89bca64d3edc93a1db6c7e3b1636b874540ba91d66367c0e5e354c56d0ea/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e737667)](LICENSE.md)

This package makes it easy to use native PostgreSQL Full Text Search capabilities with [Laravel Scout](http://laravel.com/docs/master/scout).

Contents
--------

[](#contents)

- [Installation](#installation)
    - [Laravel](#laravel)
- [Configuration](#configuration)
    - [Configuring the Engine](#configuring-the-engine)
    - [Configuring PostgreSQL](#configuring-postgresql)
    - [Prepare the Schema](#prepare-the-schema)
    - [Configuring Searchable Data](#configuring-searchable-data)
    - [Configuring the Model](#configuring-the-model)
- [Usage](#usage)
- [Testing](#testing)
- [Security](#security)
- [Changelog](#changelog)
- [Contributing](#contributing)
- [Credits](#credits)
- [License](#license)

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require devnoiseconsulting/laravel-scout-postgres-tsvector
```

### Laravel

[](#laravel)

If you're using Laravel &lt; 5.5 or if you have package auto-discovery turned off you have to manually register the service provider:

```
// config/app.php
'providers' => [
    ...
    ScoutEngines\Postgres\PostgresEngineServiceProvider::class,
],
```

Configuration
-------------

[](#configuration)

### Configuring the Engine

[](#configuring-the-engine)

Specify the database connection that should be used to access indexed documents in the Laravel Scout configuration file `config/scout.php`:

```
// config/scout.php
...
'pgsql' => [
    // Connection to use. See config/database.php
    'connection' => env('DB_CONNECTION', 'pgsql'),
    // You may want to update index documents directly in PostgreSQL (i.e. via triggers).
    // In this case you can set this value to false.
    'maintain_index' => true,
    // You can explicitly specify what PostgreSQL text search config to use by scout.
    // Use \dF in psql to see all available configurations in your database.
    'config' => 'english',
    // You may set the default querying method
    // Possible values: plainquery, phrasequery, tsquery
    // plainquery is used if this option is omitted.
    'search_using' => 'tsquery'
],
...
```

### Configuring PostgreSQL

[](#configuring-postgresql)

Make sure that an appropriate [default text search configuration](https://www.postgresql.org/docs/14/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.

To check the current value

```
SHOW default_text_search_config;
```

### Prepare the Schema

[](#prepare-the-schema)

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.

```
class CreatePostsTable extends Migration
{
    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->increments('id');
            $table->text('title');
            $table->text('content')->nullable();
            $table->integer('user_id');
            $table->timestamps();
        });

        DB::statement('ALTER TABLE posts ADD searchable tsvector NULL');
        DB::statement('CREATE INDEX posts_searchable_index ON posts USING GIN (searchable)');
        // Or alternatively
        // DB::statement('CREATE INDEX posts_searchable_index ON posts USING GIST (searchable)');
    }

    public function down()
    {
        Schema::drop('posts');
    }
}
```

### Configuring Searchable Data

[](#configuring-searchable-data)

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.

```
public function toSearchableArray()
{
    return [
        'title' => $this->title,
        'content' => $this->content,
        'author' => $this->user->name,
        'tags' => $this->tags->pluck('tag')->implode(' '),
    ];
}
```

### Configuring the Model

[](#configuring-the-model)

You may fine tune the engine behavior for a particular Model by implemeting `searchableOptions()` in your Model.

```
class Post extends Model
{
    use Searchable;

    // ...
    public function searchableOptions()
    {
        return [
            // You may wish to change the default name of the column
            // that holds parsed documents
            'column' => 'indexable',
            // You may want to store the index outside of the Model table
            // In that case let the engine know by setting this parameter to true.
            'external' => true,
            // If you don't want scout to maintain the index for you
            // You can turn it off either for a Model or globally
            'maintain_index' => true,
            // Ranking groups that will be assigned to fields
            // when document is being parsed.
            // Available groups: A, B, C and D.
            'rank' => [
                'fields' => [
                    'title' => 'A',
                    'content' => 'B',
                    'author' => 'D',
                    'tags' => 'C',
                ],
                // Ranking weights for searches.
                // [D-weight, C-weight, B-weight, A-weight].
                // Default [0.1, 0.2, 0.4, 1.0].
                'weights' => [0.1, 0.2, 0.4, 1.0],
                // Ranking function [ts_rank | ts_rank_cd]. Default ts_rank.
                'function' => 'ts_rank_cd',
                // Normalization index. Default 0.
                'normalization' => 32,
            ],
            // You can explicitly specify a PostgreSQL text search configuration for the model.
            // Use \dF in psql to see all available configurationsin your database.
            'config' => 'simple',
        ];
    }
}
...
```

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.

```
public function searchableAdditionalArray()
{
    return [
        'user_id' => $this->user_id,
    ];
}
```

You may want to make your searchable column hidden so it's not standing in your way

```
protected $hidden = [
    'searchable',
];
```

Usage
-----

[](#usage)

```
// plainto_tsquery()
$posts = App\Post::search('cat rat')
    ->usingPlainQuery()->get()

// phraseto_tsquery()
$posts = App\Post::search('cat rat')
    ->usingPhraseQuery()->get()

// to_tsquery()
$posts = App\Post::search('fat & (cat | rat)')
    ->usingTsQuery()->get()

// websearch_to_tsquery()
// uses web search syntax
$posts = App\Post::search('"sad cat" or "fat rat" -mouse')
    ->usingWebSearchQuery()->get()

// DIY using a callback
use ScoutEngines\Postgres\TsQuery\ToTsQuery;

$results = App\Post::search('fat & (cat | rat)', function ($builder, $config) {
    return new ToTsQuery($builder->query, $config);
})->get();
```

Please see the [official documentation](http://laravel.com/docs/master/scout) on how to use Laravel Scout.

Testing
-------

[](#testing)

```
composer test
```

Security
--------

[](#security)

If you discover any security related issues, please email  instead of using the issue tracker.

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Credits
-------

[](#credits)

- [Michael Flynn](https://github.com/devNoiseConsulting)
- [Peter Matseykanets](https://github.com/pmatseykanets)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

55

—

FairBetter than 98% of packages

Maintenance42

Moderate activity, may be stable

Popularity45

Moderate usage in the ecosystem

Community18

Small or concentrated contributor base

Maturity93

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 58.1% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~91 days

Recently: every ~146 days

Total

34

Last Release

534d ago

Major Versions

v5.0.0 → v6.0.02019-09-19

v6.0.0 → v7.0.02020-03-09

v7.3.0 → v8.0.02022-07-22

v8.0.1 → v9.0.02023-02-24

v9.1.1 → 10.x-dev2023-04-27

PHP version history (9 changes)v0.1.0PHP &gt;=5.6.4

v2.0.0PHP &gt;=7.1.3

v2.2.0PHP &gt;=7.0

v6.0.0PHP ^7.2

v7.3.0PHP ^7.2|^8.0

v8.0.0PHP ^7.3|^8.0

v9.0.0PHP ^8.0|^8.1|^8.2

10.x-devPHP ^8.1|^8.2

v9.2.0PHP ^8.1|^8.2|^8.3|^8.4

### Community

Maintainers

![](https://www.gravatar.com/avatar/37ead4f34ac0cffef432be16bd0760547d1ed863f8addba4b30926288f179927?d=identicon)[dev\_noise](/maintainers/dev_noise)

---

Top Contributors

[![pmatseykanets](https://avatars.githubusercontent.com/u/779965?v=4)](https://github.com/pmatseykanets "pmatseykanets (86 commits)")[![devNoiseConsulting](https://avatars.githubusercontent.com/u/1700471?v=4)](https://github.com/devNoiseConsulting "devNoiseConsulting (40 commits)")[![gregmartyn](https://avatars.githubusercontent.com/u/974822?v=4)](https://github.com/gregmartyn "gregmartyn (7 commits)")[![tortuetorche](https://avatars.githubusercontent.com/u/5038872?v=4)](https://github.com/tortuetorche "tortuetorche (6 commits)")[![willvincent](https://avatars.githubusercontent.com/u/689891?v=4)](https://github.com/willvincent "willvincent (5 commits)")[![cpgo](https://avatars.githubusercontent.com/u/739891?v=4)](https://github.com/cpgo "cpgo (2 commits)")[![roquie](https://avatars.githubusercontent.com/u/3214290?v=4)](https://github.com/roquie "roquie (1 commits)")[![keithbrink](https://avatars.githubusercontent.com/u/15267205?v=4)](https://github.com/keithbrink "keithbrink (1 commits)")

---

Tags

searchlaravelpostgresqlfull text searchftslaravel scout

###  Code Quality

TestsPHPUnit

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/devnoiseconsulting-laravel-scout-postgres-tsvector/health.svg)

```
[![Health](https://phpackages.com/badges/devnoiseconsulting-laravel-scout-postgres-tsvector/health.svg)](https://phpackages.com/packages/devnoiseconsulting-laravel-scout-postgres-tsvector)
```

###  Alternatives

[pmatseykanets/laravel-scout-postgres

PostgreSQL Full Text Search Driver for Laravel Scout

164213.7k2](/packages/pmatseykanets-laravel-scout-postgres)[algolia/scout-extended

Scout Extended extends Laravel Scout adding algolia-specific features

4186.3M6](/packages/algolia-scout-extended)[mailerlite/laravel-elasticsearch

An easy way to use the official PHP ElasticSearch client in your Laravel applications.

934529.3k2](/packages/mailerlite-laravel-elasticsearch)[teamtnt/laravel-scout-tntsearch-driver

Driver for Laravel Scout search package based on https://github.com/teamtnt/tntsearch

1.1k2.5M28](/packages/teamtnt-laravel-scout-tntsearch-driver)[jeroen-g/explorer

Next-gen Elasticsearch driver for Laravel Scout.

397612.3k](/packages/jeroen-g-explorer)[typesense/laravel-scout-typesense-driver

Laravel Scout Driver for Typesense

144637.2k3](/packages/typesense-laravel-scout-typesense-driver)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
