KingbaseES 内置的缺省的分词解析器采用空格分词,因为中文的词语之间没有空格分割,所以这种方法并不适用于中文。要支持中文的全文检索需要额外的中文分词插件:zhparser and sys_jieba,其中zhparser 支持 GBK 和 UTF8 字符集,sys_jieba 支持 UTF8 字符集。

一、默认空格分词

1、tsvector

  1. test=# SELECT to_tsvector('English','Try not to become a man of success, but rather try to become a man of value');
  2. to_tsvector
  3. ----------------------------------------------------------------------
  4. 'becom':4,13 'man':6,15 'rather':10 'success':8 'tri':1,11 'valu':17
  5. (1 row)
  6.  
  7. test=# SELECT to_tsvector('simple','Try not to become a man of success, but rather try to become a man of value');
  8. to_tsvector
  9. ---------------------------------------------------------------------------------------------------------------------
  10. 'a':5,14 'become':4,13 'but':9 'man':6,15 'not':2 'of':7,16 'rather':10 'success':8 'to':3,12 'try':1,11 'value':17
  11. (1 row)
  12.  
  13. test=# SELECT to_tsvector('Try not to become a man of success, but rather try to become a man of value');
  14. to_tsvector
  15. ---------------------------------------------------------------------------------------------------------------------
  16. 'a':5,14 'become':4,13 'but':9 'man':6,15 'not':2 'of':7,16 'rather':10 'success':8 'to':3,12 'try':1,11 'value':17
  17. (1 row)

这里可以看到,如果词干分析器是english ,会采取词干标准化的过程;而simple 只是转换成小写。默认是 simple。

  1. test=# show default_text_search_config;
  2. default_text_search_config
  3. ----------------------------
  4. pg_catalog.simple
  5. (1 row)

2、标准化过程

标准化过程会完成以下操作:

  1. 总是把大写字母换成小写的
  2. 也经常移除后缀(比如英语中的s,es和ing等),这样可以搜索同一个字的各种变体,而不是乏味地输入所有可能的变体。
  3. 数字表示词位在原始字符串中的位置,比如“man"出现在第6和15的位置上。
  4. to_tesvetor的默认配置的文本搜索是“英语“。它会忽略掉英语中的停用词(stopword,译注:也就是am is are a an等单词)。

3、tsvector搜索

  1. test=# SELECT to_tsvector('Try not to become a man of success, but rather try to become a man of value') @@ 'become';
  2. ?column?
  3. ----------
  4. t
  5. (1 row)
  6.  
  7. test=# SELECT to_tsvector('Try not to become a man of success, but rather try to become a man of value') @@ 'becom';
  8. ?column?
  9. ----------
  10. f
  11. (1 row)

  12. test=# select 'become'::tsquery,to_tsquery('become'),to_tsquery('english','become');
  13. tsquery | to_tsquery | to_tsquery
  14. ----------+------------+------------
  15. 'become' | 'become' | 'becom'
  16. (1 row)

to_tsquery 也会进行标准化转换,在搜索时必须用 to_tsquery,确保数据不会因为标准化转换而搜索不到。

4、逻辑操作

  1. test=# SELECT to_tsvector('Try not to become a man of success, but rather try to become a man of value') @@ to_tsquery('become');
  2. ?column?
  3. ----------
  4. t
  5. (1 row)
  6.  
  7. test=# SELECT to_tsvector('Try not to become a man of success, but rather try to become a man of value') @@ to_tsquery('!become');
  8. ?column?
  9. ----------
  10. f
  11. (1 row)
  12.  
  13. test=# SELECT to_tsvector('Try not to become a man of success, but rather try to become a man of value') @@ to_tsquery('tri & become');
  14. ?column?
  15. ----------
  16. t
  17. (1 row)
  18.  
  19. test=# SELECT to_tsvector('Try not to become a man of success, but rather try to become a man of value') @@ to_tsquery('Try & !becom');
  20. ?column?
  21. ----------
  22. f
  23. (1 row)
  24.  
  25. test=# SELECT to_tsvector('Try not to become a man of success, but rather try to become a man of value') @@ to_tsquery('Try | !become');
  26. ?column?
  27. ----------
  28. t
  29. (1 row)

5、可以用 :* 表示某词开始字符

  1. test=# SELECT to_tsvector('Try not to become a man of success, but rather try to become a man of value') @@ to_tsquery('bec:*');
  2. ?column?
  3. ----------
  4. t
  5. (1 row)

6、其他语言支持

  1. test=# SELECT to_tsvector('simple','Try not to become a man of success, but rather try to become a man of value');
  2. to_tsvector
  3. ---------------------------------------------------------------------------------------------------------------------
  4. 'a':5,14 'become':4,13 'but':9 'man':6,15 'not':2 'of':7,16 'rather':10 'success':8 'to':3,12 'try':1,11 'value':17
  5. (1 row)
  6.  
  7. test=# SELECT to_tsvector('english','Try not to become a man of success, but rather try to become a man of value') ;
  8. to_tsvector
  9. ----------------------------------------------------------------------
  10. 'becom':4,13 'man':6,15 'rather':10 'success':8 'tri':1,11 'valu':17
  11. (1 row)
  12. ^
  13. test=# SELECT to_tsvector('french','Try not to become a man of success, but rather try to become a man of value') ;
  14. to_tsvector
  15. -----------------------------------------------------------------------------------------------------------------
  16. 'a':5,14 'becom':4,13 'but':9 'man':6,15 'not':2 'of':7,16 'rath':10 'success':8 'to':3,12 'try':1,11 'valu':17
  17. (1 row)
  18. ^
  19. test=# SELECT to_tsvector('french'::regconfig,'Try not to become a man of success, but rather try to become a man of value') ;
  20. to_tsvector
  21. -----------------------------------------------------------------------------------------------------------------
  22. 'a':5,14 'becom':4,13 'but':9 'man':6,15 'not':2 'of':7,16 'rath':10 'success':8 'to':3,12 'try':1,11 'valu':17
  23. (1 row)

simple并不忽略禁用词表,它也不会试着去查找单词的词根。使用simple时,空格分割的每一组字符都是一个语义;simple 只做了小写转换;对于数据来说,simple文本搜索配置项很实用。 

二、中文检索

在开始介绍中文检索前,我们先来看个例子:

  1. test=# select to_tsvector('人大金仓致力于提供高可靠的数据库产品');
  2. to_tsvector
  3. ------------------------------------------
  4. '人大金仓致力于提供高可靠的数据库产品':1

因为内置的分词器是按空格分割的,而中文间没有空格,因此,整句话就被看做一个分词。

1、创建中文搜索插件

  1. create extension zhparser;
  2. create text search configuration zhongwen_parser (parser = zhparser);
  3. alter text search configuration zhongwen_parser add mapping for n,v,a,i,e,l,j with simple;

上面 for 后面的字母表示分词的token,上面的token映射只映射了名词(n),动词(v),形容词(a),成语(i),叹词(e),缩写(j) 和习用语(l)6种,这6种以外的token全部被屏蔽。词典使用的是内置的simple词典。具体的token 如下:

  1. test=# select ts_token_type('zhparser');
  2. ts_token_type
  3. ------------------------
  4. (97,a,adjective)
  5. (98,b,differentiation)
  6. (99,c,conjunction)
  7. (100,d,adverb)
  8. (101,e,exclamation)
  9. (102,f,position)
  10. (103,g,root)
  11. (104,h,head)
  12. (105,i,idiom)
  13. (106,j,abbreviation)
  14. (107,k,tail)
  15. (108,l,tmp)
  16. (109,m,numeral)
  17. (110,n,noun)
  18. (111,o,onomatopoeia)
  19. (112,p,prepositional)
  20. (113,q,quantity)
  21. (114,r,pronoun)
  22. (115,s,space)
  23. (116,t,time)
  24. (117,u,auxiliary)
  25. (118,v,verb)
  26. (119,w,punctuation)
  27. (120,x,unknown)
  28. (121,y,modal)
  29. (122,z,status)
  30. (26 rows)

2、查看pg_ts_config

创建text search configuration 后,可以在视图pg_ts_config 看到如下信息:

  1. test=# select * from pg_ts_config;
  2. oid | cfgname | cfgnamespace | cfgowner | cfgparser
  3. -------+-----------------+--------------+----------+-----------
  4. 3748 | simple | 11 | 10 | 3722
  5. 13265 | arabic | 11 | 10 | 3722
  6. 13267 | danish | 11 | 10 | 3722
  7. 13269 | dutch | 11 | 10 | 3722
  8. 13271 | english | 11 | 10 | 3722
  9. 13273 | finnish | 11 | 10 | 3722
  10. 13275 | french | 11 | 10 | 3722
  11. 13277 | german | 11 | 10 | 3722
  12. 13279 | hungarian | 11 | 10 | 3722
  13. 13281 | indonesian | 11 | 10 | 3722
  14. 13283 | irish | 11 | 10 | 3722
  15. 13285 | italian | 11 | 10 | 3722
  16. 13287 | lithuanian | 11 | 10 | 3722
  17. 13289 | nepali | 11 | 10 | 3722
  18. 13291 | norwegian | 11 | 10 | 3722
  19. 13293 | portuguese | 11 | 10 | 3722
  20. 13295 | romanian | 11 | 10 | 3722
  21. 13297 | russian | 11 | 10 | 3722
  22. 13299 | spanish | 11 | 10 | 3722
  23. 13301 | swedish | 11 | 10 | 3722
  24. 13303 | tamil | 11 | 10 | 3722
  25. 13305 | turkish | 11 | 10 | 3722
  26. 16390 | parser_name | 2200 | 10 | 16389
  27. 24587 | zhongwen_parser | 2200 | 10 | 16389

3、使用中文分词

  1. test=# select to_tsvector('zhongwen_parser','人大金仓致力于提供高可靠的数据库产品');
  2. to_tsvector
  3. ------------------------------------------------------------------
  4. '产品':7 '人大':1 '可靠':5 '提供':3 '数据库':6 '致力于':2 '高':4

4、contains 函数

  1. test=# \df+ contains
  2. List of functions
  3. Schema | Name | Result data type | Argument data types | Type | Volatility | Parallel | Owner | Security | Access privileges | Language | Source code
  4. | Description
  5. --------+----------+------------------+---------------------+------+------------+----------+--------+----------+-------------------+----------+------------------------------------------+-------------
  6. sys | contains | boolean | text, text | func | immutable | safe | system | invoker | | sql | select to_tsvector($1) @@ to_tsquery($2) |
  7. sys | contains | boolean | text, text, integer | func | immutable | safe | system | invoker | | sql | select to_tsvector($1) @@ to_tsquery($2) |
  8. sys | contains | boolean | text, tsquery | func | immutable | safe | system | invoker | | sql | select $1::tsvector @@ $2 |
  9. sys | contains | boolean | tsvector, text | func | immutable | safe | system | invoker | | sql | select $1 @@ $2::tsquery |
  10. sys | contains | boolean | tsvector, tsquery | func | immutable | safe | system | invoker | | sql | select $1 @@ $2 |

默认contains 函数使用的是空格分词解析器,因此,无法使用contains 进行中文判断

  1. test=# select contains('人大金仓致力于提供高可靠的数据库产品','产品');
  2. contains
  3. ----------
  4. f

KingbaseES 全文检索功能介绍的更多相关文章

  1. .NET平台开源项目速览(13)机器学习组件Accord.NET框架功能介绍

    Accord.NET Framework是在AForge.NET项目的基础上封装和进一步开发而来.因为AForge.NET更注重与一些底层和广度,而Accord.NET Framework更注重与机器 ...

  2. 微信小程序产品定位及功能介绍

    产品定位及功能介绍 微信小程序是一种全新的连接用户与服务的方式,它可以在微信内被便捷地获取和传播,同时具有出色的使用体验. 小程序注册 注册小程序帐号 在微信公众平台官网首页(mp.weixin.qq ...

  3. 带你走近AngularJS - 基本功能介绍

    带你走近AngularJS系列: 带你走近AngularJS - 基本功能介绍 带你走近AngularJS - 体验指令实例 带你走近AngularJS - 创建自定义指令 ------------- ...

  4. MWeb 1.4 新功能介绍一:引入文件夹到 MWeb 中管理,支持 Octpress、Jekyll 等静态博客拖拽插入图片和实时预览

    之前在 MWeb 中打开非文档库中的 Markdown 文档,如果文档中有引用到本机图片,是没办法在 MWeb 中显示出来和预览的.这是因为 Apple 规定在 Mac App Store(MAS) ...

  5. Joomla软件功能介绍与开源程序大比拼Joomla,wordpress,Drupal哪个好?

    Joomla 软件功能介绍:    Joomla!是一套在国外相当知名的内容管理系统 (Content Management System, CMS),它属于Portal(企业入口网站)类型,顾名思义 ...

  6. CentOS以及Oracle数据库发展历史及各版本新功能介绍, 便于构造环境时有个对应关系

    CentOS版本历史 版本 CentOS版本号有两个部分,一个主要版本和一个次要版本,主要和次要版本号分别对应于RHEL的主要版本与更新包,CentOS采取从RHEL的源代码包来构建.例如CentOS ...

  7. python中列表、元组、字典内部功能介绍

    一.列表(list) 常用功能的介绍:

  8. 网页引导:jQuery插件实现的页面功能介绍引导页效果

    现在很多网站不仅是介绍,更多的是有一些功能,怎么样让客户快速的知道网站有哪些功能呢?这里pagewalkthrough.js插件能帮我们实现,它是一个轻量级的jQuery插件,它可以帮助我们创建一个遮 ...

  9. 原创开源项目HierarchyViewer for iOS 2.1 Beta新功能介绍

    回顾 HierarchyViewer for iOS是我们发布的一个开源项目,采用GPL v3.0协议. HierarchyViewer for iOS可以帮助iOS应用的开发和测试人员,在没有源代码 ...

随机推荐

  1. sublime_text 3安装Emmet时出现PyV8警告

    使用Emmet是需要在PyV8依赖下才可以的.1. 下面是下载网址:PyV8下载地址 下载自己系统版本的压缩包,然后解压,自己创建一个名为PyV8文件夹.将解压后的文件放入该文件夹里. 打开首选项里的 ...

  2. Servlet 之 Http协议

    请求消息数据格式 请求行  请求方式 请求url 请求协议或者版本 (GET    /login.html   HTTP/1.1) 请求头 请求头名称:请求头值 多个用逗号分隔 请求空行  空行分隔作 ...

  3. Halcon 模板匹配实战代码(一)

    模板图片:目标是获取图像左上角位置的数字 直接想法,直接用一个框将数字框出来,然后对图片进行模板匹配(不可行,因为图像中的数字不是固定的) 所以需要选择图像中的固定不变的区域来作为模板,然后根据模板区 ...

  4. 零基础学Java(4)字符串

    字符串 从概念上讲,Java字符串就是Unicode字符序列.例如,字符串"Java\u2122"由5个Unicode字符J.a.v.a和组成.Java没有内置的字符串类型,而是在 ...

  5. 生成RSA密钥的方法[转载]

    openssl genrsa -des3 -out privkey.pem 2048 这个命令会生成一个2048位的密钥,同时有一个des3方法加密的密码,如果你不想要每次都输入密码,可以改成(测试常 ...

  6. 研发效能|Kubernetes核心技术剖析和DevOps落地经验

    本文主要介绍Kubernetes 的核心组件.架构.服务编排,以及在集群规模.网络&隔离.SideCar.高可用上的一些使用建议,尤其是在CICD中落地,什么是 GitOps. 通过此文可彻底 ...

  7. Eolink 推出面向中小企业及初创企业支持计划,为企业赋能!

    2022,疫情持续蔓延,Eolink 作为一家初创公司,深切地感受到疫情下中小企业和初创企业的不易. Eolink 宣布正式推出「 Eolink 微光计划」,面向中小企业和初创企业,提供免费一年的私有 ...

  8. CodeQL使用流程

    前言 好久没用CodeQL了,看了自己之前写的文章发现竟然没有做过相关记录 然后就不知道怎么用了hhh 使用流程 0x1 生成数据库 我们拿到一套源码,首先需要使用CodeQL生成数据库 执行命令: ...

  9. kube-scheduler的调度上下文

    前一章节了解到了kube-scheduler中的概念,该章节则对调度上下文的源码进行分析 Scheduler Scheduler 是整个 kube-scheduler 的一个 structure,提供 ...

  10. 搭建一个完整的K8S集群-------基于CentOS 8系统

    创建三个centos节点: 192.168.5.141 k8s-master 192.168.5.142 k8s-nnode1 192.168.5.143 k8s-nnode2 查看centos系统版 ...