加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

为什么PostgreSQL在ts_headline()中剥离HTML实体?

发布时间:2020-12-13 18:10:28 所属栏目:百科 来源:网络整理
导读:我正在编写一个全文搜索功能的原型,它将在搜索结果中返回找到的文档的“标题”.这是 Postgres docs的一个略微修改的例子: SELECT ts_headline('english','The most common type of search is to find all documents containing given query terms band/b re
我正在编写一个全文搜索功能的原型,它将在搜索结果中返回找到的文档的“标题”.这是 Postgres docs的一个略微修改的例子:
SELECT ts_headline('english','The most common type of search is to find all documents containing given query terms <b>and</b> return them in <order> of their similarity to the query.',to_tsquery('query & similarity'),'StartSel = XXX,StopSel = YYY');

我期望的是什么样的

"documents containing given XXXqueryYYY terms <b>and</b> return them in <order> of their XXXsimilarityYYY to the XXXqueryYYY."

我得到的是

"documents containing given XXXqueryYYY terms  and  return them in   of their XXXsimilarityYYY to the XXXqueryYYY."

看起来像HTML标签一样远程看起来的所有内容都被剥离并替换为单个空格字符(请注意和之间的双重空格).

我没有在文档中找到任何声明Postgres假设输入文本是HTML并且用户希望剥离标签的地方. api允许从默认的< b>覆盖StartSel和StopSel.和< / b>,所以我认为这是为了提供更一般的用例.

在我缺少的文档中是否有一些设置或注释?

&LT b取代;和< / b>被识别为标记令牌.默认情况下他们是
忽略.您需要修改现有配置或创建新配置:
=# CREATE TEXT SEARCH CONFIGURATION english_tag (COPY = english);
=# alter text search configuration english_tag
   add mapping for tag with simple;

然后不跳过标签:

=# select * from ts_debug('english_tag','query <b>test</b>');
   alias   |   description   | token |  dictionaries  |  dictionary  | lexemes
-----------+-----------------+-------+----------------+--------------+---------
 asciiword | Word,all ASCII | query | {english_stem} | english_stem | {queri}
 blank     | Space symbols   |       | {}             | (null)       | (null)
 tag       | XML tag         | <b>   | {simple}       | simple       | {<b>}
 asciiword | Word,all ASCII | test  | {english_stem} | english_stem | {test}
 tag       | XML tag         | </b>  | {simple}       | simple       | {</b>}

但即使在这种情况下,ts_headline也会跳过标签.因为它是
hardcoded:

#define HLIDREPLACE(x)  ( (x)==TAG_T )

当然有一种解决方法.可以创建自己的文本搜索解析器扩展. GitHub的例子.并改变

#define HLIDREPLACE(x)  ( (x)==TAG_T )

#define HLIDREPLACE(x)  ( false )

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读