smlar

高效的相似度搜索函数

概览

扩展包名版本分类许可证语言
smlar1.0RAGPostgreSQLC
ID扩展名BinLibLoadCreateTrustReloc模式
1850smlar-
相关扩展pg_similarity fuzzystrmatch pg_trgm intarray vector pg_bigm unaccent vchord

fix pg18 break issue by https://github.com/Vonng/smlar

版本

类型仓库版本PG 大版本包名依赖
EXTPIGSTY1.01817161514smlar-
RPMPIGSTY1.01817161514smlar_$v-
DEBPIGSTY1.01817161514postgresql-$v-smlar-
OS / PGPG18PG17PG16PG15PG14
el8.x86_64
el8.aarch64
el9.x86_64
el9.aarch64
el10.x86_64
el10.aarch64
d12.x86_64
d12.aarch64
d13.x86_64
d13.aarch64
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
u22.x86_64
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
u22.aarch64
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
u24.x86_64
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
u24.aarch64
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0

构建

您可以使用 pig build 命令构建 smlar 扩展的 RPM / DEB 包:

pig build pkg smlar         # 构建 RPM / DEB 包

安装

您可以直接安装 smlar 扩展包的预置二进制包,首先确保 PGDGPIGSTY 仓库已经添加并启用:

pig repo add pgsql -u          # 添加仓库并更新缓存

使用 pig 或者是 apt/yum/dnf 安装扩展:

pig install smlar;          # 当前活跃 PG 版本安装
pig ext install -y smlar -v 18  # PG 18
pig ext install -y smlar -v 17  # PG 17
pig ext install -y smlar -v 16  # PG 16
pig ext install -y smlar -v 15  # PG 15
pig ext install -y smlar -v 14  # PG 14
dnf install -y smlar_18       # PG 18
dnf install -y smlar_17       # PG 17
dnf install -y smlar_16       # PG 16
dnf install -y smlar_15       # PG 15
dnf install -y smlar_14       # PG 14
apt install -y postgresql-18-smlar   # PG 18
apt install -y postgresql-17-smlar   # PG 17
apt install -y postgresql-16-smlar   # PG 16
apt install -y postgresql-15-smlar   # PG 15
apt install -y postgresql-14-smlar   # PG 14

创建扩展

CREATE EXTENSION smlar;

用法

smlar:PostgreSQL 数组的高效相似度搜索。 来源:README

smlar 扩展提供 PostgreSQL 数组上的高效相似度搜索,支持可配置的相似度公式、GiST 和 GIN 索引,以及 TF/IDF 加权。


函数

float4 smlar(anyarray, anyarray)

计算两个数组的相似度。数组应为相同类型。

float4 smlar(anyarray, anyarray, bool useIntersect)

计算两个复合类型数组的相似度。复合类型格式如下:

CREATE TYPE type_name AS (element_name anytype, weight_name FLOAT4);

useIntersect 选项指定分母中仅使用交集元素。

float4 smlar(anyarray a, anyarray b, text formula)

通过给定公式计算两个数组的相似度。公式中的预定义变量:

  • N.i – 两个数组中的公共元素数量(交集)
  • N.a – 第一个数组中的唯一元素数量
  • N.b – 第二个数组中的唯一元素数量

示例:

SELECT smlar('{1,4,6}'::int[], '{5,4,6}');
SELECT smlar('{1,4,6}'::int[], '{5,4,6}', 'N.i / sqrt(N.a * N.b)');
-- 这两个调用是等价的。
anyarray % anyarray

如果数组的相似度大于阈值限制则返回 true。

text[] tsvector2textarray(tsvector)

将 tsvector 类型转换为文本数组。

anyarray array_unique(anyarray)

排序并去重数组。

float4 inarray(anyarray, anyelement)

如果第二个参数不存在于第一个参数中返回零,否则返回 1.0。

float4 inarray(anyarray, anyelement, float4, float4)

如果第二个参数不存在于第一个参数中返回第四个参数,否则返回第三个参数。


GUC 配置变量

smlar.threshold  FLOAT

相似度低于阈值的数组不被 % 运算视为相似。

smlar.persistent_cache  BOOL

全局统计缓存存储在事务无关的内存中。

smlar.type  STRING

相似度公式类型:cosine(默认)、tfidfoverlap

smlar.stattable  STRING

存储集合级统计数据的表名。表应定义为:

CREATE TABLE table_name (
    value   data_type UNIQUE,
    ndoc    int4 (or bigint)  NOT NULL CHECK (ndoc > 0)
);

值为 null 的行表示文档总数。仅用于 smlar.type = 'tfidf'

smlar.tf_method  STRING

词频计算方法。取值:

  • "n" – 简单计数(默认)
  • "log" – 1 + log(n)
  • "const" – TF 等于 1

仅用于 smlar.type = 'tfidf'

smlar.idf_plus_one  BOOL

如果为 false(默认),idf 计算为 log(d/df)。如果为 true,计算为 log(1+d/df)。仅用于 smlar.type = 'tfidf'

强烈建议在 postgresql.conf 中添加:

smlar.threshold = 0.6  # 或其他 > 0 且 < 1 的值

GiST/GIN 索引支持

%&& 操作支持多种数组类型的 GiST 和 GIN 索引:

数组类型GIN 操作符类GiST 操作符类
bit[]_bit_sml_ops
bytea[]_bytea_sml_ops_bytea_sml_ops
char[]_char_sml_ops_char_sml_ops
cidr[]_cidr_sml_ops_cidr_sml_ops
date[]_date_sml_ops_date_sml_ops
float4[]_float4_sml_ops_float4_sml_ops
float8[]_float8_sml_ops_float8_sml_ops
inet[]_inet_sml_ops_inet_sml_ops
int2[]_int2_sml_ops_int2_sml_ops
int4[]_int4_sml_ops_int4_sml_ops
int8[]_int8_sml_ops_int8_sml_ops
interval[]_interval_sml_ops_interval_sml_ops
macaddr[]_macaddr_sml_ops_macaddr_sml_ops
money[]_money_sml_ops
numeric[]_numeric_sml_ops_numeric_sml_ops
oid[]_oid_sml_ops_oid_sml_ops
text[]_text_sml_ops_text_sml_ops
time[]_time_sml_ops_time_sml_ops
timestamp[]_timestamp_sml_ops_timestamp_sml_ops
timestamptz[]_timestamptz_sml_ops_timestamptz_sml_ops
timetz[]_timetz_sml_ops_timetz_sml_ops
varbit[]_varbit_sml_ops
varchar[]_varchar_sml_ops_varchar_sml_ops

最后修改 2026-03-14: update extension metadata (953cbd0)