我正在编写一个全文搜索功能的原型,它将在搜索结果中返回找到的文档的"标题"。下面是一个来自Postgres文档的稍微修改过的例子:
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标签的东西都被剥离并替换为单个空格字符(注意and
周围的双空格)。
我没有在文档中找到任何地方声明Postgres假设输入文本是HTML,并且用户希望剥离标签。该api允许从默认的<b>
和</b>
重写StartSel
和StopSel
,所以我认为它是为了服务于更一般的用例。
文档中是否缺少了一些设置或注释?
<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也会跳过标签。因为它是硬编码:
#define HLIDREPLACE(x) ( (x)==TAG_T )
当然有一个变通方法。可以创建自己的文本搜索解析器扩展。GitHub上的例子。和改变
#define HLIDREPLACE(x) ( (x)==TAG_T )
#define HLIDREPLACE(x) ( false )