postgresql----文本搜索类型和检索函数
postgresql提供两种数据类型用于支持全文检索:tsvector类型产生一个文档(以优化全文检索形式)和tsquery类型用于查询检索。
tsvector的值是一个无重复的lexemes排序列表(什么是lexemes?),比如将一个字符串转换为tsvector类型:
test=# SELECT $$the lexeme ' ' contains spaces$$::tsvector;
tsvector
----------------------------------------
' ' 'contains' 'lexeme' 'spaces' 'the'
(1 row)
可以在单词后面跟:数字表示单词在字符串中的位置,数字的范围是1-16383。
test=# SELECT $$a:1 fat:2 cat:3 sat:4 on:5 a:6 mat:7 and:8 ate:9 a:10 fat:11 rat:12$$::tsvector;
tsvector
-------------------------------------------------------------------------------
'a':1,6,10 'and':8 'ate':9 'cat':3 'fat':2,11 'mat':7 'on':5 'rat':12 'sat':4
(1 row)
还可以在位置后面跟字母标识权,字母的范围是A,B,C,D,默认是D,不显示。
test=# SELECT $$a:1A fat:2B,4C cat:5D$$::tsvector;
tsvector
----------------------------
'a':1A 'cat':5 'fat':2B,4C
(1 row)
tsquery存储用于检索的词汇,并且使用布尔操作符&(AND),|(OR),!(NOT)来组合它们。!(NOT)结合的最紧密,而&(AND)结合的比|(OR)紧密,也可以用括号来强调分组。
test=# SELECT $$fat & rat$$::tsquery;
tsquery
---------------
'fat' & 'rat'
(1 row) test=#
test=# SELECT $$fat & (rat | cat)$$::tsquery;
tsquery
---------------------------
'fat' & ( 'rat' | 'cat' )
(1 row) test=#
test=# SELECT $$fat & rat & ! cat$$::tsquery;
tsquery
------------------------
'fat' & 'rat' & !'cat'
(1 row)
tsquery中的词汇可以用*进行标记来指定前缀匹配:
test=# SELECT to_tsvector('postgraduate');
to_tsvector
---------------
'postgradu':1
(1 row)
test=# SELECT to_tsquery( 'postgres:*');
to_tsquery
------------
'postgr':*
(1 row)
test=# SELECT to_tsvector('postgraduate') @@ to_tsquery( 'postgres:*');
?column?
----------
t
(1 row)
为什么呢???我也没明白,继续往下看吧!
文本检索函数和操作符
操作符
| 操作符 | 返回类型 | 描述 | 示例 | 结果 |
| @@ | boolean | tsvector是否匹配tsquery | select to_tsvector('fat cats ate rats') @@ to_tsquery('cat & rat'); | t |
| @@@ | boolean | 废弃的@@ | ||
| || | tsvector | 连接tsvector | select 'a:1 b:2'::tsvector || 'c:1 d:2 b:3'::tsvector; | 'a':1 'b':2,5 'c':3 'd':4 |
| && | tsquery | tsquery与 | select 'fat | rat'::tsquery && 'cat'::tsquery; | ( 'fat' | 'rat' ) & 'cat' |
| || | tsquery | tsquery或 | select 'fat | rat'::tsquery || 'cat'::tsquery; | 'fat' | 'rat' | 'cat' |
| !! | tsquery | tsquery非 | select !! 'cat'::tsquery; | !'cat' |
| <-> | tsquery | tsquery紧跟tsquery | select to_tsquery('fat') <-> to_tsquery('rat'); | 'fat' <-> 'rat' |
| @> | boolean | tsquery包含另一个tsquery | select 'cat'::tsquery @> 'cat & rat'::tsquery; | f |
| <@ | boolean | tsquery是否包含于另一个tsquery | select 'cat'::tsquery <@ 'cat & rat'::tsquery; | t |
函数
| 函数 | 返回类型 | 描述 | 示例 | 结果 |
| array_to_tsvector(text[]) | tsvector | 将text数组转换成tsvector | select array_to_tsvector('{fat,cat,rat}'::text[]); | 'fat' 'cat' 'rat' |
| get_current_ts_config() | regconfig | 获取当前文本检索配置 | select get_current_ts_config(); | english |
| length(tsvector) | integer | tsvector中单词个数 | select length('fat:2,4 cat:3 rat:5A'::tsvector); | 3 |
| numnode(tsquery) | integer | tsquery中的单词加上操作符的数量 | select numnode('(fat & rat) | cat'::tsquery); | 5 |
| plainto_tsquery([ config regconfig , ] query text) | tsquery | 忽略标点(punctuation)生成tsquery | select plainto_tsquery('english', 'The Fat Rats'); | 'fat' & 'rat' |
| phraseto_tsquery([ config regconfig , ] query text) | tsquery | 忽略标点(punctuation)生成tsquery用于检索短语 | select phraseto_tsquery('english', 'The Fat Rats'); | 'fat' <-> 'rat' |
| querytree(query tsquery) | text | 获取tsquery可索引部分 | select querytree('foo & ! bar'::tsquery); | 'foo' |
| setweight(vector tsvector, weight "char") | tsvector | 给tsvector每一个元素赋权 | select setweight('fat:2,4 cat:3 rat:5B'::tsvector, 'A'); | 'cat':3A 'fat':2A,4A 'rat':5A |
| setweight(vector tsvector, weight "char", lexemestext[]) | tsvector | 给tsvector给指定元素赋权 | select setweight('fat:2,4 cat:3 rat:5B'::tsvector, 'A', '{cat,rat}'); | 'cat':3A 'fat':2,4 'rat':5A |
| strip(tsvector) | tsvector | 删除tsvector中的位置和权 | select strip('fat:2,4 cat:3 rat:5A'::tsvector); | 'cat' 'fat' 'rat' |
| to_tsquery([ config regconfig , ] query text) | tsquery | 标准化单词并转换为tsquery | select to_tsquery('english', 'The & Fat & Rats'); | 'fat' & 'rat' |
| to_tsvector([ config regconfig , ] document text) | tsvector | 减少文本至tsvector | select to_tsvector('english', 'The Fat Rats'); | 'fat':2 'rat':3 |
| ts_delete(vector tsvector, lexeme text) | tsvector | 从tsvector中删除指定元素 | select ts_delete('fat:2,4 cat:3 rat:5A'::tsvector, 'fat'); | 'cat':3 'rat':5A |
| ts_delete(vector tsvector, lexemes text[]) | tsvector | 从tsvector中删除指定的一组元素 | select ts_delete('fat:2,4 cat:3 rat:5A'::tsvector, ARRAY['fat','rat']); | 'cat':3 |
| ts_filter(vector tsvector, weights "char"[]) | tsvector | 只查询指定权值的元素 | select ts_filter('fat:2,4 cat:3b rat:5A'::tsvector, '{a,b}'); | 'cat':3B 'rat':5A |
|
ts_headline([ config regconfig, ] document text, querytsquery [, options text ]) |
text | 显示一个查询匹配项 | select ts_headline('x y z', 'z'::tsquery); | x y <b>z</b> |
|
ts_rank([ weights float4[], ] vector tsvector, querytsquery [, normalization integer ]) |
float4 | 为查询进行文档排序 | select ts_rank('fat:2,4 cat:3b rat:5A'::tsvector, 'rat'::tsquery); | 0.607927 |
|
ts_rank_cd([ weights float4[], ] vector tsvector, querytsquery [, normalization integer ]) |
float4 | 使用覆盖密度为查询进行文档排序 | select ts_rank_cd('{0.1, 0.2, 0.4, 1.0}','fat:2,4 cat:3b rat:5A'::tsvector, 'rat'::tsquery); | 1 |
|
ts_rewrite(query tsquery, target tsquery, substitute tsquery) |
tsquery | 使用 substitute替换target中query | select ts_rewrite('a & b'::tsquery, 'a'::tsquery, 'foo|bar'::tsquery); | 'b' & ( 'foo' | 'bar' ) |
| ts_rewrite(query tsquery, select text) | tsquery | 从SELECT结果中得到的substitute和target来替换query,前提是SELECT必须要有结果 | SELECT ts_rewrite('a & b'::tsquery, 'SELECT t,s FROM aliases') | |
| tsquery_phrase(query1 tsquery, query2 tsquery) | tsquery | 功能同操作符 <-> | select tsquery_phrase(to_tsquery('fat'), to_tsquery('cat')); | 'fat' <-> 'cat' |
|
tsquery_phrase(query1 tsquery, query2 tsquery, distanceinteger) |
tsquery | 确保query1和query2之间距离最大为distance | select tsquery_phrase(to_tsquery('fat'), to_tsquery('cat'), 10); | 'fat' <10> 'cat' |
| tsvector_to_array(tsvector) | text[] | 将tsvector转换为数组 | select tsvector_to_array('fat:2,4 cat:3 rat:5A'::tsvector); | {cat,fat,rat} |
| tsvector_update_trigger() | trigger | 更新tsvector列自动触发触发器函数 | ||
| tsvector_update_trigger_column() | trigger | 同上 | ||
| unnest(tsvector, OUT lexeme text, OUT positionssmallint[], OUT weights text) | setof record | 将tsvector扩展成行类型 | select unnest('fat:2,4 cat:3 rat:5A'::tsvector); |
(cat,{3},{D}) |
工作中没有用过文本检索类型,英语也不好,有兴趣还是去看原版吧:
https://www.postgresql.org/docs/9.6/static/functions-textsearch.html
postgresql----文本搜索类型和检索函数的更多相关文章
- PostgreSQL 的字段类型和表操作笔记
字段类型 数值类型 Name Storage Size Description Range smallint 2 bytes small-range integer -32768 to +32767 ...
- PostgreSQL 基本数据类型及常用SQL 函数操作
数据类型 名字 别名 描述 bigint int8 有符号的8字节整数 bigserial serial8 自动增长的8字节整数 bit [ (n) ] 定长位串 bit varying [ (n ...
- MySQL(十)操纵表及全文本搜索
一.创建表 MySQL不仅用于表数据操作,还可以用来执行数据库和表的所有操作,包括表本身的创建和处理. 创建表一般有如下两种方式: ①使用具有交互式创建和管理表的工具: ②直接使用MySQL语句操纵表 ...
- PostgreSQL各数据类型的内置函数
参考<PostgreSQL实战> 3.1.2 数字类型操作符和数学函数 PostgreSQL 支持数字类型操作符和丰富的数学函数 例如支持加.减.乘.除.模取取余操作符 SELECT 1+ ...
- 《mysql必知必会》笔记2(子查询、联接、组合查询、全文本搜索)
十四:使用子查询 1:子查询是嵌套在其他查询中的查询. 2:需要列出订购TNT2的所有客户信息,需要下面几步: a:从orderitems表中检索出包含物品TNT2的所有订单号: b:根据上一步得出的 ...
- eclipse 设置文本模板中 insert variable... 函数 详解
设置文本模板简要图: 设置文本模板详细过程:http://www.cnblogs.com/lsy131479/p/8478711.html 此处引出设置文本模板中 insert variable... ...
- Nebula 基于 ElasticSearch 的全文搜索引擎的文本搜索
本文首发于 Nebula Graph 公众号 NebulaGraphCommunity,Follow 看大厂图数据库技术实践. 1 背景 Nebula 2.0 中已经支持了基于外部全文搜索引擎的文本查 ...
- PostgreSQL Array 数组类型与 FreeSql 打出一套【组合拳】
前言 PostgreSQL 是世界公认的功能最强大的开源数据库,除了基础数据类型 int4/int8/varchar/numeric/timestamp 等数据类型,还支持 int4[]/int8[] ...
- 介绍一个很爽的 php 字符串特定检索函数---strpos()
大家在用 php 开发的时候 是否 有遇到过,对于一个获取的字符串,如果想要特定检测它是否 含有某个特定的字符或者子字符串,总是找不到好方法,或者根本做不到,迫于无奈而使用foreach. 函数: s ...
随机推荐
- kettle教程二
转载:http://www.cnblogs.com/limengqiang/archive/2013/01/16/KettleApply2.html 1.应用场景 这里简单概括一下几种具体的应用场景, ...
- css3-rem
http://www.w3cplus.com/css3/define-font-size-with-css3-rem https://mp.weixin.qq.com/s/DpLXJhfCHsgrbg ...
- CDbConnection failed to open the DB connection: could not find driver错误的处理
在PHP.INI文件中extension=php_pdo_mysql.dll 去掉注释
- 条件独立(conditional independence) 结合贝叶斯网络(Bayesian network) 概率有向图 (PRML8.2总结)
本文会利用到上篇,博客的分解定理,需要的可以查找上篇博客 D-separation对任何用有向图表示的概率模型都成立,无论随机变量是离散还是连续,还是两者的结合. 部分图为手写,由于本人字很丑,望见谅 ...
- Java中Volatile详解
当前的Java内存模型下,线程可以把变量保存在本地内存(比如机器的寄存器)中,而不是直接在主存中进行读写.这就可能造成一个线程在主存中修改了一个变量的值,而另外一个线程还继续使用它在寄存器中的变量值的 ...
- 使用Socket抓取网页源码
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import ...
- Jquery/js submit()无法提交问题
有朋友可能会直接利用js或jquery来提交数据而不是使用表单直接提交了,小编来给大家介绍小编碰到的一个问题就是 submit()无法提交,下面我们来看解决办法与原因分析. jquery无法提交 代 ...
- Ubuntu 13.10 下安装搜狗输入法
1.卸载ibus输入法: sudo apt-get remove ibus sudo为取得root权限的意思,Ubuntu系统默认root账户关闭,很多操作需要取得root 权限才可以 ...
- (原)android修改文件所属的用户组
首先得安装了busybox: 命令如下: busybox fileName 其中的0表示root,改成1000则表示system,改成2000则表示shell.
- React组件设计(转)
React组件设计 组件分类 展示组件和容器组件 展示组件 容器组件 关注事物的展示 关注事物如何工作 可能包含展示和容器组件,并且一般会有DOM标签和css样式 可能包含展示和容器组件,并且不会有D ...