From 1664ae1978bf0f5ee940dc2fc8313e6400a7e7da Mon Sep 17 00:00:00 2001 From: Teodor Sigaev Date: Thu, 5 Apr 2018 19:55:11 +0300 Subject: Add websearch_to_tsquery Error-tolerant conversion function with web-like syntax for search query, it simplifies constraining search engine with close to habitual interface for users. Bump catalog version Authors: Victor Drobny, Dmitry Ivanov with editorization by me Reviewed by: Aleksander Alekseev, Tomas Vondra, Thomas Munro, Aleksandr Parfenov Discussion: https://www.postgresql.org/message-id/flat/fe931111ff7e9ad79196486ada79e268@postgrespro.ru --- doc/src/sgml/func.sgml | 12 ++++++ doc/src/sgml/textsearch.sgml | 92 ++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 100 insertions(+), 4 deletions(-) (limited to 'doc/src') diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml index 9a1efc14cf7..122f034f177 100644 --- a/doc/src/sgml/func.sgml +++ b/doc/src/sgml/func.sgml @@ -9630,6 +9630,18 @@ CREATE TYPE rainbow AS ENUM ('red', 'orange', 'yellow', 'green', 'blue', 'purple phraseto_tsquery('english', 'The Fat Rats') 'fat' <-> 'rat' + + + + websearch_to_tsquery + + websearch_to_tsquery( config regconfig , query text) + + tsquery + produce tsquery from a web search style query + websearch_to_tsquery('english', '"fat rat" or rat') + 'fat' <-> 'rat' | 'rat' + diff --git a/doc/src/sgml/textsearch.sgml b/doc/src/sgml/textsearch.sgml index 610b7bf0337..19f58511c82 100644 --- a/doc/src/sgml/textsearch.sgml +++ b/doc/src/sgml/textsearch.sgml @@ -797,13 +797,16 @@ UPDATE tt SET ti = PostgreSQL provides the functions to_tsquery, - plainto_tsquery, and - phraseto_tsquery + plainto_tsquery, + phraseto_tsquery and + websearch_to_tsquery for converting a query to the tsquery data type. to_tsquery offers access to more features than either plainto_tsquery or - phraseto_tsquery, but it is less forgiving - about its input. + phraseto_tsquery, but it is less forgiving about its + input. websearch_to_tsquery is a simplified version + of to_tsquery with an alternative syntax, similar + to the one used by web search engines. @@ -962,6 +965,87 @@ SELECT phraseto_tsquery('english', 'The Fat & Rats:C'); + +websearch_to_tsquery( config regconfig, querytext text) returns tsquery + + + + 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. + + + + + + Examples: + + select websearch_to_tsquery('english', 'The fat rats'); + websearch_to_tsquery + ----------------- + 'fat' & 'rat' + (1 row) + + + select websearch_to_tsquery('english', '"supernovae stars" -crab'); + websearch_to_tsquery + ---------------------------------- + 'supernova' <-> 'star' & !'crab' + (1 row) + + + select websearch_to_tsquery('english', '"sad cat" or "fat rat"'); + websearch_to_tsquery + ----------------------------------- + 'sad' <-> 'cat' | 'fat' <-> 'rat' + (1 row) + + + select websearch_to_tsquery('english', 'signal -"segmentation fault"'); + websearch_to_tsquery + --------------------------------------- + 'signal' & !( 'segment' <-> 'fault' ) + (1 row) + + + select websearch_to_tsquery('english', '""" )( dummy \\ query <->'); + websearch_to_tsquery + ---------------------- + 'dummi' & 'queri' + (1 row) + + -- cgit v1.2.3