[Groonga-commit] pgroonga/pgroonga at 478b3ba [master] Support similar search by &~?

Back to archive index

Kouhei Sutou null+****@clear*****
Tue Mar 29 00:06:02 JST 2016


Kouhei Sutou	2016-03-29 00:06:02 +0900 (Tue, 29 Mar 2016)

  New Revision: 478b3bacd7664951f447889db7a82f5ec9a04993
  https://github.com/pgroonga/pgroonga/commit/478b3bacd7664951f447889db7a82f5ec9a04993

  Message:
    Support similar search by &~?
    
    It doesn't work in sequential scan.

  Added files:
    expected/full-text-search/text/single/similar-v2/bitmapscan.out
    expected/full-text-search/text/single/similar-v2/indexscan.out
    expected/full-text-search/text/single/similar-v2/seqscan.out
    sql/full-text-search/text/single/similar-v2/bitmapscan.sql
    sql/full-text-search/text/single/similar-v2/indexscan.sql
    sql/full-text-search/text/single/similar-v2/seqscan.sql
  Modified files:
    pgroonga.sql
    src/pgroonga.c
    src/pgroonga.h

  Added: expected/full-text-search/text/single/similar-v2/bitmapscan.out (+21 -0) 100644
===================================================================
--- /dev/null
+++ expected/full-text-search/text/single/similar-v2/bitmapscan.out    2016-03-29 00:06:02 +0900 (c55ff11)
@@ -0,0 +1,21 @@
+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 grnindex ON memos
+ USING pgroonga (content pgroonga.text_full_text_search_ops_v2);
+SET enable_seqscan = off;
+SET enable_indexscan = off;
+SET enable_bitmapscan = on;
+SELECT id, content
+  FROM memos
+ WHERE content &~? 'Mroonga is a MySQL plugin that uses Groonga.';
+ id |                        content                        
+----+-------------------------------------------------------
+  3 | PGroonga is a PostgreSQL extension that uses Groonga.
+(1 row)
+
+DROP TABLE memos;

  Added: expected/full-text-search/text/single/similar-v2/indexscan.out (+21 -0) 100644
===================================================================
--- /dev/null
+++ expected/full-text-search/text/single/similar-v2/indexscan.out    2016-03-29 00:06:02 +0900 (f594dd5)
@@ -0,0 +1,21 @@
+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 grnindex ON memos
+ USING pgroonga (content pgroonga.text_full_text_search_ops_v2);
+SET enable_seqscan = off;
+SET enable_indexscan = on;
+SET enable_bitmapscan = off;
+SELECT id, content
+  FROM memos
+ WHERE content &~? 'Mroonga is a MySQL plugin that uses Groonga.';
+ id |                        content                        
+----+-------------------------------------------------------
+  3 | PGroonga is a PostgreSQL extension that uses Groonga.
+(1 row)
+
+DROP TABLE memos;

  Added: expected/full-text-search/text/single/similar-v2/seqscan.out (+17 -0) 100644
===================================================================
--- /dev/null
+++ expected/full-text-search/text/single/similar-v2/seqscan.out    2016-03-29 00:06:02 +0900 (ecedad9)
@@ -0,0 +1,17 @@
+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 grnindex ON memos
+ USING pgroonga (content pgroonga.text_full_text_search_ops_v2);
+SET enable_seqscan = on;
+SET enable_indexscan = off;
+SET enable_bitmapscan = off;
+SELECT id, content
+  FROM memos
+ WHERE content &~? 'Mroonga is a MySQL plugin that uses Groonga.';
+ERROR:  pgroonga: operator &~? is available only in index scan
+DROP TABLE memos;

  Modified: pgroonga.sql (+18 -4)
===================================================================
--- pgroonga.sql    2016-03-28 23:11:11 +0900 (3207764)
+++ pgroonga.sql    2016-03-29 00:06:02 +0900 (3bda70a)
@@ -206,7 +206,7 @@ CREATE FUNCTION pgroonga.options(internal)
 DELETE FROM pg_catalog.pg_am WHERE amname = 'pgroonga';
 INSERT INTO pg_catalog.pg_am VALUES(
 	'pgroonga',	-- amname
-	16,		-- amstrategies
+	17,		-- amstrategies
 	0,		-- amsupport
 	true,		-- amcanorder
 	true,		-- amcanorderbyop
@@ -401,6 +401,19 @@ CREATE OPERATOR &? (
 	RIGHTARG = text
 );
 
+CREATE FUNCTION pgroonga.similar_text(text, text)
+	RETURNS bool
+	AS 'MODULE_PATHNAME', 'pgroonga_similar_text'
+	LANGUAGE C
+	IMMUTABLE
+	STRICT;
+
+CREATE OPERATOR &~? (
+	PROCEDURE = pgroonga.similar_text,
+	LEFTARG = text,
+	RIGHTARG = text
+);
+
 CREATE FUNCTION pgroonga.script_text(text, text)
 	RETURNS bool
 	AS 'MODULE_PATHNAME', 'pgroonga_script_text'
@@ -446,6 +459,7 @@ CREATE OPERATOR CLASS pgroonga.text_full_text_search_ops_v2 FOR TYPE text
 		OPERATOR 7 pg_catalog.~~*,
 		OPERATOR 12 &@,
 		OPERATOR 13 &?,
-		OPERATOR 14 &`,
-		OPERATOR 15 &@> (text, text[]),
-		OPERATOR 16 &?> (text, text[]);
+		OPERATOR 14 &~?,
+		OPERATOR 15 &`,
+		OPERATOR 16 &@> (text, text[]),
+		OPERATOR 17 &?> (text, text[]);

  Added: sql/full-text-search/text/single/similar-v2/bitmapscan.sql (+21 -0) 100644
===================================================================
--- /dev/null
+++ sql/full-text-search/text/single/similar-v2/bitmapscan.sql    2016-03-29 00:06:02 +0900 (0bf8463)
@@ -0,0 +1,21 @@
+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 grnindex ON memos
+ USING pgroonga (content pgroonga.text_full_text_search_ops_v2);
+
+SET enable_seqscan = off;
+SET enable_indexscan = off;
+SET enable_bitmapscan = on;
+
+SELECT id, content
+  FROM memos
+ WHERE content &~? 'Mroonga is a MySQL plugin that uses Groonga.';
+
+DROP TABLE memos;

  Added: sql/full-text-search/text/single/similar-v2/indexscan.sql (+21 -0) 100644
===================================================================
--- /dev/null
+++ sql/full-text-search/text/single/similar-v2/indexscan.sql    2016-03-29 00:06:02 +0900 (9c8c9ff)
@@ -0,0 +1,21 @@
+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 grnindex ON memos
+ USING pgroonga (content pgroonga.text_full_text_search_ops_v2);
+
+SET enable_seqscan = off;
+SET enable_indexscan = on;
+SET enable_bitmapscan = off;
+
+SELECT id, content
+  FROM memos
+ WHERE content &~? 'Mroonga is a MySQL plugin that uses Groonga.';
+
+DROP TABLE memos;

  Added: sql/full-text-search/text/single/similar-v2/seqscan.sql (+21 -0) 100644
===================================================================
--- /dev/null
+++ sql/full-text-search/text/single/similar-v2/seqscan.sql    2016-03-29 00:06:02 +0900 (f44261d)
@@ -0,0 +1,21 @@
+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 grnindex ON memos
+ USING pgroonga (content pgroonga.text_full_text_search_ops_v2);
+
+SET enable_seqscan = on;
+SET enable_indexscan = off;
+SET enable_bitmapscan = off;
+
+SELECT id, content
+  FROM memos
+ WHERE content &~? 'Mroonga is a MySQL plugin that uses Groonga.';
+
+DROP TABLE memos;

  Modified: src/pgroonga.c (+31 -0)
===================================================================
--- src/pgroonga.c    2016-03-28 23:11:11 +0900 (913c6c2)
+++ src/pgroonga.c    2016-03-29 00:06:02 +0900 (1aebaae)
@@ -142,6 +142,7 @@ PG_FUNCTION_INFO_V1(pgroonga_match_regexp_varchar);
 /* v2 */
 PG_FUNCTION_INFO_V1(pgroonga_match_text);
 PG_FUNCTION_INFO_V1(pgroonga_query_text);
+PG_FUNCTION_INFO_V1(pgroonga_similar_text);
 PG_FUNCTION_INFO_V1(pgroonga_script_text);
 PG_FUNCTION_INFO_V1(pgroonga_match_contain_text);
 PG_FUNCTION_INFO_V1(pgroonga_query_contain_text);
@@ -1550,6 +1551,32 @@ pgroonga_query_text(PG_FUNCTION_ARGS)
 	PG_RETURN_BOOL(matched);
 }
 
+/**
+ * pgroonga.similar_text(target text, document text) : bool
+ */
+Datum
+pgroonga_similar_text(PG_FUNCTION_ARGS)
+{
+#ifdef NOT_USED
+	text *target = PG_GETARG_TEXT_PP(0);
+	text *document = PG_GETARG_TEXT_PP(1);
+	bool matched = false;
+
+	matched = pgroonga_similar_raw(VARDATA_ANY(target),
+								   VARSIZE_ANY_EXHDR(target),
+								   VARDATA_ANY(document),
+								   VARSIZE_ANY_EXHDR(document));
+
+	PG_RETURN_BOOL(matched);
+#endif
+
+	ereport(ERROR,
+			(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+			 errmsg("pgroonga: operator &~? is available only in index scan")));
+
+	PG_RETURN_BOOL(false);
+}
+
 static grn_bool
 pgroonga_script_raw(const char *target, unsigned int targetSize,
 					const char *script, unsigned int scriptSize)
@@ -2324,6 +2351,10 @@ PGrnSearchBuildCondition(IndexScanDesc scan,
 		break;
 	case PGrnQueryStrategyNumber:
 	case PGrnQueryStrategyV2Number:
+		break;
+	case PGrnSimilarStrategyV2Number:
+		operator = GRN_OP_SIMILAR;
+		break;
 	case PGrnScriptStrategyV2Number:
 		break;
 	case PGrnRegexpStrategyNumber:

  Modified: src/pgroonga.h (+5 -3)
===================================================================
--- src/pgroonga.h    2016-03-28 23:11:11 +0900 (a6ee001)
+++ src/pgroonga.h    2016-03-29 00:06:02 +0900 (0e0306f)
@@ -26,9 +26,10 @@
 
 #define PGrnMatchStrategyV2Number		12	/* operator &@	(@ in Groonga) */
 #define PGrnQueryStrategyV2Number		13	/* operator &?  (query in Groonga) */
-#define PGrnScriptStrategyV2Number		14	/* operator &`  (script in Groonga) */
-#define PGrnMatchContainStrategyNumber	15	/* operator &@> (@ in Groonga) */
-#define PGrnQueryContainStrategyNumber	16	/* operator &?> (query in Groonga) */
+#define PGrnSimilarStrategyV2Number		14	/* operator &~? (similar in Groonga) */
+#define PGrnScriptStrategyV2Number		15	/* operator &`  (script in Groonga) */
+#define PGrnMatchContainStrategyNumber	16	/* operator &@> (@ in Groonga) */
+#define PGrnQueryContainStrategyNumber	17	/* operator &?> (query in Groonga) */
 
 /* file and table names */
 #define PGrnLogBasename					"pgroonga.log"
@@ -68,6 +69,7 @@ extern Datum PGDLLEXPORT pgroonga_match_jsonb(PG_FUNCTION_ARGS);
 /* v2 */
 extern Datum PGDLLEXPORT pgroonga_match_text(PG_FUNCTION_ARGS);
 extern Datum PGDLLEXPORT pgroonga_query_text(PG_FUNCTION_ARGS);
+extern Datum PGDLLEXPORT pgroonga_similar_text(PG_FUNCTION_ARGS);
 extern Datum PGDLLEXPORT pgroonga_script_text(PG_FUNCTION_ARGS);
 extern Datum PGDLLEXPORT pgroonga_match_contain_text(PG_FUNCTION_ARGS);
 extern Datum PGDLLEXPORT pgroonga_query_contain_text(PG_FUNCTION_ARGS);
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Back to archive index