Skip to content

Commit 7c63714

Browse files
committed
improve
1 parent 0758544 commit 7c63714

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

201802/20180202_01.md

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -797,11 +797,25 @@ postgres=# select set_limit(0.9);
797797
例子
798798

799799
```
800-
create or replace function get_res(text, int8) returns setof record as $$
800+
create or replace function get_res(
801+
text, -- 要按相似搜的文本
802+
int8, -- 限制返回多少条
803+
float4 default 0.3, -- 相似度阈值,低于这个值不再搜搜
804+
float4 default 0.1 -- 相似度递减步长,直至阈值
805+
) returns setof record as $$
801806
declare
802807
lim float4 := 1;
803808
begin
804-
809+
-- 判定
810+
if not ($3 <= 1 and $3 > 0) then
811+
raise notice '$3 must >0 and <=1';
812+
return;
813+
end if;
814+
815+
if not ($4 > 0 and $4 < 1) then
816+
raise notice '$4 must >0 and <=1';
817+
return;
818+
end if;
805819
loop
806820
-- 设置相似度阈值
807821
perform set_limit(lim);
@@ -817,18 +831,18 @@ begin
817831
818832
-- 否则继续,降低阈值
819833
-- 当阈值小于0.3时,不再降阈值搜索,认为没有相似。
820-
if lim < 0.3 then
834+
if lim < $3 then
821835
return;
822836
else
823-
lim := lim - 0.1;
837+
lim := lim - $4;
824838
end if;
825839
end loop;
826840
end;
827841
$$ language plpgsql strict;
828842
```
829843

830844
```
831-
select * from get_res('输入搜索文本', 输入限制条数) as t(sml float4, id int, info text);
845+
select * from get_res('输入搜索文本', 输入限制条数, 输入阈值, 输入步长) as t(sml float4, id int, info text);
832846
```
833847

834848
使用这个UDF搜索,既快又准。

0 commit comments

Comments
 (0)