Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iwCyJ-0003tl-Br for pgsql-bugs@arkaria.postgresql.org; Mon, 27 Jan 2020 22:35:07 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iwCyH-0008VZ-PX for pgsql-bugs@arkaria.postgresql.org; Mon, 27 Jan 2020 22:35:05 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iwCyH-0008VS-Gu for pgsql-bugs@lists.postgresql.org; Mon, 27 Jan 2020 22:35:05 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1iwCyE-0005Q1-RD for pgsql-bugs@lists.postgresql.org; Mon, 27 Jan 2020 22:35:05 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id 00RMYwsH023962; Mon, 27 Jan 2020 17:34:58 -0500 From: Tom Lane To: dominik.giger@gmail.com cc: pgsql-bugs@lists.postgresql.org Subject: Re: BUG #16235: ts_rank ignores match and only considers lower weighted vector In-reply-to: <16235-2dad73572326cfab@postgresql.org> References: <16235-2dad73572326cfab@postgresql.org> Comments: In-reply-to PG Bug reporting form message dated "Mon, 27 Jan 2020 20:04:26 +0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <23960.1580164498.1@sss.pgh.pa.us> Date: Mon, 27 Jan 2020 17:34:58 -0500 Message-ID: <23961.1580164498@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk PG Bug reporting form writes: > The following query shows the problem: > select ts_rank(doc1, query) as rank_wrong, ts_rank(doc2, query) as > rank_correct > from (select setweight(to_tsvector('simple', 'foo something'), 'A') || > setweight(to_tsvector('simple', 'foobar'), 'C') as doc1, > setweight(to_tsvector('simple', 'foo something'), 'A') as > doc2, > to_tsquery('simple', 'foo:* & something') as > query) as subquery; > ts_rank on doc1 is only half of the rank of doc2. ts_rank seems to only > consider the 'foobar' term with lower weight when calculating the rank. The > foo:1A is only considered in doc2. No, that's not correct. What it actually is doing is taking some sort of average of the weights of the occurrences, as you can see if you play around with a few more examples besides these two. That could be better documented, perhaps, but I don't think it's obviously broken. I can see that there might be a use for taking the max or even the sum of the weights rather than an average --- in many situations it wouldn't be desirable to rank doc1 of your example lower than doc2. But really that'd be a different ranking algorithm, not a bug fix for this one. The manual claims you can write your own ranking algorithm ... but AFAICS you'd have to code it in C, because we aren't exposing anything at SQL level that would let you get at the raw match data :-(. So there's room for improvement there. Also, you might try using ts_rank_cd() instead, as that uses a different algorithm for combining the weights. At least on this example, doc1 gets a higher score than doc2. regards, tom lane