Skip to content

Commit 61366a9

Browse files
committed
More accuracy works with stopwords in queries
1 parent d1031cd commit 61366a9

File tree

5 files changed

+39
-56
lines changed

5 files changed

+39
-56
lines changed

contrib/tsearch2/expected/tsearch2.out

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,30 @@ select to_tsquery('default', '\'the wether\':dc & \' sKies \':BC ');
569569
'wether':CD & 'sky':BC
570570
(1 row)
571571

572+
select to_tsquery('asd&(and|fghj)');
573+
to_tsquery
574+
----------------
575+
'asd' & 'fghj'
576+
(1 row)
577+
578+
select to_tsquery('(asd&and)|fghj');
579+
to_tsquery
580+
----------------
581+
'asd' | 'fghj'
582+
(1 row)
583+
584+
select to_tsquery('(asd&!and)|fghj');
585+
to_tsquery
586+
----------------
587+
'asd' | 'fghj'
588+
(1 row)
589+
590+
select to_tsquery('(the|and&(i&1))&fghj');
591+
to_tsquery
592+
--------------
593+
'1' & 'fghj'
594+
(1 row)
595+
572596
select 'a b:89 ca:23A,64b d:34c'::tsvector @@ 'd:AC & ca';
573597
?column?
574598
----------

contrib/tsearch2/query.c

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,6 @@ Datum to_tsquery_name(PG_FUNCTION_ARGS);
5252
PG_FUNCTION_INFO_V1(to_tsquery_current);
5353
Datum to_tsquery_current(PG_FUNCTION_ARGS);
5454

55-
#define END 0
56-
#define ERR 1
57-
#define VAL 2
58-
#define OPR 3
59-
#define OPEN 4
60-
#define CLOSE 5
61-
#define VALTRUE 6 /* for stop words */
62-
#define VALFALSE 7
63-
6455
/* parser's states */
6556
#define WAITOPERAND 1
6657
#define WAITOPERATOR 2
@@ -293,7 +284,7 @@ pushval_morph(QPRS_STATE * state, int typeval, char *strval, int lenval, int2 we
293284

294285
/* XXX */
295286
if (prs.curwords == 0)
296-
pushval_asis(state, VALTRUE, 0, 0, 0);
287+
pushval_asis(state, VALSTOP, 0, 0, 0);
297288
}
298289

299290
#define STACKDEPTH 32
@@ -526,7 +517,7 @@ findoprnd(ITEM * ptr, int4 *pos)
526517
elog(DEBUG3, (ptr[*pos].type == OPR) ?
527518
"%d %c" : "%d %d", *pos, ptr[*pos].val);
528519
#endif
529-
if (ptr[*pos].type == VAL || ptr[*pos].type == VALTRUE)
520+
if (ptr[*pos].type == VAL || ptr[*pos].type == VALSTOP)
530521
{
531522
ptr[*pos].left = 0;
532523
(*pos)++;

contrib/tsearch2/query.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ typedef struct
4646
#define OPR 3
4747
#define OPEN 4
4848
#define CLOSE 5
49-
#define VALTRUE 6 /* for stop words */
50-
#define VALFALSE 7
49+
#define VALSTOP 6 /* for stop words */
5150

5251
bool TS_execute(ITEM * curitem, void *checkval,
5352
bool calcnot, bool (*chkcond) (void *checkval, ITEM * val));

contrib/tsearch2/rewrite.c

Lines changed: 8 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ clean_NOT_v2(ITEM * ptr, int4 *len)
177177
#define V_UNKNOWN 0
178178
#define V_TRUE 1
179179
#define V_FALSE 2
180+
#define V_STOP 3
180181

181182
/*
182183
* Clean query tree from values which is always in
@@ -190,10 +191,10 @@ clean_fakeval_intree(NODE * node, char *result)
190191

191192
if (node->valnode->type == VAL)
192193
return node;
193-
else if (node->valnode->type == VALTRUE)
194+
else if (node->valnode->type == VALSTOP)
194195
{
195196
pfree(node);
196-
*result = V_TRUE;
197+
*result = V_STOP;
197198
return NULL;
198199
}
199200

@@ -203,65 +204,29 @@ clean_fakeval_intree(NODE * node, char *result)
203204
node->right = clean_fakeval_intree(node->right, &rresult);
204205
if (!node->right)
205206
{
206-
*result = (rresult == V_TRUE) ? V_FALSE : V_TRUE;
207+
*result = V_STOP;
207208
freetree(node);
208209
return NULL;
209210
}
210211
}
211-
else if (node->valnode->val == (int4) '|')
212-
{
213-
NODE *res = node;
214-
215-
node->left = clean_fakeval_intree(node->left, &lresult);
216-
node->right = clean_fakeval_intree(node->right, &rresult);
217-
if (lresult == V_TRUE || rresult == V_TRUE)
218-
{
219-
freetree(node);
220-
*result = V_TRUE;
221-
return NULL;
222-
}
223-
else if (lresult == V_FALSE && rresult == V_FALSE)
224-
{
225-
freetree(node);
226-
*result = V_FALSE;
227-
return NULL;
228-
}
229-
else if (lresult == V_FALSE)
230-
{
231-
res = node->right;
232-
pfree(node);
233-
}
234-
else if (rresult == V_FALSE)
235-
{
236-
res = node->left;
237-
pfree(node);
238-
}
239-
return res;
240-
}
241212
else
242213
{
243214
NODE *res = node;
244215

245216
node->left = clean_fakeval_intree(node->left, &lresult);
246217
node->right = clean_fakeval_intree(node->right, &rresult);
247-
if (lresult == V_FALSE || rresult == V_FALSE)
248-
{
249-
freetree(node);
250-
*result = V_FALSE;
251-
return NULL;
252-
}
253-
else if (lresult == V_TRUE && rresult == V_TRUE)
218+
if (lresult == V_STOP && rresult == V_STOP)
254219
{
255220
freetree(node);
256-
*result = V_TRUE;
221+
*result = V_STOP;
257222
return NULL;
258223
}
259-
else if (lresult == V_TRUE)
224+
else if (lresult == V_STOP)
260225
{
261226
res = node->right;
262227
pfree(node);
263228
}
264-
else if (rresult == V_TRUE)
229+
else if (rresult == V_STOP)
265230
{
266231
res = node->left;
267232
pfree(node);

contrib/tsearch2/sql/tsearch2.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ SELECT length(to_tsvector('default', '345 qwe@efd.r \' http://www.com/ http://ae
8787
select to_tsquery('default', 'qwe & sKies ');
8888
select to_tsquery('simple', 'qwe & sKies ');
8989
select to_tsquery('default', '\'the wether\':dc & \' sKies \':BC ');
90+
select to_tsquery('asd&(and|fghj)');
91+
select to_tsquery('(asd&and)|fghj');
92+
select to_tsquery('(asd&!and)|fghj');
93+
select to_tsquery('(the|and&(i&1))&fghj');
9094
select 'a b:89 ca:23A,64b d:34c'::tsvector @@ 'd:AC & ca';
9195
select 'a b:89 ca:23A,64b d:34c'::tsvector @@ 'd:AC & ca:B';
9296
select 'a b:89 ca:23A,64b d:34c'::tsvector @@ 'd:AC & ca:A';

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy