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 ...
随机推荐
- mysql 错误代码:1118解决方法
错误描述: 错误代码: 1118Row size too large. The maximum row size for the used table type, not counting BLOBs ...
- Cookie 和 Session机制具体解释
原文地址:http://blog.csdn.net/fangaoxin/article/details/6952954 会话(Session)跟踪是Web程序中经常使用的技术,用来跟踪用户的整 ...
- Jackson Gson Json.simple part 2
这篇blog介绍 Jackson 的特点和使用方法 Jackson支持三种使用方法 流API(streaming api Incremental parsing/generation) JsonPar ...
- PHP压缩html网页代码原理(清除空格,换行符,制表符,注释标记)
本博启用了一个叫wp super cache的页面压缩工具, 源代码没有去查看,不过原理很简单. 我们可以自己动手书写一个压缩脚本. 清除换行符,清除制表符,去掉注释标记 .它所起到的作用不可小视. ...
- .Net使用DES加密,.Net和java分别解密,并正则匹配替换加密密码为明文
在VS中用WindowsApplication做一个exe程序,用来给数据库密码加密,加密代码如下 private void generateBtn_Click(object sender, Even ...
- Linux应急响应(四):盖茨木马
0x00 前言 Linux盖茨木马是一类有着丰富历史,隐藏手法巧妙,网络攻击行为显著的DDoS木马,主要恶意特点是具备了后门程序,DDoS攻击的能力,并且会替换常用的系统文件进行伪装.木马得名于其 ...
- python virtualenv安装说明
环境说明: virtualenv安装: 第一步,创建目录code: 第二步,创建一个独立的Python运行环境,命名为venv: 新建的Python环境被放到当前目录下的venv目录. 第三步,有了v ...
- 设置开机自启动VirtualBox虚拟机系统
如果常用VirtualBox虚拟机系统的话,设置个随开机启动也是很方便的.不需要打开VirtualBox窗口,直接就是系统启动了. 又继续上网搜索学习了.(设置开机自启动VirtualBox虚拟机系统 ...
- openjdk源码阅读
http://rednaxelafx.iteye.com/blog/1549577 http://blog.csdn.net/fancyerii/article/details/7007503 ├—a ...
- mac 常用的终端命令
OSX 的文件系统 OSX 采用的Unix文件系统,所有文件都挂在跟目录 / 下面,所以不在要有Windows 下的盘符概念. 你在桌面上看到的硬盘都挂在 /Volumes 下. 比如接上个叫做 US ...