datatype-textsearch.zh.md 1.8 KB
Newer Older
K
KyleZhang 已提交
1 2
## 8.11. Text Search Types

李少辉-开发者's avatar
李少辉-开发者 已提交
3
[8.11.1.`tsvector`](datatype-textsearch.html#DATATYPE-TSVECTOR)
茶陵後's avatar
茶陵後 已提交
4

李少辉-开发者's avatar
李少辉-开发者 已提交
5
[8.11.2.`tsquery`](datatype-textsearch.html#DATATYPE-TSQUERY)
K
KyleZhang 已提交
6

李少辉-开发者's avatar
李少辉-开发者 已提交
7
[](<>)[](<>)
K
KyleZhang 已提交
8

李少辉-开发者's avatar
李少辉-开发者 已提交
9
PostgreSQL provides two data types that are designed to support full text search, which is the activity of searching through a collection of natural-language*documents*to locate those that best match a*query*. The`tsvector`type represents a document in a form optimized for text search; the`tsquery`type similarly represents a text query.[Chapter 12](textsearch.html)provides a detailed explanation of this facility, and[Section 9.13](functions-textsearch.html)summarizes the related functions and operators.
K
KyleZhang 已提交
10

李少辉-开发者's avatar
李少辉-开发者 已提交
11
### 8.11.1.`tsvector`
K
KyleZhang 已提交
12

李少辉-开发者's avatar
李少辉-开发者 已提交
13
[](<>)
K
KyleZhang 已提交
14

李少辉-开发者's avatar
李少辉-开发者 已提交
15
A`tsvector`value is a sorted list of distinct*lexemes*, which are words that have been*normalized*to merge different variants of the same word (see[Chapter 12](textsearch.html)for details). Sorting and duplicate-elimination are done automatically during input, as shown in this example:
K
KyleZhang 已提交
16 17 18 19 20 21 22 23 24 25 26 27 28 29

```
SELECT 'a fat cat sat on a mat and ate a fat rat'::tsvector;
                      tsvector
### 8.11.2. `tsquery`

[]()

 A `tsquery` value stores lexemes that are to be searched for, and can combine them using the Boolean operators `&` (AND), `|` (OR), and `!` (NOT), as well as the phrase search operator `<->` (FOLLOWED BY). There is also a variant `<*`N`*>` of the FOLLOWED BY operator, where *`N`* is an integer constant that specifies the distance between the two lexemes being searched for. `<->` is equivalent to `<1>`.

 Parentheses can be used to enforce grouping of these operators. In the absence of parentheses, `!` (NOT) binds most tightly, `<->` (FOLLOWED BY) next most tightly, then `&` (AND), with `|` (OR) binding the least tightly.

 Here are some examples:
```
李少辉-开发者's avatar
李少辉-开发者 已提交
30 31

SELECT 'fat & rat'::tsquery;     tsquery