Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pIHLW-00084G-Lo for pgsql-hackers@arkaria.postgresql.org; Wed, 18 Jan 2023 22:55:54 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pIHLV-0006QJ-E6 for pgsql-hackers@arkaria.postgresql.org; Wed, 18 Jan 2023 22:55:53 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pIHLV-0006Q8-4l for pgsql-hackers@lists.postgresql.org; Wed, 18 Jan 2023 22:55:53 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pIHLS-0008Sz-OL for pgsql-hackers@lists.postgresql.org; Wed, 18 Jan 2023 22:55:52 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 30IMtkZS3951262; Wed, 18 Jan 2023 17:55:46 -0500 From: Tom Lane To: Alvaro Herrera cc: pgsql-hackers@lists.postgresql.org, sebastian.patino-lang@posteo.net Subject: Re: Rethinking the implementation of ts_headline() In-reply-to: <20230118110942.od2naagwp6molgxz@alvherre.pgsql> References: <20230118110942.od2naagwp6molgxz@alvherre.pgsql> Comments: In-reply-to Alvaro Herrera message dated "Wed, 18 Jan 2023 12:09:42 +0100" MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-ID: <3951260.1674082546.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Wed, 18 Jan 2023 17:55:46 -0500 Message-ID: <3951261.1674082546@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Alvaro Herrera writes: > I tried this other test, based on looking at the new regression tests > you added, > SELECT ts_headline('english', ' > Day after day, day after day, > We stuck, nor breath nor motion, > As idle as a painted Ship > Upon a painted Ocean. > Water, water, every where > And all the boards did shrink; > Water, water, every where, > Nor any drop to drink. > S. T. Coleridge (1772-1834) > ', to_tsquery('english', '(day & drink) | (idle & painted)'), 'MaxFragme= nts=3D5, MaxWords=3D9, MinWords=3D4'); > ts_headline = > =E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80= =E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2= =94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94= =80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80= =E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80 > motion, =E2=86=B5 > As idle as a painted Ship=E2=86=B5 > Upon > (1 fila) > and was surprised that the match for the 'day & drink' arm of the OR > disappears from the reported headline. I'd argue that that's exactly what should happen. It's supposed to find as-short-as-possible cover strings that satisfy the query. In this case, satisfying the 'day & drink' condition would require nearly the entire input text, whereas 'idle & painted' can be satisfied in just the third line. So what you get is the third line, slightly expanded because of some later rules that like to add context if the cover is shorter than MaxWords. I don't find anything particularly principled about the old behavior: > Day after day, day after day,=E2=86=B5 > We stuck ... motion, =E2=86=B5 > As idle as a painted Ship =E2=86=B5 > Upon It's including hits for "day" into the cover despite the lack of any nearby match to "drink". > Another thing I think might be a regression is the way fragments are > selected. Consider what happens if I change the "idle & painted" in the > earlier query to "idle <-> painted", and MaxWords is kept low: Of course, "idle <-> painted" is satisfied nowhere in this text (the words are there, but not adjacent). So the cover has to run from the last 'day' to the 'drink'. I think v15 is deciding that it runs from the first 'day' to the 'drink', which while not exactly wrong is not the shortest cover. The rest of this is just minor variations in what mark_hl_fragments() decides to do based on the precise cover string it's given. I don't dispute that mark_hl_fragments() could be improved, but this patch doesn't touch its algorithm and I think that doing so should be material for a different patch. (I have no immediate ideas about what would be a better algorithm for it, anyway.) > (Both 15 and master highlight 'painted' in the "Upon a painted Ocean" > verse, which perhaps they shouldn't do, since it's not preceded by > 'idle'.) Yeah, and 'idle' too. Once it's chosen a string to show, it'll highlight all the query words within that string, whether they constitute part of the match or not. I can see arguments on both sides of doing it that way; it was probably more sensible before we had phrase match than it is now. But again, changing that phase of the processing is outside the scope of this patch. I'm just trying to undo the damage I did to the cover-string-selection phase. regards, tom lane