Adam Johnson: PostgreSQL: Full text search with the “websearch” syntax

原文英文,约500词,阅读约需2分钟。发表于:

PostgreSQL’s powerful full text search feature supports several query syntaxes. Of these, a website search feature should typically pick the websearch syntax. websearch copies some features from popular search engines, as covered below, offering familiar short syntax to users. It is also forgiving and will never raise a syntax error for user input, whilst other syntaxes can. The syntax The websearch syntax discards stop words, such as “a” or “the” in English, then parses according to these rules: Individual words match independently. Double-quoted phrases match as a single unit. The word or (case-insensitive) specifies an “or” condition between two words or phrases. A - prefix specifies to not match the following word or phrase. The below table gives some examples mapping queries to their tsquery representations. Query tsquery Notes the donkey 'donkey' Stop word “the” removed the blue donkey 'blue' & 'donkey' The two words can appear in any order, with any number of words between "blue donkey" 'blue' <-> 'donkey' The words must appear together in the given order "the donkey" 'donkey' Stop words are removed within quotes donkey or mule 'donkey' | 'mule' Either word will match "blue donkey" or "red mule" 'blue' <-> 'donkey' | 'red' <-> 'mule' [...]

PostgreSQL的全文搜索功能支持多种查询语法,其中网站搜索功能通常应选择websearch语法。websearch从流行的搜索引擎中借鉴了一些功能,为用户提供了熟悉的简短语法。使用websearch_to_tsquery()函数解析查询以使用websearch语法。在Django中,使用SearchQuery(query, search_type="websearch")调用websearch_to_tsquery。感谢James Turk和Paolo Melchiorre的工作,使得Django支持了websearch语法。

Adam Johnson: PostgreSQL: Full text search with the “websearch” syntax
相关推荐 去reddit讨论