And here is the patch, that I forgot to attach
> Hello,
> 
> I have developed a variation of cover density ranking functions that counts 
> only covers that are lesser than a specified limit. It is useful for finding 
> combinations of terms that appear nearby one another. Here is an example of 
> usage:

...

> 
> Find attached a path agains 9.1.2 sources. I preferred to make a patch, not a 
> separate extension because it is only 1 statement change in calc_rank_cd 
> function. If I have to make an extension a lot of code would be duplicated 
> between backend/utils/adt/tsrank.c and the extension.
> 
--
Luben Karavelov
diff -pur postgresql-9.1-9.1.2/src/backend/utils/adt/tsrank.c /usr/src/postgresql-9.1-9.1.2/src/backend/utils/adt/tsrank.c
--- postgresql-9.1-9.1.2/src/backend/utils/adt/tsrank.c	2011-12-01 23:47:20.000000000 +0200
+++ /usr/src/postgresql-9.1-9.1.2/src/backend/utils/adt/tsrank.c	2012-01-27 07:45:34.558028176 +0200
@@ -724,7 +724,7 @@ get_docrep(TSVector txt, QueryRepresenta
 }
 
 static float4
-calc_rank_cd(float4 *arrdata, TSVector txt, TSQuery query, int method)
+calc_rank_cd(int limit, float4 *arrdata, TSVector txt, TSQuery query, int method)
 {
 	DocRepresentation *doc;
 	int			len,
@@ -768,6 +768,9 @@ calc_rank_cd(float4 *arrdata, TSVector t
 		int			nNoise;
 		DocRepresentation *ptr = ext.begin;
 
+        if (limit > 0 && ext.end->pos - ext.begin->pos > limit) 
+            continue;
+
 		while (ptr <= ext.end)
 		{
 			InvSum += invws[ptr->wclass];
@@ -834,7 +837,7 @@ ts_rankcd_wttf(PG_FUNCTION_ARGS)
 	int			method = PG_GETARG_INT32(3);
 	float		res;
 
-	res = calc_rank_cd(getWeights(win), txt, query, method);
+	res = calc_rank_cd(0, getWeights(win), txt, query, method);
 
 	PG_FREE_IF_COPY(win, 0);
 	PG_FREE_IF_COPY(txt, 1);
@@ -850,7 +853,7 @@ ts_rankcd_wtt(PG_FUNCTION_ARGS)
 	TSQuery		query = PG_GETARG_TSQUERY(2);
 	float		res;
 
-	res = calc_rank_cd(getWeights(win), txt, query, DEF_NORM_METHOD);
+	res = calc_rank_cd(0, getWeights(win), txt, query, DEF_NORM_METHOD);
 
 	PG_FREE_IF_COPY(win, 0);
 	PG_FREE_IF_COPY(txt, 1);
@@ -866,7 +869,7 @@ ts_rankcd_ttf(PG_FUNCTION_ARGS)
 	int			method = PG_GETARG_INT32(2);
 	float		res;
 
-	res = calc_rank_cd(getWeights(NULL), txt, query, method);
+	res = calc_rank_cd(0, getWeights(NULL), txt, query, method);
 
 	PG_FREE_IF_COPY(txt, 0);
 	PG_FREE_IF_COPY(query, 1);
@@ -880,9 +883,75 @@ ts_rankcd_tt(PG_FUNCTION_ARGS)
 	TSQuery		query = PG_GETARG_TSQUERY(1);
 	float		res;
 
-	res = calc_rank_cd(getWeights(NULL), txt, query, DEF_NORM_METHOD);
+	res = calc_rank_cd(0, getWeights(NULL), txt, query, DEF_NORM_METHOD);
 
 	PG_FREE_IF_COPY(txt, 0);
 	PG_FREE_IF_COPY(query, 1);
 	PG_RETURN_FLOAT4(res);
 }
+
+Datum
+ts_rankcd_lwttf(PG_FUNCTION_ARGS)
+{
+    int         limit = PG_GETARG_INT32(0);
+	ArrayType  *win = (ArrayType *) PG_DETOAST_DATUM(PG_GETARG_DATUM(1));
+	TSVector	txt = PG_GETARG_TSVECTOR(2);
+	TSQuery		query = PG_GETARG_TSQUERY(3);
+	int			method = PG_GETARG_INT32(4);
+	float		res;
+
+	res = calc_rank_cd(limit, getWeights(win), txt, query, method);
+
+	PG_FREE_IF_COPY(win, 1);
+	PG_FREE_IF_COPY(txt, 2);
+	PG_FREE_IF_COPY(query, 3);
+	PG_RETURN_FLOAT4(res);
+}
+
+Datum
+ts_rankcd_lwtt(PG_FUNCTION_ARGS)
+{
+    int         limit = PG_GETARG_INT32(0);
+	ArrayType  *win = (ArrayType *) PG_DETOAST_DATUM(PG_GETARG_DATUM(1));
+	TSVector	txt = PG_GETARG_TSVECTOR(2);
+	TSQuery		query = PG_GETARG_TSQUERY(3);
+	float		res;
+
+	res = calc_rank_cd(limit, getWeights(win), txt, query, DEF_NORM_METHOD);
+
+	PG_FREE_IF_COPY(win, 1);
+	PG_FREE_IF_COPY(txt, 2);
+	PG_FREE_IF_COPY(query, 3);
+	PG_RETURN_FLOAT4(res);
+}
+
+Datum
+ts_rankcd_lttf(PG_FUNCTION_ARGS)
+{
+    int         limit = PG_GETARG_INT32(0);
+	TSVector	txt = PG_GETARG_TSVECTOR(1);
+	TSQuery		query = PG_GETARG_TSQUERY(2);
+	int			method = PG_GETARG_INT32(3);
+	float		res;
+
+	res = calc_rank_cd(limit, getWeights(NULL), txt, query, method);
+
+	PG_FREE_IF_COPY(txt, 1);
+	PG_FREE_IF_COPY(query, 2);
+	PG_RETURN_FLOAT4(res);
+}
+
+Datum
+ts_rankcd_ltt(PG_FUNCTION_ARGS)
+{
+    int         limit = PG_GETARG_INT32(0);
+	TSVector	txt = PG_GETARG_TSVECTOR(1);
+	TSQuery		query = PG_GETARG_TSQUERY(2);
+	float		res;
+
+	res = calc_rank_cd(limit, getWeights(NULL), txt, query, DEF_NORM_METHOD);
+
+	PG_FREE_IF_COPY(txt, 1);
+	PG_FREE_IF_COPY(query, 2);
+	PG_RETURN_FLOAT4(res);
+}
diff -pur postgresql-9.1-9.1.2/src/include/catalog/pg_proc.h /usr/src/postgresql-9.1-9.1.2/src/include/catalog/pg_proc.h
--- postgresql-9.1-9.1.2/src/include/catalog/pg_proc.h	2011-12-01 23:47:20.000000000 +0200
+++ /usr/src/postgresql-9.1-9.1.2/src/include/catalog/pg_proc.h	2012-01-27 05:45:53.944979678 +0200
@@ -4159,6 +4159,15 @@ DATA(insert OID = 3709 (  ts_rank_cd	PGN
 DESCR("relevance");
 DATA(insert OID = 3710 (  ts_rank_cd	PGNSP PGUID 12 1 0 0 f f f t f i 2 0 700 "3614 3615" _null_ _null_ _null_ _null_ ts_rankcd_tt _null_ _null_ _null_ ));
 DESCR("relevance");
+DATA(insert OID = 3675 (  ts_rank_cd	PGNSP PGUID 12 1 0 0 f f f t f i 5 0 700 "23 1021 3614 3615 23" _null_ _null_ _null_ _null_ ts_rankcd_lwttf _null_ _null_ _null_ ));
+DESCR("relevance");
+DATA(insert OID = 3676 (  ts_rank_cd	PGNSP PGUID 12 1 0 0 f f f t f i 4 0 700 "23 1021 3614 3615" _null_ _null_ _null_ _null_ ts_rankcd_lwtt _null_ _null_ _null_ ));
+DESCR("relevance");
+DATA(insert OID = 3677 (  ts_rank_cd	PGNSP PGUID 12 1 0 0 f f f t f i 4 0 700 "23 3614 3615 23" _null_ _null_ _null_ _null_ ts_rankcd_lttf _null_ _null_ _null_ ));
+DESCR("relevance");
+DATA(insert OID = 3678 (  ts_rank_cd	PGNSP PGUID 12 1 0 0 f f f t f i 3 0 700 "23 3614 3615" _null_ _null_ _null_ _null_ ts_rankcd_ltt _null_ _null_ _null_ ));
+DESCR("relevance");
+
 
 DATA(insert OID = 3713 (  ts_token_type PGNSP PGUID 12 1 16 0 f f f t t i 1 0 2249 "26" "{26,23,25,25}" "{i,o,o,o}" "{parser_oid,tokid,alias,description}" _null_ ts_token_type_byid _null_ _null_ _null_ ));
 DESCR("get parser's token types");
diff -pur postgresql-9.1-9.1.2/src/include/tsearch/ts_type.h /usr/src/postgresql-9.1-9.1.2/src/include/tsearch/ts_type.h
--- postgresql-9.1-9.1.2/src/include/tsearch/ts_type.h	2011-12-01 23:47:20.000000000 +0200
+++ /usr/src/postgresql-9.1-9.1.2/src/include/tsearch/ts_type.h	2012-01-27 05:41:24.741571183 +0200
@@ -152,6 +152,10 @@ extern Datum ts_rankcd_tt(PG_FUNCTION_AR
 extern Datum ts_rankcd_wtt(PG_FUNCTION_ARGS);
 extern Datum ts_rankcd_ttf(PG_FUNCTION_ARGS);
 extern Datum ts_rankcd_wttf(PG_FUNCTION_ARGS);
+extern Datum ts_rankcd_ltt(PG_FUNCTION_ARGS);
+extern Datum ts_rankcd_lwtt(PG_FUNCTION_ARGS);
+extern Datum ts_rankcd_lttf(PG_FUNCTION_ARGS);
+extern Datum ts_rankcd_lwttf(PG_FUNCTION_ARGS);
 
 extern Datum tsmatchsel(PG_FUNCTION_ARGS);
 extern Datum tsmatchjoinsel(PG_FUNCTION_ARGS);
-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

Reply via email to