Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit028350f

Browse files
committed
Make exact distance match for FTS phrase operator
Phrase operator now requires exact distance betweens lexems instead ofless-or-equal.Per discussion c19fcfec308e6ccd952cdde9e648b505@mail.gmail.com
1 parentf199303 commit028350f

File tree

4 files changed

+64
-48
lines changed

4 files changed

+64
-48
lines changed

‎doc/src/sgml/textsearch.sgml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -346,10 +346,10 @@ SELECT to_tsvector('error is not fatal') @@ to_tsquery('fatal <-> error');
346346

347347
There is a more general version of the FOLLOWED BY operator having the
348348
form <literal>&lt;<replaceable>N</>&gt;</literal>,
349-
where <replaceable>N</> is an integer standing for thegreatest distance
349+
where <replaceable>N</> is an integer standing for theexact distance
350350
allowed between the matching lexemes. <literal>&lt;1&gt;</literal> is
351351
the same as <literal>&lt;-&gt;</>, while <literal>&lt;2&gt;</literal>
352-
allows one other lexeme tooptionallyappear between the matches, and so
352+
allows one other lexeme to appear between the matches, and so
353353
on. The <literal>phraseto_tsquery</> function makes use of this
354354
operator to construct a <literal>tsquery</> that can match a multi-word
355355
phrase when some of the words are stop words. For example:
@@ -1529,7 +1529,7 @@ SELECT to_tsquery('fat') &lt;-&gt; to_tsquery('cat | rat');
15291529
<para>
15301530
Returns a query that searches for a match to the first given query
15311531
followed by a match to the second given query at a distance of at
1532-
most<replaceable>distance</replaceable> lexemes, using
1532+
<replaceable>distance</replaceable> lexemes, using
15331533
the <literal>&lt;<replaceable>N</>&gt;</literal>
15341534
<type>tsquery</> operator. For example:
15351535

‎src/backend/utils/adt/tsvector_op.c

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1375,6 +1375,7 @@ TS_phrase_execute(QueryItem *curitem,
13751375
ExecPhraseDataLdata= {0, false,NULL},
13761376
Rdata= {0, false,NULL};
13771377
WordEntryPos*Lpos,
1378+
*LposStart,
13781379
*Rpos,
13791380
*pos_iter=NULL;
13801381

@@ -1416,52 +1417,60 @@ TS_phrase_execute(QueryItem *curitem,
14161417
pos_iter=data->pos;
14171418
}
14181419

1419-
Lpos=Ldata.pos;
1420-
Rpos=Rdata.pos;
1421-
14221420
/*
14231421
* Find matches by distance, WEP_GETPOS() is needed because
14241422
* ExecPhraseData->data can point to the tsvector's WordEntryPosVector
14251423
*/
14261424

1425+
Rpos=Rdata.pos;
1426+
LposStart=Ldata.pos;
14271427
while (Rpos<Rdata.pos+Rdata.npos)
14281428
{
1429+
/*
1430+
* We need to check all possible distances, so reset Lpos
1431+
* to guranteed not yet satisfied position.
1432+
*/
1433+
Lpos=LposStart;
14291434
while (Lpos<Ldata.pos+Ldata.npos)
14301435
{
1431-
if (WEP_GETPOS(*Lpos) <=WEP_GETPOS(*Rpos))
1436+
if (WEP_GETPOS(*Rpos)-WEP_GETPOS(*Lpos)==
1437+
curitem->qoperator.distance)
14321438
{
1433-
/*
1434-
* Lpos is behind the Rpos, so we have to check the
1435-
* distance condition
1436-
*/
1437-
if (WEP_GETPOS(*Rpos)-WEP_GETPOS(*Lpos) <=curitem->qoperator.distance)
1439+
/* MATCH! */
1440+
if (data)
14381441
{
1439-
/* MATCH! */
1440-
if (data)
1441-
{
1442-
*pos_iter=WEP_GETPOS(*Rpos);
1443-
pos_iter++;
1444-
1445-
break;/* We need to build a unique result
1446-
* array, so go to the next Rpos */
1447-
}
1448-
else
1449-
{
1450-
/*
1451-
* We are in the root of the phrase tree and hence
1452-
* we don't have to store the resulting positions
1453-
*/
1454-
return true;
1455-
}
1442+
/* Store position for upper phrase operator */
1443+
*pos_iter=WEP_GETPOS(*Rpos);
1444+
pos_iter++;
1445+
1446+
/*
1447+
* Set left start position to next, because current one
1448+
* could not satisfy distance for any other right
1449+
* position
1450+
*/
1451+
LposStart=Lpos+1;
1452+
break;
1453+
}
1454+
else
1455+
{
1456+
/*
1457+
* We are in the root of the phrase tree and hence
1458+
* we don't have to store the resulting positions
1459+
*/
1460+
return true;
14561461
}
1462+
14571463
}
1458-
else
1464+
elseif (WEP_GETPOS(*Rpos) <=WEP_GETPOS(*Lpos)||
1465+
WEP_GETPOS(*Rpos)-WEP_GETPOS(*Lpos)<
1466+
curitem->qoperator.distance)
14591467
{
14601468
/*
1461-
* Go to the next Rpos, because Lpos is aheadof the
1462-
* currentRpos
1469+
* Go to the next Rpos, because Lpos is aheador on less
1470+
*distance than required bycurrentoperator
14631471
*/
14641472
break;
1473+
14651474
}
14661475

14671476
Lpos++;

‎src/test/regress/expected/tstypes.out

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -665,10 +665,10 @@ SELECT to_tsvector('simple', '1 2 3 1') @@ '1 <-> 2' AS "true";
665665
t
666666
(1 row)
667667

668-
SELECT to_tsvector('simple', '1 2 3 1') @@ '1 <2> 2' AS "true";
669-
true
670-
------
671-
t
668+
SELECT to_tsvector('simple', '1 2 3 1') @@ '1 <2> 2' AS "false";
669+
false
670+
-------
671+
f
672672
(1 row)
673673

674674
SELECT to_tsvector('simple', '1 2 3 1') @@ '1 <-> 3' AS "false";
@@ -683,6 +683,12 @@ SELECT to_tsvector('simple', '1 2 3 1') @@ '1 <2> 3' AS "true";
683683
t
684684
(1 row)
685685

686+
SELECT to_tsvector('simple', '1 2 1 2') @@ '1 <3> 2' AS "true";
687+
true
688+
------
689+
t
690+
(1 row)
691+
686692
SELECT to_tsvector('simple', '1 2 11 3') @@ '1 <-> 3' AS "false";
687693
false
688694
-------
@@ -897,7 +903,7 @@ SELECT ts_rank_cd(' a:1 sa:2A sb:2D g'::tsvector, 'a <-> s:*');
897903
SELECT ts_rank_cd(' a:1 sa:2A sb:2D g'::tsvector, 'a <-> s:* <-> sa:A');
898904
ts_rank_cd
899905
------------
900-
0.0714286
906+
0
901907
(1 row)
902908

903909
SELECT ts_rank_cd(' a:1 sa:2A sb:2D g'::tsvector, 'a <-> s:* <-> sa:B');
@@ -924,10 +930,10 @@ SELECT 'a:1 b:2'::tsvector @@ 'a <1> b'::tsquery AS "true";
924930
t
925931
(1 row)
926932

927-
SELECT 'a:1 b:2'::tsvector @@ 'a <2> b'::tsquery AS "true";
928-
true
929-
------
930-
t
933+
SELECT 'a:1 b:2'::tsvector @@ 'a <2> b'::tsquery AS "false";
934+
false
935+
-------
936+
f
931937
(1 row)
932938

933939
SELECT 'a:1 b:3'::tsvector @@ 'a <-> b'::tsquery AS "false";
@@ -954,10 +960,10 @@ SELECT 'a:1 b:3'::tsvector @@ 'a <2> b'::tsquery AS "true";
954960
t
955961
(1 row)
956962

957-
SELECT 'a:1 b:3'::tsvector @@ 'a <3> b'::tsquery AS "true";
958-
true
959-
------
960-
t
963+
SELECT 'a:1 b:3'::tsvector @@ 'a <3> b'::tsquery AS "false";
964+
false
965+
-------
966+
f
961967
(1 row)
962968

963969
-- tsvector editing operations

‎src/test/regress/sql/tstypes.sql

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,10 @@ SELECT 'supeznova supernova'::tsvector @@ 'super:*'::tsquery AS "true";
130130

131131
--phrase search
132132
SELECT to_tsvector('simple','1 2 3 1') @@'1 <-> 2'AS"true";
133-
SELECT to_tsvector('simple','1 2 3 1') @@'1 <2> 2'AS"true";
133+
SELECT to_tsvector('simple','1 2 3 1') @@'1 <2> 2'AS"false";
134134
SELECT to_tsvector('simple','1 2 3 1') @@'1 <-> 3'AS"false";
135135
SELECT to_tsvector('simple','1 2 3 1') @@'1 <2> 3'AS"true";
136+
SELECT to_tsvector('simple','1 2 1 2') @@'1 <3> 2'AS"true";
136137

137138
SELECT to_tsvector('simple','1 2 11 3') @@'1 <-> 3'AS"false";
138139
SELECT to_tsvector('simple','1 2 11 3') @@'1:* <-> 3'AS"true";
@@ -180,12 +181,12 @@ SELECT ts_rank_cd(' a:1 sa:2A sb:2D g'::tsvector, 'a <-> s:* <-> sa:B');
180181
SELECT'a:1 b:2'::tsvector @@'a <-> b'::tsqueryAS"true";
181182
SELECT'a:1 b:2'::tsvector @@'a <0> b'::tsqueryAS"false";
182183
SELECT'a:1 b:2'::tsvector @@'a <1> b'::tsqueryAS"true";
183-
SELECT'a:1 b:2'::tsvector @@'a <2> b'::tsqueryAS"true";
184+
SELECT'a:1 b:2'::tsvector @@'a <2> b'::tsqueryAS"false";
184185
SELECT'a:1 b:3'::tsvector @@'a <-> b'::tsqueryAS"false";
185186
SELECT'a:1 b:3'::tsvector @@'a <0> b'::tsqueryAS"false";
186187
SELECT'a:1 b:3'::tsvector @@'a <1> b'::tsqueryAS"false";
187188
SELECT'a:1 b:3'::tsvector @@'a <2> b'::tsqueryAS"true";
188-
SELECT'a:1 b:3'::tsvector @@'a <3> b'::tsqueryAS"true";
189+
SELECT'a:1 b:3'::tsvector @@'a <3> b'::tsqueryAS"false";
189190

190191
-- tsvector editing operations
191192

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp