これはPGroonga 2.0.0以降用のドキュメントです。PGroonga 1.Xを使っているならPGroonga 1.Xのドキュメントを見てください。
<&@*>演算子4.0.5で追加。
まだ実験的な機能です。
<&@*>演算子はテキスト間の距離を計算します。
値が小さいほど意味的に近いテキストと判断できます。
column <&@*> pgroonga_condition(query)
columnは検索対象のカラムです。型はtext型です。
queryは距離を計算するクエリーです。型はtext型です。
pgroonga_condition関数を使います。
pgroonga_condition関数には引数がいくつかありますが、queryのみを指定してご利用ください。
この演算子を使うにはpgroonga_text_semantic_search_ops_v2演算子クラスを指定する必要があります。
例に使うサンプルスキーマとデータは次の通りです。
CREATE TABLE memos (
id integer,
content text
);
INSERT INTO memos VALUES (1, 'PostgreSQL is a RDBMS.');
INSERT INTO memos VALUES (2, 'Groonga is fast full text search engine.');
INSERT INTO memos VALUES (3, 'PGroonga is a PostgreSQL extension that uses Groonga.');
次のようにインデックスを作成します。インデックス作成についてはCREATE INDEX USING pgroongaをご覧ください。
CREATE INDEX pgroonga_index ON memos
USING pgroonga (content pgroonga_text_semantic_search_ops_v2)
WITH (plugins = 'language_model/knn',
model = 'hf:///groonga/all-MiniLM-L6-v2-Q4_K_M-GGUF');
<&@*> 演算子をORDER BYで使うと距離が近い(= 意味が近い)順にソートできます。
SELECT id, content
FROM memos
ORDER BY content <&@*> pgroonga_condition('What is a MySQL alternative?');
-- id | content
-- ----+-------------------------------------------------------
-- 1 | PostgreSQL is a RDBMS.
-- 3 | PGroonga is a PostgreSQL extension that uses Groonga.
-- 2 | Groonga is fast full text search engine.
-- (3 rows)
&@*演算子: セマンティックサーチ