设为首页 收藏本站
查看: 836|回复: 0

[经验分享] 如何为Postgresql数据库全文搜索(full text search)编写解析器(parser)

[复制链接]

尚未签到

发表于 2016-11-21 06:36:36 | 显示全部楼层 |阅读模式
  用英文写的,主要是命令和代码,就不翻译了,偷懒一下。
  This article show you how to create a parser to handle camel case [3] in
string in Postgresql full text search. This parser is tested on Postgresql 8.3.9
version. The OS is ubuntu 8.10.
  [1,2] will give you basic background and sample for old version. I update the
code to fit 8.3.9 version, especial lots of changes in the SQL statement.

Create parser
  First, create a directory for put all files, for example: /home/user/Desktop/test_parser
  Create file "test_parser.c". I use vim.


#include "postgres.h"


#include "utils/builtins.h"







#ifdef PG_MODULE_MAGIC


PG_MODULE_MAGIC;


#endif




/*


* types


*/




/* self-defined type */


typedef struct {


char * buffer; /* text to parse */


int len; /* length of the text in buffer */


int pos; /* position of the parser */


} ParserState;




/* copy-paste from wparser.h of tsearch2 */


typedef struct {


int lexid;


char *alias;


char *descr;


} LexDescr;




/*


* prototypes


*/


PG_FUNCTION_INFO_V1(testprs_start);


Datum testprs_start(PG_FUNCTION_ARGS);




PG_FUNCTION_INFO_V1(testprs_getlexeme);


Datum testprs_getlexeme(PG_FUNCTION_ARGS);




PG_FUNCTION_INFO_V1(testprs_end);


Datum testprs_end(PG_FUNCTION_ARGS);




PG_FUNCTION_INFO_V1(testprs_lextype);


Datum testprs_lextype(PG_FUNCTION_ARGS);




/*


* functions


*/


Datum testprs_start


(PG_FUNCTION_ARGS)


{


ParserState *pst = (ParserState *) palloc(sizeof(ParserState));


pst->buffer = (char *) PG_GETARG_POINTER(0);


pst->len = PG_GETARG_INT32(1);


pst->pos = 0;


PG_RETURN_POINTER(pst);


}




Datum testprs_getlexeme


(PG_FUNCTION_ARGS)


{


ParserState *pst = (ParserState *) PG_GETARG_POINTER(0);


char **t = (char **) PG_GETARG_POINTER(1);


int *tlen = (int *) PG_GETARG_POINTER(2);


int type;




*tlen = pst->pos;


*t = pst->buffer + pst->pos;




HTML clipboard/* main process here */

if (((pst->buffer)[pst->pos] >= 'A' && (pst->buffer)[pst->pos] <= 'Z') &&
(pst->pos < pst->len)) {

/* word type */

type = 3;

(pst->pos)++;

/* for case like: ItemURL => Item URL*/

if(((pst->buffer)[pst->pos] >= 'A' && (pst->buffer)[pst->pos] <= 'Z') && (pst->pos
< pst->len)){

while (((pst->buffer)[pst->pos] >= 'A' && (pst->buffer)[pst->pos] <= 'Z')
&& (pst->pos < pst->len)) {

(pst->pos)++;

}

}


/* go to the next upper case character */

while (((pst->buffer)[pst->pos] >= 'a' && (pst->buffer)[pst->pos] <= 'z')
&& (pst->pos < pst->len)) {

(pst->pos)++;

}

}


if (((pst->buffer)[pst->pos] >= 'a' && (pst->buffer)[pst->pos] <= 'z') &&
(pst->pos < pst->len)) {

/* word type */

type = 3;

(pst->pos)++;

/* go to the next upper case character */

while (((pst->buffer)[pst->pos] >= 'a' && (pst->buffer)[pst->pos] <= 'z')
&& (pst->pos < pst->len)) {

(pst->pos)++;

}

}




*tlen = pst->pos - *tlen;




/* we are finished if (*tlen == 0) */


if (*tlen == 0) type=0;


PG_RETURN_INT32(type);


}




Datum testprs_end


(PG_FUNCTION_ARGS)


{


ParserState *pst = (ParserState *) PG_GETARG_POINTER(0);


pfree(pst);


PG_RETURN_VOID();


}




Datum testprs_lextype


(PG_FUNCTION_ARGS)


{


/*


Remarks:


- we have to return the blanks for headline reason


- we use the same lexids like Teodor in the default


word parser; in this way we can reuse the headline


function of the default word parser.


*/


LexDescr *descr = (LexDescr *) palloc(sizeof(LexDescr) * (2+1));




/* there are only two types in this parser */


descr[0].lexid = 3;


descr[0].alias = pstrdup("word");


descr[0].descr = pstrdup("Word");


descr[1].lexid = 12;


descr[1].alias = pstrdup("blank");


descr[1].descr = pstrdup("Space symbols");


descr[2].lexid = 0;


PG_RETURN_POINTER(descr);


}
  Create "Makefile" file under the same directory.


override CPPFLAGS := -I. $(CPPFLAGS)

MODULE_big = test_parser

OBJS = test_parser.o


DATA_built = test_parser.sql

DATA =

REGRESS = test_parser


ifdef USE_PGXS

PGXS := $(shell pg_config --pgxs)

include $(PGXS)

else

subdir = contrib/test_parser

top_builddir = ../..

include $(top_builddir)/src/Makefile.global

include $(top_srcdir)/contrib/contrib-global.mk

endif

  Create "test_parser.sql" file under the same directory.


SET search_path = public;

BEGIN;


--DROP TEXT SEARCH CONFIGURATION testcfg;

--DROP TEXT SEARCH PARSER testparser;


CREATE OR REPLACE FUNCTION testprs_start(internal,int4)

RETURNS internal

AS '$libdir/test_parser'

LANGUAGE 'C';


CREATE OR REPLACE FUNCTION testprs_getlexeme(internal,internal,internal)

RETURNS internal

AS '$libdir/test_parser'

LANGUAGE 'C';


CREATE OR REPLACE FUNCTION testprs_end(internal)

RETURNS void

AS '$libdir/test_parser'

LANGUAGE 'C';


CREATE OR REPLACE FUNCTION testprs_lextype(internal)

RETURNS internal

AS '$libdir/test_parser'

LANGUAGE 'C';

CREATE TEXT SEARCH PARSER testparser(

START = 'testprs_start',

GETTOKEN = 'testprs_getlexeme',

END = 'testprs_end',

LEXTYPES = 'testprs_lextype'

);


CREATE TEXT SEARCH CONFIGURATION testcfg (

PARSER ='testparser'

);


ALTER TEXT SEARCH CONFIGURATION testcfg ADD MAPPING FOR word WITH
english_stem;




END;

  Note: the bold part is total changed for the 8.3.9
version.


"DROP TEXT SEARCH
"
is very useful if you make modification and load the script again.

Install the parser:


  • open a terminal, switch to the directory you just create. For example:
    cd /home/user/Desktop/test_parser
  • type: sudo PATH=/usr/local/pgsql/bin:$PATH USE_PGXS=1 make install
  • switch to user "prostgres", type: /usr/local/pgsql/bin/psql
    -f /usr/local/pgsql/share/contrib/test_parser.sql your-database


    SET

    BEGIN

    CREATE FUNCTION

    CREATE FUNCTION

    CREATE FUNCTION

    CREATE FUNCTION

    DROP TEXT SEARCH CONFIGURATION

    DROP TEXT SEARCH PARSER

    CREATE TEXT SEARCH PARSER

    CREATE TEXT SEARCH CONFIGURATION

    ALTER TEXT SEARCH CONFIGURATION

    COMMIT


  • type: /usr/local/pgsql/bin/psql postgres
  • test the parser:

    postgres=# SELECT
    to_tsvector('testcfg','itemValue');

    to_tsvector

    --------------------

    'item':1 'value':2

    (1 row)


    postgres=# SELECT to_tsvector('testcfg','ItemValue');

    to_tsvector

    --------------------

    'item':1 'value':2

    (1 row)


    postgres=# SELECT to_tsquery('testcfg','itemsValue');

    to_tsquery

    -------------------

    'items' & 'value'

    (1 row)


  
Reference:

[1]
http://www.sai.msu.su/~megera/postgres/gist/tsearch/V2/docs/HOWTO-parser-tsearch2.html


[2]
http://www.pgcon.org/2007/schedule/attachments/12-fts.pdf


[3]
http://en.wikipedia.org/wiki/CamelCase


[4]
http://www.sai.msu.su/~megera/postgres/fts/doc/sql-fts-createmap.html


[5]
http://www.postgresql.org/docs/8.3/static/textsearch-configuration.html


[6]
http://developer.postgresql.org/pgdocs/postgres/sql-createtsconfig.html


[7]
http://www.postgresql.org/docs/7.4/interactive/sql-createfunction.html

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-303070-1-1.html 上篇帖子: Postgresql 插入数据时自动截取一定长度的字符串 下篇帖子: postgresql 连线被拒,请检查主机名称和埠号,并确定 postmaster 可以接受 TCP/IP 连线
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表