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

Why is PostgreSQL stripping HTML entities in ts_headline()?

我正在编写一个全文搜索功能的原型,它将 return 在搜索结果中找到文档' "headlines"。这是 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 标签的所有内容都被删除并替换为单个 space 字符(注意 [=15= 周围的双 space ]).

我没有在文档中找到任何地方表明 Postgres 假设输入文本是 HTML 并且用户希望剥离标签。 api 允许从默认的 <b></b> 覆盖 StartSelStopSel,所以我认为它是为了更一般的用途-案例.

文档中是否有我遗漏的某些设置或评论?

<b></b> 被识别为 tag 标记。默认情况下,它们是 忽略。您需要修改现有配置或创建新配置:

=# 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 )