這兩個(gè)函數(shù)可以返回查詢文本的相似度和匹配的結(jié)果,使用場(chǎng)景非常足,先把文檔翻譯出來(lái)放在這里,以后再慢慢補(bǔ)充使用過(guò)程中的經(jīng)驗(yàn)和玩法
Scoring
- 用法
zdb.score(tid) RETURNS real - 作用 返回當(dāng)前對(duì)比列的得分
tutorial=#
SELECT zdb.score(ctid), *
FROM products
WHERE products ==> 'sports box'
ORDER BY score desc;
score | id | name | keywords | short_summary | long_description | price |
----------+----+----------+--------------------------------------+--------------------------------+-------------------------------------------------------------------------------------+-------+-
1.00079 | 4 | Box | {wooden,box,"negative space",square} | Just an empty box made of wood | A wooden container that will eventually rot away. Put stuff it in (but not a cat). | 17000 |
0.698622 | 2 | Baseball | {baseball,sports,round} | It's a baseball | Throw it at a person with a big wooden stick and hope they don't hit it | 1249 |
ctid 是pg系統(tǒng)里面的隱藏唯一id列,作為zdb.score的參數(shù)
zdb.score()不可以用于where但可以用于order by
實(shí)現(xiàn)where功能需要使用dsl.min_score()
SELECT * FROM (
SELECT zdb.score(ctid), *
FROM products WHERE products ==> 'sports box' )
x WHERE x.score > 1.0;
- 錯(cuò)誤示范
# SELECT zdb.score(ctid), * FROM products
# WHERE products ==> 'sports box' AND zdb.score(ctid) > 1.0;
ERROR: zdb.score() can only be used as a target entry or as a sort
Highlighting
- 用法
zdb.highlight(tid, fieldname [, json_highlight_descriptor]) RETURNS text[] - 功能 返回帶標(biāo)注的重點(diǎn)結(jié)果,es的默認(rèn)標(biāo)注結(jié)果,第三個(gè)參數(shù)可以自定義highligh標(biāo)注的方法
tutorial=#
SELECT
zdb.score(ctid),
zdb.highlight(ctid, 'long_description'),
long_description
FROM products
WHERE products ==> 'wooden person'
ORDER BY score desc;
score | highlight | long_description
----------+--------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------
0.882384 | {"Throw it at a <em>person</em> with a big <em>wooden</em> stick and hope they don't hit it"} | Throw it at a person with a big wooden stick and hope they don't hit it
0.224636 | {"A <em>wooden</em> container that will eventually rot away. Put stuff it in (but not a cat)."} | A wooden container that will eventually rot away. Put stuff it in (but not a cat).