SELECT'a fat cat sat on a mat and ate a fat rat'::tsvector@@'cat & rat'::tsquery;?column?----------
tSELECT'cat & rat'::tsquery@@'a fat cat sat on a mat and ate a fat rat'::tsvector;?column?----------
t
提示
如上例所示,tsquery 与 tsvector 一样,并不仅仅是原始文本。tsquery 包含必须已经规范化为词位(lexeme)的搜索词项,并且可以使用 AND、OR、NOT 以及 FOLLOWED BY 操作符组合多个词项。在本例中,tsquery'cat & rat' 与 tsvector'a fat cat sat on a mat and ate a fat rat' 匹配,因为文档中同时包含 'cat' 和 'rat' 两个词项。
CREATETABLEitems(idbigserialPRIMARYKEY,contenttextNOTNULL,embeddingvector(3)NOTNULL-- 3 维
);INSERTINTOitems(content,embedding)VALUES('a fat cat sat on a mat and ate a fat rat','[1, 2, 3]'),('a fat dog sat on a mat and ate a fat rat','[4, 5, 6]'),('a thin cat sat on a mat and ate a thin rat','[7, 8, 9]'),('a thin dog sat on a mat and ate a thin rat','[10, 11, 12]');
查询该表将得到如下内容:
postgres=# select * from items;
id | content | embedding
----+--------------------------------------------+--------------
2 | a fat cat sat on a mat and ate a fat rat | [1, 2, 3]
3 | a fat dog sat on a mat and ate a fat rat | [4, 5, 6]
4 | a thin cat sat on a mat and ate a thin rat | [7, 8, 9]
5 | a thin dog sat on a mat and ate a thin rat | [10, 11, 12]
(4 rows)
SELECTto_tsvector('english','a fat cat sat on a mat - it ate a fat rats');to_tsvector-----------------------------------------------------
'ate':9'cat':3'fat':2,11'mat':7'rat':12'sat':4
content | embedding
--------------------------------------------+-----------
a fat cat sat on a mat and ate a fat rat | [1, 2, 3]
a thin cat sat on a mat and ate a thin rat | [7, 8, 9]
(2 rows)
content | embedding
--------------------------------------------+-----------
a thin cat sat on a mat and ate a thin rat | [7, 8, 9]
a fat cat sat on a mat and ate a fat rat | [1, 2, 3]
(2 rows)
id | content | embedding | rank
----+--------------------------------------------+--------------+------------------------
4 | a thin cat sat on a mat and ate a thin rat | [7, 8, 9] | 1.00000000000000000000
2 | a fat cat sat on a mat and ate a fat rat | [1, 2, 3] | 0.70000000000000000000
3 | a fat dog sat on a mat and ate a fat rat | [4, 5, 6] | 0.33333333333333333333
5 | a thin dog sat on a mat and ate a thin rat | [10, 11, 12] | 0.33333333333333333333
(4 rows)