24 static const float weights[] = {0.1f, 0.2f, 0.4f, 1.0f};
26 #define wpos(wep) ( w[ WEP_GETWEIGHT(wep) ] )
28 #define RANK_NO_NORM 0x00
29 #define RANK_NORM_LOGLENGTH 0x01
30 #define RANK_NORM_LENGTH 0x02
31 #define RANK_NORM_EXTDIST 0x04
32 #define RANK_NORM_UNIQ 0x08
33 #define RANK_NORM_LOGUNIQ 0x10
34 #define RANK_NORM_RDIVRPLUS1 0x20
35 #define DEF_NORM_METHOD RANK_NO_NORM
49 return 1.0 / (1.005 + 0.05 * exp(((
float4) w) / 1.5 - 2));
75 #define WordECompareQueryItem(e,q,p,i,m) \
76 tsCompareString((q) + (i)->distance, (i)->length, \
77 (e) + (p)->pos, (p)->len, (m))
96 while (StopLow < StopHigh)
98 StopMiddle = StopLow + (StopHigh - StopLow) / 2;
102 StopHigh = StopMiddle;
107 StopLow = StopMiddle + 1;
109 StopHigh = StopMiddle;
114 if (StopLow >= StopHigh)
115 StopMiddle = StopHigh;
127 return (*nitem > 0) ? StopHigh : NULL;
137 char *operand = (
char *)
arg;
185 while (ptr -
res < *size)
195 *size = prevptr + 1 -
res;
235 for (
i = 0;
i < size;
i++)
241 while (entry - firstentry < nitem)
250 for (k = 0; k <
i; k++)
254 lenct = pos[k]->
npos;
256 for (l = 0; l < dimt; l++)
258 for (p = 0; p < lenct; p++)
261 if (dist || (dist == 0 && (pos[
i] == POSNULL || pos[k] == POSNULL)))
268 res = (
res < 0) ? curw : 1.0 - (1.0 -
res) * (1.0 - curw);
303 for (
i = 0;
i < size;
i++)
313 while (entry - firstentry < nitem)
329 for (
j = 0;
j < dimt;
j++)
331 resj = resj +
wpos(post[
j]) / ((
j + 1) * (
j + 1));
332 if (
wpos(post[
j]) > wjm)
345 res =
res + (wjm + resj - wjm / ((jm + 1) * (jm + 1))) / 1.64493406685;
391 res /= log((
double) (t->
size + 1)) / log(2.0);
411 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
412 errmsg(
"array of weight must be one-dimensional")));
416 (
errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
417 errmsg(
"array of weight is too short")));
421 (
errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
422 errmsg(
"array of weight must not contain nulls")));
427 ws[
i] = (arrdata[
i] >= 0) ? arrdata[
i] :
weights[
i];
430 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
431 errmsg(
"weight out of range")));
528 if (
a->data.map.entry ==
b->data.map.entry)
531 return (
a->data.map.entry >
b->data.map.entry) ? 1 : -1;
540 #define MAXQROPOS MAXENTRYPOS
556 #define QR_GET_OPERAND_DATA(q, v) \
557 ( (q)->operandData + (((QueryItem*)(v)) - GETQUERY((q)->query)) )
621 if (opData->
npos == 0)
624 opData->
pos[lastPos] = entry->
pos;
639 opData->
pos[lastPos] = entry->
pos;
649 int lastpos = ext->
pos;
662 ptr = doc + ext->
pos;
665 while (ptr - doc <
len)
692 while (ptr >= doc + ext->
pos)
712 if (ext->
p <= ext->
q)
718 ext->
pos = (ptr - doc) + 1;
761 while (entry - firstentry < nitem)
782 for (
j = 0;
j < dimt;
j++)
784 if (curoperand->
weight == 0 ||
817 while (rptr - doc <
cur)
819 if (rptr->
pos == (rptr - 1)->pos &&
820 rptr->
data.
map.entry == (rptr - 1)->data.map.entry)
841 *doclen = wptr - doc;
859 double SumDist = 0.0,
867 invws[
i] = ((double) ((arrdata[
i] >= 0) ? arrdata[
i] :
weights[
i]));
870 (
errcode(ERRCODE_INVALID_PARAMETER_VALUE),
871 errmsg(
"weight out of range")));
872 invws[
i] = 1.0 / invws[
i];
887 while (
Cover(doc, doclen, &qr, &ext))
895 while (ptr <= ext.
end)
901 Cpos = ((double) (ext.
end - ext.
begin + 1)) / InvSum;
908 nNoise = (ext.
q - ext.
p) - (ext.
end - ext.
begin);
911 Wdoc += Cpos / ((double) (1 + nNoise));
913 CurExtPos = ((double) (ext.
q + ext.
p)) / 2.0;
914 if (NExtent > 0 && CurExtPos > PrevExtPos
917 SumDist += 1.0 / (CurExtPos - PrevExtPos);
919 PrevExtPos = CurExtPos;
930 Wdoc /= (double)
len;
934 Wdoc /= ((
double) NExtent) / SumDist;
937 Wdoc /= (
double) (txt->
size);
940 Wdoc /= log((
double) (txt->
size + 1)) / log(2.0);
bool array_contains_nulls(ArrayType *array)
int ArrayGetNItems(int ndim, const int *dims)
#define MemSet(start, val, len)
static void PGresult * res
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereport(elevel,...)
#define PG_FREE_IF_COPY(ptr, n)
#define PG_GETARG_DATUM(n)
#define PG_DETOAST_DATUM(datum)
#define PG_GETARG_INT32(n)
#define PG_RETURN_FLOAT4(x)
Datum difference(PG_FUNCTION_ARGS)
void pfree(void *pointer)
void * palloc0(Size size)
void * repalloc(void *pointer, Size size)
void qsort_arg(void *base, size_t nel, size_t elsize, qsort_arg_comparator cmp, void *arg)
#define qsort(a, b, c, d)
void check_stack_depth(void)
DocRepresentation * begin
struct DocRepresentation::@27::@29 map
union DocRepresentation::@27 data
struct DocRepresentation::@27::@28 query
WordEntryPos pos[MAXQROPOS]
QueryRepresentationOperand * operandData
WordEntryPos pos[FLEXIBLE_ARRAY_MEMBER]
#define PG_GETARG_TSVECTOR(n)
#define PG_GETARG_TSQUERY(n)
#define RANK_NORM_RDIVRPLUS1
Datum ts_rankcd_wtt(PG_FUNCTION_ARGS)
Datum ts_rankcd_wttf(PG_FUNCTION_ARGS)
#define WordECompareQueryItem(e, q, p, i, m)
static int compareDocR(const void *va, const void *vb)
#define RANK_NORM_LOGLENGTH
static TSTernaryValue checkcondition_QueryOperand(void *checkval, QueryOperand *val, ExecPhraseData *data)
static void resetQueryRepresentation(QueryRepresentation *qr, bool reverseinsert)
static float4 calc_rank_cd(const float4 *arrdata, TSVector txt, TSQuery query, int method)
static float calc_rank(const float *w, TSVector t, TSQuery q, int32 method)
static int cnt_length(TSVector t)
static float4 word_distance(int32 w)
static DocRepresentation * get_docrep(TSVector txt, QueryRepresentation *qr, int *doclen)
static int compareQueryOperand(const void *a, const void *b, void *arg)
Datum ts_rank_ttf(PG_FUNCTION_ARGS)
Datum ts_rank_wttf(PG_FUNCTION_ARGS)
static const float weights[]
Datum ts_rank_tt(PG_FUNCTION_ARGS)
static WordEntry * find_wordentry(TSVector t, TSQuery q, QueryOperand *item, int32 *nitem)
static QueryOperand ** SortAndUniqItems(TSQuery q, int *size)
static float calc_rank_or(const float *w, TSVector t, TSQuery q)
static void fillQueryRepresentationData(QueryRepresentation *qr, DocRepresentation *entry)
Datum ts_rank_wtt(PG_FUNCTION_ARGS)
static const float * getWeights(ArrayType *win)
#define QR_GET_OPERAND_DATA(q, v)
Datum ts_rankcd_ttf(PG_FUNCTION_ARGS)
Datum ts_rankcd_tt(PG_FUNCTION_ARGS)
#define RANK_NORM_EXTDIST
static float calc_rank_and(const float *w, TSVector t, TSQuery q)
static bool Cover(DocRepresentation *doc, int len, QueryRepresentation *qr, CoverExt *ext)
#define RANK_NORM_LOGUNIQ
bool TS_execute(QueryItem *curitem, void *arg, uint32 flags, TSExecuteCallback chkcond)
int32 tsCompareString(char *a, int lena, char *b, int lenb, bool prefix)