]> granicus.if.org Git - postgresql/commitdiff
More accuracy work with MinWords option of headline function
authorTeodor Sigaev <teodor@sigaev.ru>
Wed, 13 Aug 2003 14:37:55 +0000 (14:37 +0000)
committerTeodor Sigaev <teodor@sigaev.ru>
Wed, 13 Aug 2003 14:37:55 +0000 (14:37 +0000)
contrib/tsearch2/wparser_def.c

index 4680d746b3e4fc8f489053aee144f6095dc71e00..8937bbc49fed973e00f07ffdf3115d5c61c6bd16 100644 (file)
@@ -189,7 +189,7 @@ prsd_headline(PG_FUNCTION_ARGS)
        int                     bestb = -1,
                                beste = -1;
        int                     bestlen = -1;
-       int                     pose = 0,
+       int                     pose = 0, posb,
                                poslen,
                                curlen;
 
@@ -229,15 +229,15 @@ prsd_headline(PG_FUNCTION_ARGS)
                if (min_words >= max_words)
                        ereport(ERROR,
                                        (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
-                                        errmsg("must be MinWords < MaxWords")));
+                                        errmsg("MinWords must be less than MaxWords")));
                if (min_words <= 0)
                        ereport(ERROR,
                                        (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
-                                        errmsg("must be MinWords > 0")));
+                                        errmsg("MinWords should be positive")));
                if (shortword < 0)
                        ereport(ERROR,
                                        (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
-                                        errmsg("must be ShortWord >= 0")));
+                                        errmsg("ShortWord hould be = 0")));
        }
 
        while (hlCover(prs, query, &p, &q))
@@ -261,6 +261,7 @@ prsd_headline(PG_FUNCTION_ARGS)
                        continue;
                }
 
+               posb=p;
                if (curlen < max_words)
                {                                               /* find good end */
                        for (i = i - 1; i < prs->curwords && curlen < max_words; i++)
@@ -278,6 +279,19 @@ prsd_headline(PG_FUNCTION_ARGS)
                                if (curlen >= min_words)
                                        break;
                        }
+                       if ( curlen < min_words && i>=prs->curwords ) { /* got end of text and our cover is shoter than min_words */
+                               for(i=p; i>= 0; i--) {
+                                       if (!NONWORDTOKEN(prs->words[i].type))
+                                               curlen++;
+                                       if (prs->words[i].item && !prs->words[i].repeated)
+                                               poslen++;
+                                       if (NOENDTOKEN(prs->words[i].type) || prs->words[i].len <= shortword)
+                                               continue;
+                                       if (curlen >= min_words)
+                                               break;
+                               }
+                               posb=(i>=0) ? i : 0;
+                       }
                }
                else
                {                                               /* shorter cover :((( */
@@ -298,7 +312,7 @@ prsd_headline(PG_FUNCTION_ARGS)
                        (bestlen >= 0 && !(NOENDTOKEN(prs->words[pose].type) || prs->words[pose].len <= shortword) &&
                         (NOENDTOKEN(prs->words[beste].type) || prs->words[beste].len <= shortword)))
                {
-                       bestb = p;
+                       bestb = posb;
                        beste = pose;
                        bestlen = poslen;
                }