diff --git a/src/backend/utils/adt/tsvector_op.c b/src/backend/utils/adt/tsvector_op.c index ad5a254..215f58f 100644 --- a/src/backend/utils/adt/tsvector_op.c +++ b/src/backend/utils/adt/tsvector_op.c @@ -1474,14 +1474,14 @@ TS_phrase_execute(QueryItem *curitem, */ Rpos = Rdata.pos; - LposStart = Ldata.pos; while (Rpos < Rdata.pos + Rdata.npos) { /* - * We need to check all possible distances, so reset Lpos to - * guaranteed not yet satisfied position. + * For each physically positioned right-side operand iterate over each + * instance of the left-side operand to locate one at exactly the specified + * distance. As soon as a match is found move onto the next right-operand + * and continue searching starting with the next left-operand. */ - Lpos = LposStart; while (Lpos < Ldata.pos + Ldata.npos) { if (WEP_GETPOS(*Rpos) - WEP_GETPOS(*Lpos) == @@ -1490,16 +1490,16 @@ TS_phrase_execute(QueryItem *curitem, /* MATCH! */ if (data) { - /* Store position for upper phrase operator */ + /* Store the position of the right-operand */ *pos_iter = WEP_GETPOS(*Rpos); pos_iter++; /* - * Set left start position to next, because current - * one could not satisfy distance for any other right - * position + * Move onto the next right-operand, and also the next + * left-operand as the current one cannot be a match + * for any other. */ - LposStart = Lpos + 1; + Lpos++ break; } else @@ -1512,18 +1512,23 @@ TS_phrase_execute(QueryItem *curitem, } } - else if (WEP_GETPOS(*Rpos) <= WEP_GETPOS(*Lpos) || - WEP_GETPOS(*Rpos) - WEP_GETPOS(*Lpos) < + else if (WEP_GETPOS(*Rpos) - WEP_GETPOS(*Lpos) < curitem->qoperator.distance) { /* - * Go to the next Rpos, because Lpos is ahead or on less - * distance than required by current operator + * We are now within the required distance so + * continue onto the next right-operand and retry + * the current left-operand to see if the added + * distance causes it to match. */ break; } + /* + * The current left-operand is too far away so + * try the next one. + */ Lpos++; }