From 8f5791f685cc11a8f405a9006d04b3ebde430c3f Mon Sep 17 00:00:00 2001 From: Abe Tomoaki Date: Mon, 7 Oct 2024 16:36:07 +0900 Subject: [PATCH] query-v2: Add when to use pgroonga_condition() (#389) GitHub: GH-335 --- _po/ja/reference/operators/query-v2.po | 192 +++++++++++++++++++++++-- ja/reference/operators/query-v2.md | 69 ++++++++- reference/operators/query-v2.md | 67 ++++++++- 3 files changed, 309 insertions(+), 19 deletions(-) diff --git a/_po/ja/reference/operators/query-v2.po b/_po/ja/reference/operators/query-v2.po index 8a7874660..1e3236c8d 100644 --- a/_po/ja/reference/operators/query-v2.po +++ b/_po/ja/reference/operators/query-v2.po @@ -58,6 +58,13 @@ msgid "" "pgroonga_full_text_search_condition\n" "column &@~ (query, weights, scorers, index_name)::" "pgroonga_full_text_search_condition_with_scorers\n" +"column &@~ pgroonga_condition(query,\n" +" weights,\n" +" scorers,\n" +" schema_name,\n" +" index_name,\n" +" column_name,\n" +" fuzzy_max_distance_ratio)\n" "```" msgstr "" @@ -89,6 +96,19 @@ msgstr "" msgid "The third signature is available since 2.0.6." msgstr "3つ目の使い方は2.0.6から使えます。" +msgid "" +"The fourth signature is using [`pgroonga_condition` function][condition].\n" +"You can implement the second and third signatures uses with " +"[`pgroonga_condition` function][condition].\n" +"You should use this signature in 3.1.6 and above.\n" +"See [`pgroonga_condition` function][condition] for details." +msgstr "" +"4つ目の使い方は[`pgroonga_condition`関数][condition]を使います。\n" +"[`pgroonga_condition`関数][condition]を使うと2つ目と3つ目の使い方が実現できま" +"す。\n" +"3.1.6以降はこの構文を使ってください。詳しくは[`pgroonga_condition`関数]" +"[condition]をご覧ください。" + msgid "Here is the description of the first signature." msgstr "以下は1つ目の使い方の説明です。" @@ -383,8 +403,7 @@ msgid "" "index. | 2\n" "-- CLI | There is groonga " "command. | 1\n" -"-- (4 rows)\n" -"```" +"-- (4 rows)" msgstr "" "```sql\n" "SELECT *, pgroonga_score(tableoid, ctid) AS score\n" @@ -407,6 +426,54 @@ msgstr "" "PostgreSQLの拡張機能です。 | 2\n" "-- コマンドライン | groongaコマンドがありま" "す。 | 1\n" +"-- (4 rows)" + +msgid "" +"-- Query with pgroonga_condition().\n" +"SELECT *, pgroonga_score(tableoid, ctid) AS score\n" +" FROM memos\n" +" WHERE ARRAY[title, content] &@~\n" +" pgroonga_condition('Groonga OR PostgreSQL',\n" +" weights => ARRAY[5, 1],\n" +" index_name => 'pgroonga_memos_index')\n" +" ORDER BY score DESC;\n" +"-- title | " +"content | score\n" +"-- ------------" +"+------------------------------------------------------------------------" +"+-------\n" +"-- Groonga | Groonga is a fast full text search engine that supports all " +"languages. | 6\n" +"-- PostgreSQL | PostgreSQL is a relational database management " +"system. | 6\n" +"-- PGroonga | PGroonga is a PostgreSQL extension that uses Groonga as " +"index. | 2\n" +"-- CLI | There is groonga " +"command. | 1\n" +"-- (4 rows)\n" +"```" +msgstr "" +"-- pgroonga_condition()を使う\n" +"SELECT *, pgroonga_score(tableoid, ctid) AS score\n" +" FROM memos\n" +" WHERE ARRAY[title, content] &@~\n" +" pgroonga_condition('Groonga OR PostgreSQL',\n" +" weights => ARRAY[5, 1],\n" +" index_name => 'pgroonga_memos_index')\n" +" ORDER BY score DESC;\n" +"-- title | " +"content | score \n" +"-- ----------------" +"+---------------------------------------------------------------------------" +"+-------\n" +"-- Groonga | Groongaは日本語対応の高速な全文検索エンジンで" +"す。 | 6\n" +"-- PostgreSQL | PostgreSQLはリレーショナル・データベース管理システムで" +"す。 | 6\n" +"-- PGroonga | PGroongaはインデックスとしてGroongaを使うための" +"PostgreSQLの拡張機能です。 | 2\n" +"-- コマンドライン | groongaコマンドがありま" +"す。 | 1\n" "-- (4 rows)\n" "```" @@ -442,8 +509,7 @@ msgid "" "languages. | 5\n" "-- PostgreSQL | PostgreSQL is a relational database management " "system. | 5\n" -"-- (2 rows)\n" -"```" +"-- (2 rows)" msgstr "" "```sql\n" "SELECT *, pgroonga_score(tableoid, ctid) AS score\n" @@ -461,6 +527,45 @@ msgstr "" "| 5\n" "-- PostgreSQL | PostgreSQLはリレーショナル・データベース管理システムです。 " "| 5\n" +"-- (2 rows)" + +msgid "" +"-- Query with pgroonga_condition().\n" +"SELECT *, pgroonga_score(tableoid, ctid) AS score\n" +" FROM memos\n" +" WHERE ARRAY[title, content] &@~\n" +" pgroonga_condition('Groonga OR PostgreSQL',\n" +" weights => ARRAY[5, 0],\n" +" index_name => 'pgroonga_memos_index')\n" +" ORDER BY score DESC;\n" +"-- title | " +"content | score \n" +"-- ------------" +"+------------------------------------------------------------------------" +"+-------\n" +"-- Groonga | Groonga is a fast full text search engine that supports all " +"languages. | 5\n" +"-- PostgreSQL | PostgreSQL is a relational database management " +"system. | 5\n" +"-- (2 rows)\n" +"```" +msgstr "" +"-- pgroonga_condition()を使う\n" +"SELECT *, pgroonga_score(tableoid, ctid) AS score\n" +" FROM memos\n" +" WHERE ARRAY[title, content] &@~\n" +" pgroonga_condition('Groonga OR PostgreSQL',\n" +" weights => ARRAY[5, 0],\n" +" index_name => 'pgroonga_memos_index')\n" +" ORDER BY score DESC;\n" +"-- title | content " +"| score \n" +"-- ------------+------------------------------------------------------------" +"+-------\n" +"-- Groonga | Groongaは日本語対応の高速な全文検索エンジンです。 " +"| 5\n" +"-- PostgreSQL | PostgreSQLはリレーショナル・データベース管理システムです。 " +"| 5\n" "-- (2 rows)\n" "```" @@ -495,8 +600,7 @@ msgid "" "index. | 1\n" "-- CLI | There is groonga " "command. | 0.5\n" -"-- (4 rows)\n" -"```" +"-- (4 rows)" msgstr "" "```sql\n" "SELECT *, pgroonga_score(tableoid, ctid) AS score\n" @@ -504,7 +608,61 @@ msgstr "" " WHERE ARRAY[title, content] &@~\n" " ('Groonga OR PostgreSQL',\n" " ARRAY[5, 1],\n" -" 'pgroonga_memos_index')::pgroonga_full_text_search_condition\n" +" ARRAY[NULL, 'scorer_tf_at_most($index, 0.5)'],\n" +" 'pgroonga_memos_index')::" +"pgroonga_full_text_search_condition_with_scorers\n" +" ORDER BY score DESC;\n" +"-- title | " +"content | score \n" +"-- ----------------" +"+---------------------------------------------------------------------------" +"+-------\n" +"-- Groonga | Groongaは日本語対応の高速な全文検索エンジンで" +"す。 | 5.5\n" +"-- PostgreSQL | PostgreSQLはリレーショナル・データベース管理システムで" +"す。 | 5.5\n" +"-- PGroonga | PGroongaはインデックスとしてGroongaを使うための" +"PostgreSQLの拡張機能です。 | 1\n" +"-- コマンドライン | groongaコマンドがありま" +"す。 | 0.5\n" +"-- (4 rows)" + +msgid "" +"-- Query with pgroonga_condition().\n" +"SELECT *, pgroonga_score(tableoid, ctid) AS score\n" +" FROM memos\n" +" WHERE ARRAY[title, content] &@~\n" +" pgroonga_condition('Groonga OR PostgreSQL',\n" +" weights => ARRAY[5, 1],\n" +" scorers => ARRAY[NULL, 'scorer_tf_at_most($index, " +"0.5)'],\n" +" index_name => 'pgroonga_memos_index')\n" +" ORDER BY score DESC;\n" +"-- title | " +"content | score \n" +"-- ------------" +"+------------------------------------------------------------------------" +"+-------\n" +"-- Groonga | Groonga is a fast full text search engine that supports all " +"languages. | 5.5\n" +"-- PostgreSQL | PostgreSQL is a relational database management " +"system. | 5.5\n" +"-- PGroonga | PGroonga is a PostgreSQL extension that uses Groonga as " +"index. | 1\n" +"-- CLI | There is groonga " +"command. | 0.5\n" +"-- (4 rows)\n" +"```" +msgstr "" +"-- pgroonga_condition()を使う\n" +"SELECT *, pgroonga_score(tableoid, ctid) AS score\n" +" FROM memos\n" +" WHERE ARRAY[title, content] &@~\n" +" pgroonga_condition('Groonga OR PostgreSQL',\n" +" weights => ARRAY[5, 1],\n" +" scorers => ARRAY[NULL, 'scorer_tf_at_most($index, " +"0.5)'],\n" +" index_name => 'pgroonga_memos_index')\n" " ORDER BY score DESC;\n" "-- title | " "content | score \n" @@ -547,6 +705,9 @@ msgstr "## 参考" msgid " * [`&@` operator][match-v2]: Full text search by a keyword" msgstr " * [`&@`演算子][match-v2]:キーワード1つでの全文検索" +msgid " * [`pgroonga_condition` function][condition]" +msgstr "" + msgid " * [Groonga's query syntax][groonga-query-syntax]" msgstr " * [Groongaのクエリーの構文][groonga-query-syntax]" @@ -556,18 +717,21 @@ msgstr "" "[wikipedia-keyword-stuffing]:https://ja.wikipedia.org/wiki/" "%E3%82%AD%E3%83%BC%E3%83%AF%E3%83%BC%E3%83%89%E3%82%B9%E3%82%BF%E3%83%83%E3%83%95%E3%82%A3%E3%83%B3%E3%82%B0" +msgid "" +"[groonga-query-syntax]:http://groonga.org/docs/reference/grn_expr/" +"query_syntax.html" +msgstr "" +"[groonga-query-syntax]:http://groonga.org/ja/docs/reference/grn_expr/" +"query_syntax.html" + msgid "[groonga-scorer]:http://groonga.org/docs/reference/scorer.html" msgstr "[groonga-scorer]:http://groonga.org/ja/docs/reference/scorer.html" -msgid "[score]:../functions/pgroonga-score.html" +msgid "[condition]:../functions/pgroonga-condition.html" msgstr "" -msgid "[match-v2]:match-v2.html" +msgid "[score]:../functions/pgroonga-score.html" msgstr "" -msgid "" -"[groonga-query-syntax]:http://groonga.org/docs/reference/grn_expr/" -"query_syntax.html" +msgid "[match-v2]:match-v2.html" msgstr "" -"[groonga-query-syntax]:http://groonga.org/ja/docs/reference/grn_expr/" -"query_syntax.html" diff --git a/ja/reference/operators/query-v2.md b/ja/reference/operators/query-v2.md index efd78b463..8fc6392c3 100644 --- a/ja/reference/operators/query-v2.md +++ b/ja/reference/operators/query-v2.md @@ -23,6 +23,13 @@ upper_level: ../ column &@~ query column &@~ (query, weights, index_name)::pgroonga_full_text_search_condition column &@~ (query, weights, scorers, index_name)::pgroonga_full_text_search_condition_with_scorers +column &@~ pgroonga_condition(query, + weights, + scorers, + schema_name, + index_name, + column_name, + fuzzy_max_distance_ratio) ``` 1つ目の使い方は他の使い方よりもシンプルです。多くの場合は1つ目の使い方で十分です。 @@ -35,6 +42,10 @@ column &@~ (query, weights, scorers, index_name)::pgroonga_full_text_search_cond 3つ目の使い方は2.0.6から使えます。 +4つ目の使い方は[`pgroonga_condition`関数][condition]を使います。 +[`pgroonga_condition`関数][condition]を使うと2つ目と3つ目の使い方が実現できます。 +3.1.6以降はこの構文を使ってください。詳しくは[`pgroonga_condition`関数][condition]をご覧ください。 + 以下は1つ目の使い方の説明です。 ```sql @@ -204,6 +215,22 @@ SELECT *, pgroonga_score(tableoid, ctid) AS score -- PGroonga | PGroongaはインデックスとしてGroongaを使うためのPostgreSQLの拡張機能です。 | 2 -- コマンドライン | groongaコマンドがあります。 | 1 -- (4 rows) + +-- pgroonga_condition()を使う +SELECT *, pgroonga_score(tableoid, ctid) AS score + FROM memos + WHERE ARRAY[title, content] &@~ + pgroonga_condition('Groonga OR PostgreSQL', + weights => ARRAY[5, 1], + index_name => 'pgroonga_memos_index') + ORDER BY score DESC; +-- title | content | score +-- ----------------+---------------------------------------------------------------------------+------- +-- Groonga | Groongaは日本語対応の高速な全文検索エンジンです。 | 6 +-- PostgreSQL | PostgreSQLはリレーショナル・データベース管理システムです。 | 6 +-- PGroonga | PGroongaはインデックスとしてGroongaを使うためのPostgreSQLの拡張機能です。 | 2 +-- コマンドライン | groongaコマンドがあります。 | 1 +-- (4 rows) ``` `title`カラムに「`Groonga`」または「`PostgreSQL`」があるレコードの方が`content`カラムに「`Groonga`」または「`PostgreSQL`」がある方がスコアーが高いことを確認できます。 @@ -223,6 +250,20 @@ SELECT *, pgroonga_score(tableoid, ctid) AS score -- Groonga | Groongaは日本語対応の高速な全文検索エンジンです。 | 5 -- PostgreSQL | PostgreSQLはリレーショナル・データベース管理システムです。 | 5 -- (2 rows) + +-- pgroonga_condition()を使う +SELECT *, pgroonga_score(tableoid, ctid) AS score + FROM memos + WHERE ARRAY[title, content] &@~ + pgroonga_condition('Groonga OR PostgreSQL', + weights => ARRAY[5, 0], + index_name => 'pgroonga_memos_index') + ORDER BY score DESC; +-- title | content | score +-- ------------+------------------------------------------------------------+------- +-- Groonga | Groongaは日本語対応の高速な全文検索エンジンです。 | 5 +-- PostgreSQL | PostgreSQLはリレーショナル・データベース管理システムです。 | 5 +-- (2 rows) ``` スコアーの計算方法をカスタマイズできます。たとえば、`content`カラムのスコアーを最大で`0.5`に制限できます。 @@ -233,7 +274,25 @@ SELECT *, pgroonga_score(tableoid, ctid) AS score WHERE ARRAY[title, content] &@~ ('Groonga OR PostgreSQL', ARRAY[5, 1], - 'pgroonga_memos_index')::pgroonga_full_text_search_condition + ARRAY[NULL, 'scorer_tf_at_most($index, 0.5)'], + 'pgroonga_memos_index')::pgroonga_full_text_search_condition_with_scorers + ORDER BY score DESC; +-- title | content | score +-- ----------------+---------------------------------------------------------------------------+------- +-- Groonga | Groongaは日本語対応の高速な全文検索エンジンです。 | 5.5 +-- PostgreSQL | PostgreSQLはリレーショナル・データベース管理システムです。 | 5.5 +-- PGroonga | PGroongaはインデックスとしてGroongaを使うためのPostgreSQLの拡張機能です。 | 1 +-- コマンドライン | groongaコマンドがあります。 | 0.5 +-- (4 rows) + +-- pgroonga_condition()を使う +SELECT *, pgroonga_score(tableoid, ctid) AS score + FROM memos + WHERE ARRAY[title, content] &@~ + pgroonga_condition('Groonga OR PostgreSQL', + weights => ARRAY[5, 1], + scorers => ARRAY[NULL, 'scorer_tf_at_most($index, 0.5)'], + index_name => 'pgroonga_memos_index') ORDER BY score DESC; -- title | content | score -- ----------------+---------------------------------------------------------------------------+------- @@ -254,14 +313,18 @@ SELECT *, pgroonga_score(tableoid, ctid) AS score * [`&@`演算子][match-v2]:キーワード1つでの全文検索 + * [`pgroonga_condition`関数][condition] + * [Groongaのクエリーの構文][groonga-query-syntax] [wikipedia-keyword-stuffing]:https://ja.wikipedia.org/wiki/%E3%82%AD%E3%83%BC%E3%83%AF%E3%83%BC%E3%83%89%E3%82%B9%E3%82%BF%E3%83%83%E3%83%95%E3%82%A3%E3%83%B3%E3%82%B0 +[groonga-query-syntax]:http://groonga.org/ja/docs/reference/grn_expr/query_syntax.html + [groonga-scorer]:http://groonga.org/ja/docs/reference/scorer.html +[condition]:../functions/pgroonga-condition.html + [score]:../functions/pgroonga-score.html [match-v2]:match-v2.html - -[groonga-query-syntax]:http://groonga.org/ja/docs/reference/grn_expr/query_syntax.html diff --git a/reference/operators/query-v2.md b/reference/operators/query-v2.md index 14d002694..964640cac 100644 --- a/reference/operators/query-v2.md +++ b/reference/operators/query-v2.md @@ -23,6 +23,13 @@ There are three signatures: column &@~ query column &@~ (query, weights, index_name)::pgroonga_full_text_search_condition column &@~ (query, weights, scorers, index_name)::pgroonga_full_text_search_condition_with_scorers +column &@~ pgroonga_condition(query, + weights, + scorers, + schema_name, + index_name, + column_name, + fuzzy_max_distance_ratio) ``` The first signature is simpler than others. The first signature is enough for most cases. @@ -35,6 +42,11 @@ The third signature is useful to optimize more search score. For example, you ca The third signature is available since 2.0.6. +The fourth signature is using [`pgroonga_condition` function][condition]. +You can implement the second and third signatures uses with [`pgroonga_condition` function][condition]. +You should use this signature in 3.1.6 and above. +See [`pgroonga_condition` function][condition] for details. + Here is the description of the first signature. ```sql @@ -204,6 +216,22 @@ SELECT *, pgroonga_score(tableoid, ctid) AS score -- PGroonga | PGroonga is a PostgreSQL extension that uses Groonga as index. | 2 -- CLI | There is groonga command. | 1 -- (4 rows) + +-- Query with pgroonga_condition(). +SELECT *, pgroonga_score(tableoid, ctid) AS score + FROM memos + WHERE ARRAY[title, content] &@~ + pgroonga_condition('Groonga OR PostgreSQL', + weights => ARRAY[5, 1], + index_name => 'pgroonga_memos_index') + ORDER BY score DESC; +-- title | content | score +-- ------------+------------------------------------------------------------------------+------- +-- Groonga | Groonga is a fast full text search engine that supports all languages. | 6 +-- PostgreSQL | PostgreSQL is a relational database management system. | 6 +-- PGroonga | PGroonga is a PostgreSQL extension that uses Groonga as index. | 2 +-- CLI | There is groonga command. | 1 +-- (4 rows) ``` You can confirm that the record which has "`Groonga`" or "`PostgreSQL`" in `title` column has more high score than "`Groonga`" or "`PostgreSQL`" in `content` column. @@ -223,6 +251,20 @@ SELECT *, pgroonga_score(tableoid, ctid) AS score -- Groonga | Groonga is a fast full text search engine that supports all languages. | 5 -- PostgreSQL | PostgreSQL is a relational database management system. | 5 -- (2 rows) + +-- Query with pgroonga_condition(). +SELECT *, pgroonga_score(tableoid, ctid) AS score + FROM memos + WHERE ARRAY[title, content] &@~ + pgroonga_condition('Groonga OR PostgreSQL', + weights => ARRAY[5, 0], + index_name => 'pgroonga_memos_index') + ORDER BY score DESC; +-- title | content | score +-- ------------+------------------------------------------------------------------------+------- +-- Groonga | Groonga is a fast full text search engine that supports all languages. | 5 +-- PostgreSQL | PostgreSQL is a relational database management system. | 5 +-- (2 rows) ``` You can customize how to compute score. For example, you can limit the score of `content` column to `0.5`. @@ -243,6 +285,23 @@ SELECT *, pgroonga_score(tableoid, ctid) AS score -- PGroonga | PGroonga is a PostgreSQL extension that uses Groonga as index. | 1 -- CLI | There is groonga command. | 0.5 -- (4 rows) + +-- Query with pgroonga_condition(). +SELECT *, pgroonga_score(tableoid, ctid) AS score + FROM memos + WHERE ARRAY[title, content] &@~ + pgroonga_condition('Groonga OR PostgreSQL', + weights => ARRAY[5, 1], + scorers => ARRAY[NULL, 'scorer_tf_at_most($index, 0.5)'], + index_name => 'pgroonga_memos_index') + ORDER BY score DESC; +-- title | content | score +-- ------------+------------------------------------------------------------------------+------- +-- Groonga | Groonga is a fast full text search engine that supports all languages. | 5.5 +-- PostgreSQL | PostgreSQL is a relational database management system. | 5.5 +-- PGroonga | PGroonga is a PostgreSQL extension that uses Groonga as index. | 1 +-- CLI | There is groonga command. | 0.5 +-- (4 rows) ``` See [Groonga document][groonga-query-syntax] for query syntax details. @@ -255,14 +314,18 @@ You can't use `COLUMN_NAME:^VALUE` for prefix search. You need to use `VALUE*` f * [`&@` operator][match-v2]: Full text search by a keyword + * [`pgroonga_condition` function][condition] + * [Groonga's query syntax][groonga-query-syntax] [wikipedia-keyword-stuffing]:https://en.wikipedia.org/wiki/Keyword_stuffing +[groonga-query-syntax]:http://groonga.org/docs/reference/grn_expr/query_syntax.html + [groonga-scorer]:http://groonga.org/docs/reference/scorer.html +[condition]:../functions/pgroonga-condition.html + [score]:../functions/pgroonga-score.html [match-v2]:match-v2.html - -[groonga-query-syntax]:http://groonga.org/docs/reference/grn_expr/query_syntax.html