2003-07-21 18:27:44 +08:00
|
|
|
/*
|
|
|
|
* Relevation
|
|
|
|
* Teodor Sigaev <teodor@sigaev.ru>
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
2005-11-18 10:38:24 +08:00
|
|
|
|
2003-07-21 18:27:44 +08:00
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
#include "access/gist.h"
|
|
|
|
#include "access/itup.h"
|
2005-11-18 10:38:24 +08:00
|
|
|
#include "catalog/namespace.h"
|
|
|
|
#include "commands/trigger.h"
|
|
|
|
#include "executor/spi.h"
|
2003-07-21 18:27:44 +08:00
|
|
|
#include "fmgr.h"
|
|
|
|
#include "funcapi.h"
|
|
|
|
#include "nodes/pg_list.h"
|
2005-11-18 10:38:24 +08:00
|
|
|
#include "storage/bufpage.h"
|
2003-07-21 18:27:44 +08:00
|
|
|
#include "utils/array.h"
|
2005-11-18 10:38:24 +08:00
|
|
|
#include "utils/builtins.h"
|
2003-07-21 18:27:44 +08:00
|
|
|
|
|
|
|
#include "tsvector.h"
|
|
|
|
#include "query.h"
|
|
|
|
#include "common.h"
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(rank);
|
|
|
|
Datum rank(PG_FUNCTION_ARGS);
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(rank_def);
|
|
|
|
Datum rank_def(PG_FUNCTION_ARGS);
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(rank_cd);
|
|
|
|
Datum rank_cd(PG_FUNCTION_ARGS);
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(rank_cd_def);
|
|
|
|
Datum rank_cd_def(PG_FUNCTION_ARGS);
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(get_covers);
|
|
|
|
Datum get_covers(PG_FUNCTION_ARGS);
|
|
|
|
|
2003-08-04 08:43:34 +08:00
|
|
|
static float weights[] = {0.1, 0.2, 0.4, 1.0};
|
2003-07-21 18:27:44 +08:00
|
|
|
|
2005-01-25 20:36:25 +08:00
|
|
|
#define wpos(wep) ( w[ WEP_GETWEIGHT(wep) ] )
|
2003-07-21 18:27:44 +08:00
|
|
|
|
2003-08-04 08:43:34 +08:00
|
|
|
#define DEF_NORM_METHOD 0
|
2003-07-21 18:27:44 +08:00
|
|
|
|
2005-03-05 23:48:32 +08:00
|
|
|
static float calc_rank_or(float *w, tsvector * t, QUERYTYPE * q);
|
|
|
|
static float calc_rank_and(float *w, tsvector * t, QUERYTYPE * q);
|
2005-10-15 10:49:52 +08:00
|
|
|
|
2003-07-21 18:27:44 +08:00
|
|
|
/*
|
|
|
|
* Returns a weight of a word collocation
|
|
|
|
*/
|
2003-08-04 08:43:34 +08:00
|
|
|
static float4
|
|
|
|
word_distance(int4 w)
|
|
|
|
{
|
|
|
|
if (w > 100)
|
|
|
|
return 1e-30;
|
2003-07-21 18:27:44 +08:00
|
|
|
|
2003-08-04 08:43:34 +08:00
|
|
|
return 1.0 / (1.005 + 0.05 * exp(((float4) w) / 1.5 - 2));
|
2003-07-21 18:27:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2003-08-04 08:43:34 +08:00
|
|
|
cnt_length(tsvector * t)
|
|
|
|
{
|
|
|
|
WordEntry *ptr = ARRPTR(t),
|
|
|
|
*end = (WordEntry *) STRPTR(t);
|
|
|
|
int len = 0,
|
|
|
|
clen;
|
|
|
|
|
|
|
|
while (ptr < end)
|
|
|
|
{
|
|
|
|
if ((clen = POSDATALEN(t, ptr)) == 0)
|
2003-07-21 18:27:44 +08:00
|
|
|
len += 1;
|
|
|
|
else
|
|
|
|
len += clen;
|
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int4
|
2003-08-04 08:43:34 +08:00
|
|
|
WordECompareITEM(char *eval, char *qval, WordEntry * ptr, ITEM * item)
|
|
|
|
{
|
|
|
|
if (ptr->len == item->length)
|
|
|
|
return strncmp(
|
|
|
|
eval + ptr->pos,
|
|
|
|
qval + item->distance,
|
|
|
|
item->length);
|
|
|
|
|
|
|
|
return (ptr->len > item->length) ? 1 : -1;
|
2003-07-21 18:27:44 +08:00
|
|
|
}
|
|
|
|
|
2003-08-04 08:43:34 +08:00
|
|
|
static WordEntry *
|
|
|
|
find_wordentry(tsvector * t, QUERYTYPE * q, ITEM * item)
|
|
|
|
{
|
|
|
|
WordEntry *StopLow = ARRPTR(t);
|
|
|
|
WordEntry *StopHigh = (WordEntry *) STRPTR(t);
|
|
|
|
WordEntry *StopMiddle;
|
|
|
|
int difference;
|
|
|
|
|
|
|
|
/* Loop invariant: StopLow <= item < StopHigh */
|
|
|
|
|
|
|
|
while (StopLow < StopHigh)
|
|
|
|
{
|
|
|
|
StopMiddle = StopLow + (StopHigh - StopLow) / 2;
|
|
|
|
difference = WordECompareITEM(STRPTR(t), GETOPERAND(q), StopMiddle, item);
|
|
|
|
if (difference == 0)
|
|
|
|
return StopMiddle;
|
|
|
|
else if (difference < 0)
|
|
|
|
StopLow = StopMiddle + 1;
|
|
|
|
else
|
|
|
|
StopHigh = StopMiddle;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
2003-07-21 18:27:44 +08:00
|
|
|
}
|
|
|
|
|
2005-03-05 23:48:32 +08:00
|
|
|
|
2005-10-15 10:49:52 +08:00
|
|
|
static char *SortAndUniqOperand = NULL;
|
2005-03-05 23:48:32 +08:00
|
|
|
|
|
|
|
static int
|
2005-10-15 10:49:52 +08:00
|
|
|
compareITEM(const void *a, const void *b)
|
|
|
|
{
|
|
|
|
if ((*(ITEM **) a)->length == (*(ITEM **) b)->length)
|
|
|
|
return strncmp(SortAndUniqOperand + (*(ITEM **) a)->distance,
|
|
|
|
SortAndUniqOperand + (*(ITEM **) b)->distance,
|
|
|
|
(*(ITEM **) b)->length);
|
2005-03-05 23:48:32 +08:00
|
|
|
|
2005-10-15 10:49:52 +08:00
|
|
|
return ((*(ITEM **) a)->length > (*(ITEM **) b)->length) ? 1 : -1;
|
2005-03-05 23:48:32 +08:00
|
|
|
}
|
|
|
|
|
2005-10-15 10:49:52 +08:00
|
|
|
static ITEM **
|
|
|
|
SortAndUniqItems(char *operand, ITEM * item, int *size)
|
|
|
|
{
|
|
|
|
ITEM **res,
|
|
|
|
**ptr,
|
|
|
|
**prevptr;
|
|
|
|
|
|
|
|
ptr = res = (ITEM **) palloc(sizeof(ITEM *) * *size);
|
2005-03-05 23:48:32 +08:00
|
|
|
|
2005-10-15 10:49:52 +08:00
|
|
|
while ((*size)--)
|
|
|
|
{
|
|
|
|
if (item->type == VAL)
|
|
|
|
{
|
2005-03-05 23:48:32 +08:00
|
|
|
*ptr = item;
|
|
|
|
ptr++;
|
2005-10-15 10:49:52 +08:00
|
|
|
}
|
2005-03-05 23:48:32 +08:00
|
|
|
item++;
|
|
|
|
}
|
|
|
|
|
2005-10-15 10:49:52 +08:00
|
|
|
*size = ptr - res;
|
|
|
|
if (*size < 2)
|
2005-03-05 23:48:32 +08:00
|
|
|
return res;
|
|
|
|
|
2005-10-15 10:49:52 +08:00
|
|
|
SortAndUniqOperand = operand;
|
|
|
|
qsort(res, *size, sizeof(ITEM **), compareITEM);
|
2005-03-05 23:48:32 +08:00
|
|
|
|
|
|
|
ptr = res + 1;
|
|
|
|
prevptr = res;
|
|
|
|
|
2005-10-15 10:49:52 +08:00
|
|
|
while (ptr - res < *size)
|
|
|
|
{
|
|
|
|
if (compareITEM((void *) ptr, (void *) prevptr) != 0)
|
|
|
|
{
|
2005-03-05 23:48:32 +08:00
|
|
|
prevptr++;
|
|
|
|
*prevptr = *ptr;
|
|
|
|
}
|
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
*size = prevptr + 1 - res;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2003-08-04 08:43:34 +08:00
|
|
|
static WordEntryPos POSNULL[] = {
|
2005-01-25 20:36:25 +08:00
|
|
|
0,
|
|
|
|
0
|
2003-07-21 18:27:44 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static float
|
2003-08-04 08:43:34 +08:00
|
|
|
calc_rank_and(float *w, tsvector * t, QUERYTYPE * q)
|
|
|
|
{
|
2005-03-05 23:48:32 +08:00
|
|
|
uint16 **pos;
|
2003-08-04 08:43:34 +08:00
|
|
|
int i,
|
|
|
|
k,
|
|
|
|
l,
|
|
|
|
p;
|
|
|
|
WordEntry *entry;
|
|
|
|
WordEntryPos *post,
|
|
|
|
*ct;
|
|
|
|
int4 dimt,
|
|
|
|
lenct,
|
|
|
|
dist;
|
|
|
|
float res = -1.0;
|
2005-10-15 10:49:52 +08:00
|
|
|
ITEM **item;
|
|
|
|
int size = q->size;
|
2005-03-05 23:48:32 +08:00
|
|
|
|
2005-10-15 10:49:52 +08:00
|
|
|
item = SortAndUniqItems(GETOPERAND(q), GETQUERY(q), &size);
|
|
|
|
if (size < 2)
|
|
|
|
{
|
2005-03-05 23:48:32 +08:00
|
|
|
pfree(item);
|
|
|
|
return calc_rank_or(w, t, q);
|
2005-10-15 10:49:52 +08:00
|
|
|
}
|
2005-03-05 23:48:32 +08:00
|
|
|
pos = (uint16 **) palloc(sizeof(uint16 *) * q->size);
|
|
|
|
memset(pos, 0, sizeof(uint16 *) * q->size);
|
2003-08-04 08:43:34 +08:00
|
|
|
*(uint16 *) POSNULL = lengthof(POSNULL) - 1;
|
2005-10-15 10:49:52 +08:00
|
|
|
WEP_SETPOS(POSNULL[1], MAXENTRYPOS - 1);
|
2003-08-04 08:43:34 +08:00
|
|
|
|
2005-03-05 23:48:32 +08:00
|
|
|
for (i = 0; i < size; i++)
|
2003-08-04 08:43:34 +08:00
|
|
|
{
|
2005-03-05 23:48:32 +08:00
|
|
|
entry = find_wordentry(t, q, item[i]);
|
2003-08-04 08:43:34 +08:00
|
|
|
if (!entry)
|
2003-07-21 18:27:44 +08:00
|
|
|
continue;
|
|
|
|
|
2003-08-04 08:43:34 +08:00
|
|
|
if (entry->haspos)
|
|
|
|
pos[i] = (uint16 *) _POSDATAPTR(t, entry);
|
2003-07-21 18:27:44 +08:00
|
|
|
else
|
2003-08-04 08:43:34 +08:00
|
|
|
pos[i] = (uint16 *) POSNULL;
|
|
|
|
|
|
|
|
|
|
|
|
dimt = *(uint16 *) (pos[i]);
|
|
|
|
post = (WordEntryPos *) (pos[i] + 1);
|
|
|
|
for (k = 0; k < i; k++)
|
|
|
|
{
|
|
|
|
if (!pos[k])
|
|
|
|
continue;
|
|
|
|
lenct = *(uint16 *) (pos[k]);
|
|
|
|
ct = (WordEntryPos *) (pos[k] + 1);
|
|
|
|
for (l = 0; l < dimt; l++)
|
|
|
|
{
|
|
|
|
for (p = 0; p < lenct; p++)
|
|
|
|
{
|
2005-10-15 10:49:52 +08:00
|
|
|
dist = Abs((int) WEP_GETPOS(post[l]) - (int) WEP_GETPOS(ct[p]));
|
2003-08-04 08:43:34 +08:00
|
|
|
if (dist || (dist == 0 && (pos[i] == (uint16 *) POSNULL || pos[k] == (uint16 *) POSNULL)))
|
|
|
|
{
|
|
|
|
float curw;
|
|
|
|
|
|
|
|
if (!dist)
|
|
|
|
dist = MAXENTRYPOS;
|
2005-01-25 20:36:25 +08:00
|
|
|
curw = sqrt(wpos(post[l]) * wpos(ct[p]) * word_distance(dist));
|
2003-08-04 08:43:34 +08:00
|
|
|
res = (res < 0) ? curw : 1.0 - (1.0 - res) * (1.0 - curw);
|
2003-07-21 18:27:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pfree(pos);
|
2005-03-05 23:48:32 +08:00
|
|
|
pfree(item);
|
2003-08-04 08:43:34 +08:00
|
|
|
return res;
|
2003-07-21 18:27:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static float
|
2003-08-04 08:43:34 +08:00
|
|
|
calc_rank_or(float *w, tsvector * t, QUERYTYPE * q)
|
|
|
|
{
|
|
|
|
WordEntry *entry;
|
|
|
|
WordEntryPos *post;
|
|
|
|
int4 dimt,
|
|
|
|
j,
|
|
|
|
i;
|
2005-10-28 21:05:06 +08:00
|
|
|
float res = 0.0;
|
2005-10-15 10:49:52 +08:00
|
|
|
ITEM **item;
|
|
|
|
int size = q->size;
|
2003-08-04 08:43:34 +08:00
|
|
|
|
|
|
|
*(uint16 *) POSNULL = lengthof(POSNULL) - 1;
|
2005-10-15 10:49:52 +08:00
|
|
|
item = SortAndUniqItems(GETOPERAND(q), GETQUERY(q), &size);
|
2003-08-04 08:43:34 +08:00
|
|
|
|
2005-03-05 23:48:32 +08:00
|
|
|
for (i = 0; i < size; i++)
|
2003-08-04 08:43:34 +08:00
|
|
|
{
|
2005-11-23 02:17:34 +08:00
|
|
|
float resj,
|
|
|
|
wjm;
|
|
|
|
int4 jm;
|
|
|
|
|
2005-03-05 23:48:32 +08:00
|
|
|
entry = find_wordentry(t, q, item[i]);
|
2003-08-04 08:43:34 +08:00
|
|
|
if (!entry)
|
2003-07-21 18:27:44 +08:00
|
|
|
continue;
|
|
|
|
|
2003-08-04 08:43:34 +08:00
|
|
|
if (entry->haspos)
|
|
|
|
{
|
|
|
|
dimt = POSDATALEN(t, entry);
|
|
|
|
post = POSDATAPTR(t, entry);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dimt = *(uint16 *) POSNULL;
|
|
|
|
post = POSNULL + 1;
|
2003-07-21 18:27:44 +08:00
|
|
|
}
|
|
|
|
|
2005-11-23 02:17:34 +08:00
|
|
|
resj = 0.0;
|
|
|
|
wjm = -1.0;
|
|
|
|
jm = 0;
|
|
|
|
for (j = 0; j < dimt; j++)
|
|
|
|
{
|
|
|
|
resj = resj + wpos(post[j]) / ((j + 1) * (j + 1));
|
|
|
|
if (wpos(post[j]) > wjm)
|
|
|
|
{
|
|
|
|
wjm = wpos(post[j]);
|
|
|
|
jm = j;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
limit (sum(i/i^2),i->inf) = pi^2/6
|
|
|
|
resj = sum(wi/i^2),i=1,noccurence,
|
|
|
|
wi - should be sorted desc,
|
|
|
|
don't sort for now, just choose maximum weight. This should be corrected
|
2005-10-28 21:05:06 +08:00
|
|
|
Oleg Bartunov
|
|
|
|
*/
|
2005-11-23 02:17:34 +08:00
|
|
|
res = res + (wjm + resj - wjm / ((jm + 1) * (jm + 1))) / 1.64493406685;
|
2003-07-21 18:27:44 +08:00
|
|
|
}
|
2005-11-23 02:17:34 +08:00
|
|
|
if (size > 0)
|
|
|
|
res = res / size;
|
2005-10-15 10:49:52 +08:00
|
|
|
pfree(item);
|
2003-07-21 18:27:44 +08:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static float
|
2003-08-04 08:43:34 +08:00
|
|
|
calc_rank(float *w, tsvector * t, QUERYTYPE * q, int4 method)
|
|
|
|
{
|
|
|
|
ITEM *item = GETQUERY(q);
|
|
|
|
float res = 0.0;
|
2005-10-15 10:49:52 +08:00
|
|
|
int len;
|
2003-07-21 18:27:44 +08:00
|
|
|
|
|
|
|
if (!t->size || !q->size)
|
|
|
|
return 0.0;
|
|
|
|
|
2003-08-04 08:43:34 +08:00
|
|
|
res = (item->type != VAL && item->val == (int4) '&') ?
|
|
|
|
calc_rank_and(w, t, q) : calc_rank_or(w, t, q);
|
2003-07-21 18:27:44 +08:00
|
|
|
|
2003-08-04 08:43:34 +08:00
|
|
|
if (res < 0)
|
2003-07-21 18:27:44 +08:00
|
|
|
res = 1e-20;
|
|
|
|
|
2003-08-04 08:43:34 +08:00
|
|
|
switch (method)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
case 1:
|
2005-10-15 10:49:52 +08:00
|
|
|
res /= log((float) (cnt_length(t) + 1)) / log(2.0);
|
2003-08-04 08:43:34 +08:00
|
|
|
break;
|
|
|
|
case 2:
|
2005-06-01 19:45:03 +08:00
|
|
|
len = cnt_length(t);
|
2005-10-15 10:49:52 +08:00
|
|
|
if (len > 0)
|
|
|
|
res /= (float) len;
|
2003-08-04 08:43:34 +08:00
|
|
|
break;
|
2003-07-21 18:27:44 +08:00
|
|
|
default:
|
2003-08-04 08:43:34 +08:00
|
|
|
/* internal error */
|
|
|
|
elog(ERROR, "unrecognized normalization method: %d", method);
|
|
|
|
}
|
2003-07-21 18:27:44 +08:00
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
2003-08-04 08:43:34 +08:00
|
|
|
rank(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2003-07-21 18:27:44 +08:00
|
|
|
ArrayType *win = (ArrayType *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
|
2003-08-04 08:43:34 +08:00
|
|
|
tsvector *txt = (tsvector *) PG_DETOAST_DATUM(PG_GETARG_DATUM(1));
|
2003-07-21 18:27:44 +08:00
|
|
|
QUERYTYPE *query = (QUERYTYPE *) PG_DETOAST_DATUM(PG_GETARG_DATUM(2));
|
2003-08-04 08:43:34 +08:00
|
|
|
int method = DEF_NORM_METHOD;
|
|
|
|
float res = 0.0;
|
|
|
|
float ws[lengthof(weights)];
|
2005-11-18 10:38:24 +08:00
|
|
|
float4 *arrdata;
|
2003-08-04 08:43:34 +08:00
|
|
|
int i;
|
2003-07-21 18:27:44 +08:00
|
|
|
|
2003-08-04 08:43:34 +08:00
|
|
|
if (ARR_NDIM(win) != 1)
|
2003-07-25 01:52:50 +08:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
|
|
|
|
errmsg("array of weight must be one-dimensional")));
|
|
|
|
|
2003-08-04 08:43:34 +08:00
|
|
|
if (ARRNELEMS(win) < lengthof(weights))
|
2003-07-25 01:52:50 +08:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
|
|
|
|
errmsg("array of weight is too short")));
|
2003-07-21 18:27:44 +08:00
|
|
|
|
2005-11-18 10:38:24 +08:00
|
|
|
if (ARR_HASNULL(win))
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
|
|
|
|
errmsg("array of weight must not contain nulls")));
|
|
|
|
|
|
|
|
arrdata = (float4 *) ARR_DATA_PTR(win);
|
2003-08-04 08:43:34 +08:00
|
|
|
for (i = 0; i < lengthof(weights); i++)
|
|
|
|
{
|
2005-11-18 10:38:24 +08:00
|
|
|
ws[i] = (arrdata[i] >= 0) ? arrdata[i] : weights[i];
|
2003-08-04 08:43:34 +08:00
|
|
|
if (ws[i] > 1.0)
|
2003-07-25 01:52:50 +08:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
|
|
|
errmsg("weight out of range")));
|
2003-08-04 08:43:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (PG_NARGS() == 4)
|
|
|
|
method = PG_GETARG_INT32(3);
|
2003-07-21 18:27:44 +08:00
|
|
|
|
2003-08-04 08:43:34 +08:00
|
|
|
res = calc_rank(ws, txt, query, method);
|
2003-07-21 18:27:44 +08:00
|
|
|
|
|
|
|
PG_FREE_IF_COPY(win, 0);
|
|
|
|
PG_FREE_IF_COPY(txt, 1);
|
|
|
|
PG_FREE_IF_COPY(query, 2);
|
|
|
|
PG_RETURN_FLOAT4(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
Datum
|
2003-08-04 08:43:34 +08:00
|
|
|
rank_def(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
tsvector *txt = (tsvector *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
|
2003-07-21 18:27:44 +08:00
|
|
|
QUERYTYPE *query = (QUERYTYPE *) PG_DETOAST_DATUM(PG_GETARG_DATUM(1));
|
2003-08-04 08:43:34 +08:00
|
|
|
float res = 0.0;
|
|
|
|
int method = DEF_NORM_METHOD;
|
2003-07-21 18:27:44 +08:00
|
|
|
|
2003-08-04 08:43:34 +08:00
|
|
|
if (PG_NARGS() == 3)
|
|
|
|
method = PG_GETARG_INT32(2);
|
|
|
|
|
|
|
|
res = calc_rank(weights, txt, query, method);
|
2003-07-21 18:27:44 +08:00
|
|
|
|
|
|
|
PG_FREE_IF_COPY(txt, 0);
|
|
|
|
PG_FREE_IF_COPY(query, 1);
|
|
|
|
PG_RETURN_FLOAT4(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-08-04 08:43:34 +08:00
|
|
|
typedef struct
|
|
|
|
{
|
2005-11-23 02:17:34 +08:00
|
|
|
ITEM **item;
|
New features for tsearch2:
1 Comparison operation for tsquery
2 Btree index on tsquery
3 numnode(tsquery) - returns 'length' of tsquery
4 tsquery @ tsquery, tsquery ~ tsquery - contains, contained for tsquery.
Note: They don't gurantee exact result, only MAY BE, so it
useful only for speed up rewrite functions
5 GiST index support for @,~
6 rewrite():
select rewrite(orig, what, to);
select rewrite(ARRAY[orig, what, to]) from tsquery_table;
select rewrite(orig, 'select what, to from tsquery_table;');
7 significantly improve cover algorithm
2005-11-09 01:08:46 +08:00
|
|
|
int16 nitem;
|
|
|
|
bool needfree;
|
2003-08-04 08:43:34 +08:00
|
|
|
int32 pos;
|
|
|
|
} DocRepresentation;
|
2003-07-21 18:27:44 +08:00
|
|
|
|
|
|
|
static int
|
2003-08-04 08:43:34 +08:00
|
|
|
compareDocR(const void *a, const void *b)
|
|
|
|
{
|
|
|
|
if (((DocRepresentation *) a)->pos == ((DocRepresentation *) b)->pos)
|
2005-03-31 23:08:08 +08:00
|
|
|
return 0;
|
2003-08-04 08:43:34 +08:00
|
|
|
return (((DocRepresentation *) a)->pos > ((DocRepresentation *) b)->pos) ? 1 : -1;
|
2003-07-21 18:27:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2005-11-23 02:17:34 +08:00
|
|
|
checkcondition_ITEM(void *checkval, ITEM * val)
|
|
|
|
{
|
|
|
|
return (bool) (val->istrue);
|
2003-07-21 18:27:44 +08:00
|
|
|
}
|
|
|
|
|
New features for tsearch2:
1 Comparison operation for tsquery
2 Btree index on tsquery
3 numnode(tsquery) - returns 'length' of tsquery
4 tsquery @ tsquery, tsquery ~ tsquery - contains, contained for tsquery.
Note: They don't gurantee exact result, only MAY BE, so it
useful only for speed up rewrite functions
5 GiST index support for @,~
6 rewrite():
select rewrite(orig, what, to);
select rewrite(ARRAY[orig, what, to]) from tsquery_table;
select rewrite(orig, 'select what, to from tsquery_table;');
7 significantly improve cover algorithm
2005-11-09 01:08:46 +08:00
|
|
|
static void
|
2005-11-23 02:17:34 +08:00
|
|
|
reset_istrue_flag(QUERYTYPE * query)
|
|
|
|
{
|
|
|
|
ITEM *item = GETQUERY(query);
|
|
|
|
int i;
|
2003-07-21 18:27:44 +08:00
|
|
|
|
New features for tsearch2:
1 Comparison operation for tsquery
2 Btree index on tsquery
3 numnode(tsquery) - returns 'length' of tsquery
4 tsquery @ tsquery, tsquery ~ tsquery - contains, contained for tsquery.
Note: They don't gurantee exact result, only MAY BE, so it
useful only for speed up rewrite functions
5 GiST index support for @,~
6 rewrite():
select rewrite(orig, what, to);
select rewrite(ARRAY[orig, what, to]) from tsquery_table;
select rewrite(orig, 'select what, to from tsquery_table;');
7 significantly improve cover algorithm
2005-11-09 01:08:46 +08:00
|
|
|
/* reset istrue flag */
|
2005-11-23 02:17:34 +08:00
|
|
|
for (i = 0; i < query->size; i++)
|
|
|
|
{
|
|
|
|
if (item->type == VAL)
|
New features for tsearch2:
1 Comparison operation for tsquery
2 Btree index on tsquery
3 numnode(tsquery) - returns 'length' of tsquery
4 tsquery @ tsquery, tsquery ~ tsquery - contains, contained for tsquery.
Note: They don't gurantee exact result, only MAY BE, so it
useful only for speed up rewrite functions
5 GiST index support for @,~
6 rewrite():
select rewrite(orig, what, to);
select rewrite(ARRAY[orig, what, to]) from tsquery_table;
select rewrite(orig, 'select what, to from tsquery_table;');
7 significantly improve cover algorithm
2005-11-09 01:08:46 +08:00
|
|
|
item->istrue = 0;
|
|
|
|
item++;
|
|
|
|
}
|
|
|
|
}
|
2005-11-23 02:17:34 +08:00
|
|
|
|
2003-07-21 18:27:44 +08:00
|
|
|
static bool
|
2003-08-04 08:43:34 +08:00
|
|
|
Cover(DocRepresentation * doc, int len, QUERYTYPE * query, int *pos, int *p, int *q)
|
|
|
|
{
|
New features for tsearch2:
1 Comparison operation for tsquery
2 Btree index on tsquery
3 numnode(tsquery) - returns 'length' of tsquery
4 tsquery @ tsquery, tsquery ~ tsquery - contains, contained for tsquery.
Note: They don't gurantee exact result, only MAY BE, so it
useful only for speed up rewrite functions
5 GiST index support for @,~
6 rewrite():
select rewrite(orig, what, to);
select rewrite(ARRAY[orig, what, to]) from tsquery_table;
select rewrite(orig, 'select what, to from tsquery_table;');
7 significantly improve cover algorithm
2005-11-09 01:08:46 +08:00
|
|
|
DocRepresentation *ptr;
|
2003-08-04 08:43:34 +08:00
|
|
|
int lastpos = *pos;
|
2005-11-23 02:17:34 +08:00
|
|
|
int i;
|
|
|
|
bool found = false;
|
2003-08-04 08:43:34 +08:00
|
|
|
|
New features for tsearch2:
1 Comparison operation for tsquery
2 Btree index on tsquery
3 numnode(tsquery) - returns 'length' of tsquery
4 tsquery @ tsquery, tsquery ~ tsquery - contains, contained for tsquery.
Note: They don't gurantee exact result, only MAY BE, so it
useful only for speed up rewrite functions
5 GiST index support for @,~
6 rewrite():
select rewrite(orig, what, to);
select rewrite(ARRAY[orig, what, to]) from tsquery_table;
select rewrite(orig, 'select what, to from tsquery_table;');
7 significantly improve cover algorithm
2005-11-09 01:08:46 +08:00
|
|
|
reset_istrue_flag(query);
|
2005-11-23 02:17:34 +08:00
|
|
|
|
2003-08-04 08:43:34 +08:00
|
|
|
*p = 0x7fffffff;
|
|
|
|
*q = 0;
|
New features for tsearch2:
1 Comparison operation for tsquery
2 Btree index on tsquery
3 numnode(tsquery) - returns 'length' of tsquery
4 tsquery @ tsquery, tsquery ~ tsquery - contains, contained for tsquery.
Note: They don't gurantee exact result, only MAY BE, so it
useful only for speed up rewrite functions
5 GiST index support for @,~
6 rewrite():
select rewrite(orig, what, to);
select rewrite(ARRAY[orig, what, to]) from tsquery_table;
select rewrite(orig, 'select what, to from tsquery_table;');
7 significantly improve cover algorithm
2005-11-09 01:08:46 +08:00
|
|
|
ptr = doc + *pos;
|
|
|
|
|
|
|
|
/* find upper bound of cover from current position, move up */
|
2005-11-23 02:17:34 +08:00
|
|
|
while (ptr - doc < len)
|
|
|
|
{
|
|
|
|
for (i = 0; i < ptr->nitem; i++)
|
New features for tsearch2:
1 Comparison operation for tsquery
2 Btree index on tsquery
3 numnode(tsquery) - returns 'length' of tsquery
4 tsquery @ tsquery, tsquery ~ tsquery - contains, contained for tsquery.
Note: They don't gurantee exact result, only MAY BE, so it
useful only for speed up rewrite functions
5 GiST index support for @,~
6 rewrite():
select rewrite(orig, what, to);
select rewrite(ARRAY[orig, what, to]) from tsquery_table;
select rewrite(orig, 'select what, to from tsquery_table;');
7 significantly improve cover algorithm
2005-11-09 01:08:46 +08:00
|
|
|
ptr->item[i]->istrue = 1;
|
2005-11-23 02:17:34 +08:00
|
|
|
if (TS_execute(GETQUERY(query), NULL, false, checkcondition_ITEM))
|
|
|
|
{
|
|
|
|
if (ptr->pos > *q)
|
|
|
|
{
|
New features for tsearch2:
1 Comparison operation for tsquery
2 Btree index on tsquery
3 numnode(tsquery) - returns 'length' of tsquery
4 tsquery @ tsquery, tsquery ~ tsquery - contains, contained for tsquery.
Note: They don't gurantee exact result, only MAY BE, so it
useful only for speed up rewrite functions
5 GiST index support for @,~
6 rewrite():
select rewrite(orig, what, to);
select rewrite(ARRAY[orig, what, to]) from tsquery_table;
select rewrite(orig, 'select what, to from tsquery_table;');
7 significantly improve cover algorithm
2005-11-09 01:08:46 +08:00
|
|
|
*q = ptr->pos;
|
|
|
|
lastpos = ptr - doc;
|
|
|
|
found = true;
|
2005-11-23 02:17:34 +08:00
|
|
|
}
|
New features for tsearch2:
1 Comparison operation for tsquery
2 Btree index on tsquery
3 numnode(tsquery) - returns 'length' of tsquery
4 tsquery @ tsquery, tsquery ~ tsquery - contains, contained for tsquery.
Note: They don't gurantee exact result, only MAY BE, so it
useful only for speed up rewrite functions
5 GiST index support for @,~
6 rewrite():
select rewrite(orig, what, to);
select rewrite(ARRAY[orig, what, to]) from tsquery_table;
select rewrite(orig, 'select what, to from tsquery_table;');
7 significantly improve cover algorithm
2005-11-09 01:08:46 +08:00
|
|
|
break;
|
2003-07-21 18:27:44 +08:00
|
|
|
}
|
New features for tsearch2:
1 Comparison operation for tsquery
2 Btree index on tsquery
3 numnode(tsquery) - returns 'length' of tsquery
4 tsquery @ tsquery, tsquery ~ tsquery - contains, contained for tsquery.
Note: They don't gurantee exact result, only MAY BE, so it
useful only for speed up rewrite functions
5 GiST index support for @,~
6 rewrite():
select rewrite(orig, what, to);
select rewrite(ARRAY[orig, what, to]) from tsquery_table;
select rewrite(orig, 'select what, to from tsquery_table;');
7 significantly improve cover algorithm
2005-11-09 01:08:46 +08:00
|
|
|
ptr++;
|
2003-07-21 18:27:44 +08:00
|
|
|
}
|
|
|
|
|
2005-11-23 02:17:34 +08:00
|
|
|
if (!found)
|
2003-07-21 18:27:44 +08:00
|
|
|
return false;
|
|
|
|
|
New features for tsearch2:
1 Comparison operation for tsquery
2 Btree index on tsquery
3 numnode(tsquery) - returns 'length' of tsquery
4 tsquery @ tsquery, tsquery ~ tsquery - contains, contained for tsquery.
Note: They don't gurantee exact result, only MAY BE, so it
useful only for speed up rewrite functions
5 GiST index support for @,~
6 rewrite():
select rewrite(orig, what, to);
select rewrite(ARRAY[orig, what, to]) from tsquery_table;
select rewrite(orig, 'select what, to from tsquery_table;');
7 significantly improve cover algorithm
2005-11-09 01:08:46 +08:00
|
|
|
reset_istrue_flag(query);
|
2003-07-21 18:27:44 +08:00
|
|
|
|
New features for tsearch2:
1 Comparison operation for tsquery
2 Btree index on tsquery
3 numnode(tsquery) - returns 'length' of tsquery
4 tsquery @ tsquery, tsquery ~ tsquery - contains, contained for tsquery.
Note: They don't gurantee exact result, only MAY BE, so it
useful only for speed up rewrite functions
5 GiST index support for @,~
6 rewrite():
select rewrite(orig, what, to);
select rewrite(ARRAY[orig, what, to]) from tsquery_table;
select rewrite(orig, 'select what, to from tsquery_table;');
7 significantly improve cover algorithm
2005-11-09 01:08:46 +08:00
|
|
|
ptr = doc + lastpos;
|
2003-07-21 18:27:44 +08:00
|
|
|
|
New features for tsearch2:
1 Comparison operation for tsquery
2 Btree index on tsquery
3 numnode(tsquery) - returns 'length' of tsquery
4 tsquery @ tsquery, tsquery ~ tsquery - contains, contained for tsquery.
Note: They don't gurantee exact result, only MAY BE, so it
useful only for speed up rewrite functions
5 GiST index support for @,~
6 rewrite():
select rewrite(orig, what, to);
select rewrite(ARRAY[orig, what, to]) from tsquery_table;
select rewrite(orig, 'select what, to from tsquery_table;');
7 significantly improve cover algorithm
2005-11-09 01:08:46 +08:00
|
|
|
/* find lower bound of cover from founded upper bound, move down */
|
2005-11-23 02:17:34 +08:00
|
|
|
while (ptr >= doc)
|
|
|
|
{
|
|
|
|
for (i = 0; i < ptr->nitem; i++)
|
New features for tsearch2:
1 Comparison operation for tsquery
2 Btree index on tsquery
3 numnode(tsquery) - returns 'length' of tsquery
4 tsquery @ tsquery, tsquery ~ tsquery - contains, contained for tsquery.
Note: They don't gurantee exact result, only MAY BE, so it
useful only for speed up rewrite functions
5 GiST index support for @,~
6 rewrite():
select rewrite(orig, what, to);
select rewrite(ARRAY[orig, what, to]) from tsquery_table;
select rewrite(orig, 'select what, to from tsquery_table;');
7 significantly improve cover algorithm
2005-11-09 01:08:46 +08:00
|
|
|
ptr->item[i]->istrue = 1;
|
2005-11-23 02:17:34 +08:00
|
|
|
if (TS_execute(GETQUERY(query), NULL, true, checkcondition_ITEM))
|
|
|
|
{
|
|
|
|
if (ptr->pos < *p)
|
New features for tsearch2:
1 Comparison operation for tsquery
2 Btree index on tsquery
3 numnode(tsquery) - returns 'length' of tsquery
4 tsquery @ tsquery, tsquery ~ tsquery - contains, contained for tsquery.
Note: They don't gurantee exact result, only MAY BE, so it
useful only for speed up rewrite functions
5 GiST index support for @,~
6 rewrite():
select rewrite(orig, what, to);
select rewrite(ARRAY[orig, what, to]) from tsquery_table;
select rewrite(orig, 'select what, to from tsquery_table;');
7 significantly improve cover algorithm
2005-11-09 01:08:46 +08:00
|
|
|
*p = ptr->pos;
|
|
|
|
break;
|
2003-07-21 18:27:44 +08:00
|
|
|
}
|
New features for tsearch2:
1 Comparison operation for tsquery
2 Btree index on tsquery
3 numnode(tsquery) - returns 'length' of tsquery
4 tsquery @ tsquery, tsquery ~ tsquery - contains, contained for tsquery.
Note: They don't gurantee exact result, only MAY BE, so it
useful only for speed up rewrite functions
5 GiST index support for @,~
6 rewrite():
select rewrite(orig, what, to);
select rewrite(ARRAY[orig, what, to]) from tsquery_table;
select rewrite(orig, 'select what, to from tsquery_table;');
7 significantly improve cover algorithm
2005-11-09 01:08:46 +08:00
|
|
|
ptr--;
|
2003-07-21 18:27:44 +08:00
|
|
|
}
|
2003-08-04 08:43:34 +08:00
|
|
|
|
2005-11-23 02:17:34 +08:00
|
|
|
if (*p <= *q)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* set position for next try to next lexeme after begining of founded
|
|
|
|
* cover
|
|
|
|
*/
|
|
|
|
*pos = (ptr - doc) + 1;
|
New features for tsearch2:
1 Comparison operation for tsquery
2 Btree index on tsquery
3 numnode(tsquery) - returns 'length' of tsquery
4 tsquery @ tsquery, tsquery ~ tsquery - contains, contained for tsquery.
Note: They don't gurantee exact result, only MAY BE, so it
useful only for speed up rewrite functions
5 GiST index support for @,~
6 rewrite():
select rewrite(orig, what, to);
select rewrite(ARRAY[orig, what, to]) from tsquery_table;
select rewrite(orig, 'select what, to from tsquery_table;');
7 significantly improve cover algorithm
2005-11-09 01:08:46 +08:00
|
|
|
return true;
|
2003-07-21 18:27:44 +08:00
|
|
|
}
|
2003-08-04 08:43:34 +08:00
|
|
|
|
New features for tsearch2:
1 Comparison operation for tsquery
2 Btree index on tsquery
3 numnode(tsquery) - returns 'length' of tsquery
4 tsquery @ tsquery, tsquery ~ tsquery - contains, contained for tsquery.
Note: They don't gurantee exact result, only MAY BE, so it
useful only for speed up rewrite functions
5 GiST index support for @,~
6 rewrite():
select rewrite(orig, what, to);
select rewrite(ARRAY[orig, what, to]) from tsquery_table;
select rewrite(orig, 'select what, to from tsquery_table;');
7 significantly improve cover algorithm
2005-11-09 01:08:46 +08:00
|
|
|
(*pos)++;
|
2005-11-23 02:17:34 +08:00
|
|
|
return Cover(doc, len, query, pos, p, q);
|
2003-07-21 18:27:44 +08:00
|
|
|
}
|
|
|
|
|
2003-08-04 08:43:34 +08:00
|
|
|
static DocRepresentation *
|
|
|
|
get_docrep(tsvector * txt, QUERYTYPE * query, int *doclen)
|
|
|
|
{
|
|
|
|
ITEM *item = GETQUERY(query);
|
|
|
|
WordEntry *entry;
|
|
|
|
WordEntryPos *post;
|
|
|
|
int4 dimt,
|
|
|
|
j,
|
|
|
|
i;
|
|
|
|
int len = query->size * 4,
|
|
|
|
cur = 0;
|
2003-07-21 18:27:44 +08:00
|
|
|
DocRepresentation *doc;
|
|
|
|
|
2003-08-04 08:43:34 +08:00
|
|
|
*(uint16 *) POSNULL = lengthof(POSNULL) - 1;
|
|
|
|
doc = (DocRepresentation *) palloc(sizeof(DocRepresentation) * len);
|
New features for tsearch2:
1 Comparison operation for tsquery
2 Btree index on tsquery
3 numnode(tsquery) - returns 'length' of tsquery
4 tsquery @ tsquery, tsquery ~ tsquery - contains, contained for tsquery.
Note: They don't gurantee exact result, only MAY BE, so it
useful only for speed up rewrite functions
5 GiST index support for @,~
6 rewrite():
select rewrite(orig, what, to);
select rewrite(ARRAY[orig, what, to]) from tsquery_table;
select rewrite(orig, 'select what, to from tsquery_table;');
7 significantly improve cover algorithm
2005-11-09 01:08:46 +08:00
|
|
|
SortAndUniqOperand = GETOPERAND(query);
|
|
|
|
reset_istrue_flag(query);
|
|
|
|
|
2003-08-04 08:43:34 +08:00
|
|
|
for (i = 0; i < query->size; i++)
|
|
|
|
{
|
New features for tsearch2:
1 Comparison operation for tsquery
2 Btree index on tsquery
3 numnode(tsquery) - returns 'length' of tsquery
4 tsquery @ tsquery, tsquery ~ tsquery - contains, contained for tsquery.
Note: They don't gurantee exact result, only MAY BE, so it
useful only for speed up rewrite functions
5 GiST index support for @,~
6 rewrite():
select rewrite(orig, what, to);
select rewrite(ARRAY[orig, what, to]) from tsquery_table;
select rewrite(orig, 'select what, to from tsquery_table;');
7 significantly improve cover algorithm
2005-11-09 01:08:46 +08:00
|
|
|
if (item[i].type != VAL || item[i].istrue)
|
2003-07-21 18:27:44 +08:00
|
|
|
continue;
|
|
|
|
|
2003-08-04 08:43:34 +08:00
|
|
|
entry = find_wordentry(txt, query, &(item[i]));
|
|
|
|
if (!entry)
|
2003-07-21 18:27:44 +08:00
|
|
|
continue;
|
|
|
|
|
2003-08-04 08:43:34 +08:00
|
|
|
if (entry->haspos)
|
|
|
|
{
|
|
|
|
dimt = POSDATALEN(txt, entry);
|
|
|
|
post = POSDATAPTR(txt, entry);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dimt = *(uint16 *) POSNULL;
|
|
|
|
post = POSNULL + 1;
|
2003-07-21 18:27:44 +08:00
|
|
|
}
|
|
|
|
|
2003-08-04 08:43:34 +08:00
|
|
|
while (cur + dimt >= len)
|
|
|
|
{
|
|
|
|
len *= 2;
|
|
|
|
doc = (DocRepresentation *) repalloc(doc, sizeof(DocRepresentation) * len);
|
2003-07-21 18:27:44 +08:00
|
|
|
}
|
|
|
|
|
2003-08-04 08:43:34 +08:00
|
|
|
for (j = 0; j < dimt; j++)
|
|
|
|
{
|
2005-11-23 02:17:34 +08:00
|
|
|
if (j == 0)
|
|
|
|
{
|
|
|
|
ITEM *kptr,
|
|
|
|
*iptr = item + i;
|
|
|
|
int k;
|
|
|
|
|
New features for tsearch2:
1 Comparison operation for tsquery
2 Btree index on tsquery
3 numnode(tsquery) - returns 'length' of tsquery
4 tsquery @ tsquery, tsquery ~ tsquery - contains, contained for tsquery.
Note: They don't gurantee exact result, only MAY BE, so it
useful only for speed up rewrite functions
5 GiST index support for @,~
6 rewrite():
select rewrite(orig, what, to);
select rewrite(ARRAY[orig, what, to]) from tsquery_table;
select rewrite(orig, 'select what, to from tsquery_table;');
7 significantly improve cover algorithm
2005-11-09 01:08:46 +08:00
|
|
|
doc[cur].needfree = false;
|
|
|
|
doc[cur].nitem = 0;
|
2005-11-23 02:17:34 +08:00
|
|
|
doc[cur].item = (ITEM **) palloc(sizeof(ITEM *) * query->size);
|
New features for tsearch2:
1 Comparison operation for tsquery
2 Btree index on tsquery
3 numnode(tsquery) - returns 'length' of tsquery
4 tsquery @ tsquery, tsquery ~ tsquery - contains, contained for tsquery.
Note: They don't gurantee exact result, only MAY BE, so it
useful only for speed up rewrite functions
5 GiST index support for @,~
6 rewrite():
select rewrite(orig, what, to);
select rewrite(ARRAY[orig, what, to]) from tsquery_table;
select rewrite(orig, 'select what, to from tsquery_table;');
7 significantly improve cover algorithm
2005-11-09 01:08:46 +08:00
|
|
|
|
2005-11-23 02:17:34 +08:00
|
|
|
for (k = 0; k < query->size; k++)
|
|
|
|
{
|
|
|
|
kptr = item + k;
|
|
|
|
if (k == i || (item[k].type == VAL && compareITEM(&kptr, &iptr) == 0))
|
|
|
|
{
|
|
|
|
doc[cur].item[doc[cur].nitem] = item + k;
|
New features for tsearch2:
1 Comparison operation for tsquery
2 Btree index on tsquery
3 numnode(tsquery) - returns 'length' of tsquery
4 tsquery @ tsquery, tsquery ~ tsquery - contains, contained for tsquery.
Note: They don't gurantee exact result, only MAY BE, so it
useful only for speed up rewrite functions
5 GiST index support for @,~
6 rewrite():
select rewrite(orig, what, to);
select rewrite(ARRAY[orig, what, to]) from tsquery_table;
select rewrite(orig, 'select what, to from tsquery_table;');
7 significantly improve cover algorithm
2005-11-09 01:08:46 +08:00
|
|
|
doc[cur].nitem++;
|
|
|
|
kptr->istrue = 1;
|
|
|
|
}
|
2005-11-23 02:17:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
New features for tsearch2:
1 Comparison operation for tsquery
2 Btree index on tsquery
3 numnode(tsquery) - returns 'length' of tsquery
4 tsquery @ tsquery, tsquery ~ tsquery - contains, contained for tsquery.
Note: They don't gurantee exact result, only MAY BE, so it
useful only for speed up rewrite functions
5 GiST index support for @,~
6 rewrite():
select rewrite(orig, what, to);
select rewrite(ARRAY[orig, what, to]) from tsquery_table;
select rewrite(orig, 'select what, to from tsquery_table;');
7 significantly improve cover algorithm
2005-11-09 01:08:46 +08:00
|
|
|
doc[cur].needfree = false;
|
2005-11-23 02:17:34 +08:00
|
|
|
doc[cur].nitem = doc[cur - 1].nitem;
|
|
|
|
doc[cur].item = doc[cur - 1].item;
|
New features for tsearch2:
1 Comparison operation for tsquery
2 Btree index on tsquery
3 numnode(tsquery) - returns 'length' of tsquery
4 tsquery @ tsquery, tsquery ~ tsquery - contains, contained for tsquery.
Note: They don't gurantee exact result, only MAY BE, so it
useful only for speed up rewrite functions
5 GiST index support for @,~
6 rewrite():
select rewrite(orig, what, to);
select rewrite(ARRAY[orig, what, to]) from tsquery_table;
select rewrite(orig, 'select what, to from tsquery_table;');
7 significantly improve cover algorithm
2005-11-09 01:08:46 +08:00
|
|
|
}
|
2005-01-25 20:36:25 +08:00
|
|
|
doc[cur].pos = WEP_GETPOS(post[j]);
|
2003-07-21 18:27:44 +08:00
|
|
|
cur++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-08-04 08:43:34 +08:00
|
|
|
*doclen = cur;
|
|
|
|
|
|
|
|
if (cur > 0)
|
|
|
|
{
|
|
|
|
if (cur > 1)
|
2003-07-21 18:27:44 +08:00
|
|
|
qsort((void *) doc, cur, sizeof(DocRepresentation), compareDocR);
|
|
|
|
return doc;
|
|
|
|
}
|
2003-08-04 08:43:34 +08:00
|
|
|
|
2003-07-21 18:27:44 +08:00
|
|
|
pfree(doc);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Datum
|
2003-08-04 08:43:34 +08:00
|
|
|
rank_cd(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
int K = PG_GETARG_INT32(0);
|
|
|
|
tsvector *txt = (tsvector *) PG_DETOAST_DATUM(PG_GETARG_DATUM(1));
|
New features for tsearch2:
1 Comparison operation for tsquery
2 Btree index on tsquery
3 numnode(tsquery) - returns 'length' of tsquery
4 tsquery @ tsquery, tsquery ~ tsquery - contains, contained for tsquery.
Note: They don't gurantee exact result, only MAY BE, so it
useful only for speed up rewrite functions
5 GiST index support for @,~
6 rewrite():
select rewrite(orig, what, to);
select rewrite(ARRAY[orig, what, to]) from tsquery_table;
select rewrite(orig, 'select what, to from tsquery_table;');
7 significantly improve cover algorithm
2005-11-09 01:08:46 +08:00
|
|
|
QUERYTYPE *query = (QUERYTYPE *) PG_DETOAST_DATUM_COPY(PG_GETARG_DATUM(2));
|
2003-08-04 08:43:34 +08:00
|
|
|
int method = DEF_NORM_METHOD;
|
|
|
|
DocRepresentation *doc;
|
|
|
|
float res = 0.0;
|
|
|
|
int p = 0,
|
|
|
|
q = 0,
|
|
|
|
len,
|
New features for tsearch2:
1 Comparison operation for tsquery
2 Btree index on tsquery
3 numnode(tsquery) - returns 'length' of tsquery
4 tsquery @ tsquery, tsquery ~ tsquery - contains, contained for tsquery.
Note: They don't gurantee exact result, only MAY BE, so it
useful only for speed up rewrite functions
5 GiST index support for @,~
6 rewrite():
select rewrite(orig, what, to);
select rewrite(ARRAY[orig, what, to]) from tsquery_table;
select rewrite(orig, 'select what, to from tsquery_table;');
7 significantly improve cover algorithm
2005-11-09 01:08:46 +08:00
|
|
|
cur,
|
|
|
|
i,
|
2005-11-23 02:17:34 +08:00
|
|
|
doclen = 0;
|
2003-07-21 18:27:44 +08:00
|
|
|
|
New features for tsearch2:
1 Comparison operation for tsquery
2 Btree index on tsquery
3 numnode(tsquery) - returns 'length' of tsquery
4 tsquery @ tsquery, tsquery ~ tsquery - contains, contained for tsquery.
Note: They don't gurantee exact result, only MAY BE, so it
useful only for speed up rewrite functions
5 GiST index support for @,~
6 rewrite():
select rewrite(orig, what, to);
select rewrite(ARRAY[orig, what, to]) from tsquery_table;
select rewrite(orig, 'select what, to from tsquery_table;');
7 significantly improve cover algorithm
2005-11-09 01:08:46 +08:00
|
|
|
doc = get_docrep(txt, query, &doclen);
|
2003-08-04 08:43:34 +08:00
|
|
|
if (!doc)
|
|
|
|
{
|
2003-07-21 18:27:44 +08:00
|
|
|
PG_FREE_IF_COPY(txt, 1);
|
|
|
|
PG_FREE_IF_COPY(query, 2);
|
|
|
|
PG_RETURN_FLOAT4(0.0);
|
|
|
|
}
|
|
|
|
|
2003-08-04 08:43:34 +08:00
|
|
|
cur = 0;
|
|
|
|
if (K <= 0)
|
|
|
|
K = 4;
|
New features for tsearch2:
1 Comparison operation for tsquery
2 Btree index on tsquery
3 numnode(tsquery) - returns 'length' of tsquery
4 tsquery @ tsquery, tsquery ~ tsquery - contains, contained for tsquery.
Note: They don't gurantee exact result, only MAY BE, so it
useful only for speed up rewrite functions
5 GiST index support for @,~
6 rewrite():
select rewrite(orig, what, to);
select rewrite(ARRAY[orig, what, to]) from tsquery_table;
select rewrite(orig, 'select what, to from tsquery_table;');
7 significantly improve cover algorithm
2005-11-09 01:08:46 +08:00
|
|
|
while (Cover(doc, doclen, query, &cur, &p, &q))
|
2003-08-04 08:43:34 +08:00
|
|
|
res += (q - p + 1 > K) ? ((float) K) / ((float) (q - p + 1)) : 1.0;
|
|
|
|
|
|
|
|
if (PG_NARGS() == 4)
|
|
|
|
method = PG_GETARG_INT32(3);
|
|
|
|
|
|
|
|
switch (method)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
case 1:
|
2005-10-15 10:49:52 +08:00
|
|
|
res /= log((float) (cnt_length(txt) + 1));
|
2003-08-04 08:43:34 +08:00
|
|
|
break;
|
|
|
|
case 2:
|
2005-06-01 19:45:03 +08:00
|
|
|
len = cnt_length(txt);
|
2005-10-15 10:49:52 +08:00
|
|
|
if (len > 0)
|
|
|
|
res /= (float) len;
|
2003-08-04 08:43:34 +08:00
|
|
|
break;
|
2003-07-21 18:27:44 +08:00
|
|
|
default:
|
2003-08-04 08:43:34 +08:00
|
|
|
/* internal error */
|
|
|
|
elog(ERROR, "unrecognized normalization method: %d", method);
|
|
|
|
}
|
2003-07-21 18:27:44 +08:00
|
|
|
|
2005-11-23 02:17:34 +08:00
|
|
|
for (i = 0; i < doclen; i++)
|
|
|
|
if (doc[i].needfree)
|
|
|
|
pfree(doc[i].item);
|
2003-07-21 18:27:44 +08:00
|
|
|
pfree(doc);
|
|
|
|
PG_FREE_IF_COPY(txt, 1);
|
|
|
|
PG_FREE_IF_COPY(query, 2);
|
|
|
|
|
|
|
|
PG_RETURN_FLOAT4(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Datum
|
2003-08-04 08:43:34 +08:00
|
|
|
rank_cd_def(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
PG_RETURN_DATUM(DirectFunctionCall4(
|
|
|
|
rank_cd,
|
|
|
|
Int32GetDatum(-1),
|
|
|
|
PG_GETARG_DATUM(0),
|
|
|
|
PG_GETARG_DATUM(1),
|
2005-10-15 10:49:52 +08:00
|
|
|
(PG_NARGS() == 3) ? PG_GETARG_DATUM(2) : Int32GetDatum(DEF_NORM_METHOD)
|
2003-08-04 08:43:34 +08:00
|
|
|
));
|
2003-07-21 18:27:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**************debug*************/
|
|
|
|
|
2003-08-04 08:43:34 +08:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
char *w;
|
|
|
|
int2 len;
|
|
|
|
int2 pos;
|
|
|
|
int2 start;
|
|
|
|
int2 finish;
|
|
|
|
} DocWord;
|
2003-07-21 18:27:44 +08:00
|
|
|
|
|
|
|
static int
|
2003-08-04 08:43:34 +08:00
|
|
|
compareDocWord(const void *a, const void *b)
|
|
|
|
{
|
|
|
|
if (((DocWord *) a)->pos == ((DocWord *) b)->pos)
|
2005-03-31 23:08:08 +08:00
|
|
|
return 0;
|
2003-08-04 08:43:34 +08:00
|
|
|
return (((DocWord *) a)->pos > ((DocWord *) b)->pos) ? 1 : -1;
|
2003-07-21 18:27:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-08-04 08:43:34 +08:00
|
|
|
Datum
|
|
|
|
get_covers(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
tsvector *txt = (tsvector *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
|
New features for tsearch2:
1 Comparison operation for tsquery
2 Btree index on tsquery
3 numnode(tsquery) - returns 'length' of tsquery
4 tsquery @ tsquery, tsquery ~ tsquery - contains, contained for tsquery.
Note: They don't gurantee exact result, only MAY BE, so it
useful only for speed up rewrite functions
5 GiST index support for @,~
6 rewrite():
select rewrite(orig, what, to);
select rewrite(ARRAY[orig, what, to]) from tsquery_table;
select rewrite(orig, 'select what, to from tsquery_table;');
7 significantly improve cover algorithm
2005-11-09 01:08:46 +08:00
|
|
|
QUERYTYPE *query = (QUERYTYPE *) PG_DETOAST_DATUM_COPY(PG_GETARG_DATUM(1));
|
2003-08-04 08:43:34 +08:00
|
|
|
WordEntry *pptr = ARRPTR(txt);
|
|
|
|
int i,
|
|
|
|
dlen = 0,
|
|
|
|
j,
|
|
|
|
cur = 0,
|
|
|
|
len = 0,
|
|
|
|
rlen;
|
|
|
|
DocWord *dw,
|
|
|
|
*dwptr;
|
|
|
|
text *out;
|
|
|
|
char *cptr;
|
2003-07-21 18:27:44 +08:00
|
|
|
DocRepresentation *doc;
|
2003-08-04 08:43:34 +08:00
|
|
|
int pos = 0,
|
|
|
|
p,
|
|
|
|
q,
|
|
|
|
olddwpos = 0;
|
|
|
|
int ncover = 1;
|
2003-07-21 18:27:44 +08:00
|
|
|
|
|
|
|
doc = get_docrep(txt, query, &rlen);
|
|
|
|
|
2003-08-04 08:43:34 +08:00
|
|
|
if (!doc)
|
|
|
|
{
|
|
|
|
out = palloc(VARHDRSZ);
|
2003-07-21 18:27:44 +08:00
|
|
|
VARATT_SIZEP(out) = VARHDRSZ;
|
2003-08-04 08:43:34 +08:00
|
|
|
PG_FREE_IF_COPY(txt, 0);
|
|
|
|
PG_FREE_IF_COPY(query, 1);
|
2003-07-21 18:27:44 +08:00
|
|
|
PG_RETURN_POINTER(out);
|
|
|
|
}
|
|
|
|
|
2003-08-04 08:43:34 +08:00
|
|
|
for (i = 0; i < txt->size; i++)
|
|
|
|
{
|
2003-07-21 18:27:44 +08:00
|
|
|
if (!pptr[i].haspos)
|
2003-07-25 01:52:50 +08:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_SYNTAX_ERROR),
|
|
|
|
errmsg("no pos info")));
|
2003-08-04 08:43:34 +08:00
|
|
|
dlen += POSDATALEN(txt, &(pptr[i]));
|
2003-07-21 18:27:44 +08:00
|
|
|
}
|
|
|
|
|
2003-08-04 08:43:34 +08:00
|
|
|
dwptr = dw = palloc(sizeof(DocWord) * dlen);
|
|
|
|
memset(dw, 0, sizeof(DocWord) * dlen);
|
|
|
|
|
|
|
|
for (i = 0; i < txt->size; i++)
|
|
|
|
{
|
|
|
|
WordEntryPos *posdata = POSDATAPTR(txt, &(pptr[i]));
|
2003-07-21 18:27:44 +08:00
|
|
|
|
2003-08-04 08:43:34 +08:00
|
|
|
for (j = 0; j < POSDATALEN(txt, &(pptr[i])); j++)
|
|
|
|
{
|
|
|
|
dw[cur].w = STRPTR(txt) + pptr[i].pos;
|
|
|
|
dw[cur].len = pptr[i].len;
|
2005-01-25 20:36:25 +08:00
|
|
|
dw[cur].pos = WEP_GETPOS(posdata[j]);
|
2003-07-21 18:27:44 +08:00
|
|
|
cur++;
|
|
|
|
}
|
2003-08-04 08:43:34 +08:00
|
|
|
len += (pptr[i].len + 1) * (int) POSDATALEN(txt, &(pptr[i]));
|
2003-07-21 18:27:44 +08:00
|
|
|
}
|
|
|
|
qsort((void *) dw, dlen, sizeof(DocWord), compareDocWord);
|
|
|
|
|
2003-08-04 08:43:34 +08:00
|
|
|
while (Cover(doc, rlen, query, &pos, &p, &q))
|
|
|
|
{
|
|
|
|
dwptr = dw + olddwpos;
|
|
|
|
while (dwptr->pos < p && dwptr - dw < dlen)
|
2003-07-21 18:27:44 +08:00
|
|
|
dwptr++;
|
2003-08-04 08:43:34 +08:00
|
|
|
olddwpos = dwptr - dw;
|
|
|
|
dwptr->start = ncover;
|
|
|
|
while (dwptr->pos < q + 1 && dwptr - dw < dlen)
|
2003-07-21 18:27:44 +08:00
|
|
|
dwptr++;
|
2003-08-04 08:43:34 +08:00
|
|
|
(dwptr - 1)->finish = ncover;
|
|
|
|
len += 4 /* {}+two spaces */ + 2 * 16 /* numbers */ ;
|
|
|
|
ncover++;
|
|
|
|
}
|
|
|
|
|
|
|
|
out = palloc(VARHDRSZ + len);
|
|
|
|
cptr = ((char *) out) + VARHDRSZ;
|
|
|
|
dwptr = dw;
|
|
|
|
|
|
|
|
while (dwptr - dw < dlen)
|
|
|
|
{
|
|
|
|
if (dwptr->start)
|
|
|
|
{
|
|
|
|
sprintf(cptr, "{%d ", dwptr->start);
|
|
|
|
cptr = strchr(cptr, '\0');
|
2003-07-21 18:27:44 +08:00
|
|
|
}
|
2003-08-04 08:43:34 +08:00
|
|
|
memcpy(cptr, dwptr->w, dwptr->len);
|
|
|
|
cptr += dwptr->len;
|
|
|
|
*cptr = ' ';
|
2003-07-21 18:27:44 +08:00
|
|
|
cptr++;
|
2003-08-04 08:43:34 +08:00
|
|
|
if (dwptr->finish)
|
|
|
|
{
|
|
|
|
sprintf(cptr, "}%d ", dwptr->finish);
|
|
|
|
cptr = strchr(cptr, '\0');
|
2003-07-21 18:27:44 +08:00
|
|
|
}
|
|
|
|
dwptr++;
|
2003-08-04 08:43:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
VARATT_SIZEP(out) = cptr - ((char *) out);
|
2003-07-21 18:27:44 +08:00
|
|
|
|
|
|
|
pfree(dw);
|
2005-11-23 02:17:34 +08:00
|
|
|
for (i = 0; i < rlen; i++)
|
|
|
|
if (doc[i].needfree)
|
|
|
|
pfree(doc[i].item);
|
2003-07-21 18:27:44 +08:00
|
|
|
pfree(doc);
|
|
|
|
|
2003-08-04 08:43:34 +08:00
|
|
|
PG_FREE_IF_COPY(txt, 0);
|
|
|
|
PG_FREE_IF_COPY(query, 1);
|
2003-07-21 18:27:44 +08:00
|
|
|
PG_RETURN_POINTER(out);
|
|
|
|
}
|