[Groonga-commit] pgroonga/pgroonga at 1835139 [master] query_expand: support multiple terms case

Back to archive index

Kouhei Sutou null+****@clear*****
Fri Jun 30 15:51:22 JST 2017


Kouhei Sutou	2017-06-30 15:51:22 +0900 (Fri, 30 Jun 2017)

  New Revision: 18351398245754f13990341339d03bd0bffb56c7
  https://github.com/pgroonga/pgroonga/commit/18351398245754f13990341339d03bd0bffb56c7

  Message:
    query_expand: support multiple terms case

  Added files:
    expected/function/query-expand/multiple-terms.out
    sql/function/query-expand/multiple-terms.sql
  Modified files:
    src/pgrn-query-expand.c

  Added: expected/function/query-expand/multiple-terms.out (+14 -0) 100644
===================================================================
--- /dev/null
+++ expected/function/query-expand/multiple-terms.out    2017-06-30 15:51:22 +0900 (150413e)
@@ -0,0 +1,14 @@
+CREATE TABLE synonyms (
+  term text,
+  synonyms text[]
+);
+CREATE INDEX synonyms_term_index ON synonyms (term);
+INSERT INTO synonyms VALUES ('Groonga', ARRAY['Groonga', 'Senna']);
+INSERT INTO synonyms VALUES ('Groonga', ARRAY['Mroonga', 'PGroonga', 'Rroonga']);
+SELECT pgroonga.query_expand('synonyms', 'term', 'synonyms', 'Groonga');
+                          query_expand                          
+----------------------------------------------------------------
+ ((Mroonga) OR (PGroonga) OR (Rroonga) OR (Groonga) OR (Senna))
+(1 row)
+
+DROP TABLE synonyms;

  Added: sql/function/query-expand/multiple-terms.sql (+13 -0) 100644
===================================================================
--- /dev/null
+++ sql/function/query-expand/multiple-terms.sql    2017-06-30 15:51:22 +0900 (e8b2d15)
@@ -0,0 +1,13 @@
+CREATE TABLE synonyms (
+  term text,
+  synonyms text[]
+);
+
+CREATE INDEX synonyms_term_index ON synonyms (term);
+
+INSERT INTO synonyms VALUES ('Groonga', ARRAY['Groonga', 'Senna']);
+INSERT INTO synonyms VALUES ('Groonga', ARRAY['Mroonga', 'PGroonga', 'Rroonga']);
+
+SELECT pgroonga.query_expand('synonyms', 'term', 'synonyms', 'Groonga');
+
+DROP TABLE synonyms;

  Modified: src/pgrn-query-expand.c (+32 -22)
===================================================================
--- src/pgrn-query-expand.c    2017-06-30 15:34:53 +0900 (7314747)
+++ src/pgrn-query-expand.c    2017-06-30 15:51:22 +0900 (eae51eb)
@@ -50,8 +50,7 @@ func_query_expander_postgresql(grn_ctx *ctx,
 	int nKeys = 1;
 	HeapScanDesc heapScan = NULL;
 	HeapTuple tuple;
-	Datum synonymsDatum;
-	bool isNULL;
+	int ith_synonyms = 0;
 
 	term = args[0];
 	expandedTerm = args[1];
@@ -73,7 +72,6 @@ func_query_expander_postgresql(grn_ctx *ctx,
 					opFunctionID,
 					PointerGetDatum(termText));
 		index_rescan(currentData.scan, scanKeys, nKeys, NULL, 0);
-		tuple = index_getnext(currentData.scan, ForwardScanDirection);
 	}
 	else
 	{
@@ -86,27 +84,40 @@ func_query_expander_postgresql(grn_ctx *ctx,
 								  currentData.snapshot,
 								  nKeys,
 								  scanKeys);
-		tuple = heap_getnext(heapScan, ForwardScanDirection);
 	}
 
-	if (!tuple)
-		goto exit;
-
-	synonymsDatum = heap_getattr(tuple,
-								 currentData.synonymsAttribute->attnum,
-								 RelationGetDescr(currentData.table),
-								 &isNULL);
-	if (isNULL)
-		goto exit;
-
+	while (true)
 	{
+		Datum synonymsDatum;
+		bool isNULL;
 		ArrayType *synonymsArray;
 		int i, n;
 
+		if (currentData.index)
+			tuple = index_getnext(currentData.scan, ForwardScanDirection);
+		else
+			tuple = heap_getnext(heapScan, ForwardScanDirection);
+
+		if (!tuple)
+			break;
+
+		synonymsDatum = heap_getattr(tuple,
+									 currentData.synonymsAttribute->attnum,
+									 RelationGetDescr(currentData.table),
+									 &isNULL);
+		if (isNULL)
+			continue;
+
 		synonymsArray = DatumGetArrayTypeP(synonymsDatum);
 		n = ARR_DIMS(synonymsArray)[0];
-		if (n > 1)
+		if (n == 0)
+			continue;
+
+		if (ith_synonyms == 0)
 			GRN_TEXT_PUTC(ctx, expandedTerm, '(');
+		else
+			GRN_TEXT_PUTS(ctx, expandedTerm, " OR ");
+
 		for (i = 1; i <= n; i++)
 		{
 			Datum synonymDatum;
@@ -121,21 +132,20 @@ func_query_expander_postgresql(grn_ctx *ctx,
 			synonym = DatumGetTextP(synonymDatum);
 			if (i > 1)
 				GRN_TEXT_PUTS(ctx, expandedTerm, " OR ");
-			if (n > 1)
-				GRN_TEXT_PUTC(ctx, expandedTerm, '(');
+			GRN_TEXT_PUTC(ctx, expandedTerm, '(');
 			GRN_TEXT_PUT(ctx, expandedTerm,
 						 VARDATA_ANY(synonym),
 						 VARSIZE_ANY_EXHDR(synonym));
-			if (n > 1)
-				GRN_TEXT_PUTC(ctx, expandedTerm, ')');
-		}
-		if (n > 1)
 			GRN_TEXT_PUTC(ctx, expandedTerm, ')');
+		}
+
+		ith_synonyms++;
 
 		rc = GRN_SUCCESS;
 	}
+	if (ith_synonyms > 0)
+		GRN_TEXT_PUTC(ctx, expandedTerm, ')');
 
-exit:
 	if (heapScan)
 		heap_endscan(heapScan);
 
-------------- next part --------------
HTML����������������������������...
下載 



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