Skip to content

Commit 7791c4f

Browse files
committed
Support new websearch_tsquery() function
that was introduced in PostgreSQL 11 See https://www.postgresql.org/docs/11/static/textsearch-controls.html#TEXTSEARCH-PARSING-QUERIES websearch_to_tsquery creates a tsquery value from querytext using an alternative syntax in which simple unformatted text is a valid query. Unlike plainto_tsquery and phraseto_tsquery, it also recognizes certain operators. Moreover, this function should never raise syntax errors, which makes it possible to use raw user-supplied input for search. The following syntax is supported: - unquoted text: text not inside quote marks will be converted to terms separated by & operators, as if processed by plainto_tsquery. - "quoted text": text inside quote marks will be converted to terms separated by <-> operators, as if processed by phraseto_tsquery. - OR: logical or will be converted to the | operator. - -: the logical not operator, converted to the the ! operator.
1 parent ee9c2da commit 7791c4f

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/PostgresEngineServiceProvider.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,17 @@
88
use ScoutEngines\Postgres\TsQuery\ToTsQuery;
99
use ScoutEngines\Postgres\TsQuery\PlainToTsQuery;
1010
use ScoutEngines\Postgres\TsQuery\PhraseToTsQuery;
11+
use ScoutEngines\Postgres\TsQuery\WebSearchToTsQuery;
1112

1213
class PostgresEngineServiceProvider extends ServiceProvider
1314
{
1415
public function boot()
1516
{
1617
$this->app->make(EngineManager::class)->extend('pgsql', function () {
17-
return new PostgresEngine($this->app['db'], $this->app['config']->get('scout.pgsql', []));
18+
return new PostgresEngine(
19+
$this->app->get('db'),
20+
$this->app->get('config')->get('scout.pgsql', [])
21+
);
1822
});
1923

2024
if (! Builder::hasMacro('usingPhraseQuery')) {
@@ -46,5 +50,15 @@ public function boot()
4650
return $this;
4751
});
4852
}
53+
54+
if (! Builder::hasMacro('usingWebSearchQuery')) {
55+
Builder::macro('usingWebSearchQuery', function () {
56+
$this->callback = function ($builder, $config) {
57+
return new WebSearchToTsQuery($builder->query, $config);
58+
};
59+
60+
return $this;
61+
});
62+
}
4963
}
5064
}

src/TsQuery/WebSearchToTsQuery.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace ScoutEngines\Postgres\TsQuery;
4+
5+
class WebSearchToTsQuery extends BaseTsQueryable
6+
{
7+
protected $tsFunction = 'websearch_tsquery';
8+
}

0 commit comments

Comments
 (0)