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 1hCz57-0001Zq-Ft for pgsql-general@arkaria.postgresql.org; Sun, 07 Apr 2019 04:06:58 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hCz55-0004L5-J2 for pgsql-general@arkaria.postgresql.org; Sun, 07 Apr 2019 04:06:55 +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 1hCz54-0004Ku-SX for pgsql-general@lists.postgresql.org; Sun, 07 Apr 2019 04:06:55 +0000 Received: from cichlid.maple.relay.mailchannels.net ([23.83.214.36]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hCz4v-0005dx-AG for pgsql-general@lists.postgresql.org; Sun, 07 Apr 2019 04:06:53 +0000 X-Sender-Id: dreamhost|x-authsender|jess.wren@interference.cc Received: from relay.mailchannels.net (localhost [127.0.0.1]) by relay.mailchannels.net (Postfix) with ESMTP id 0FC6F5C4B06 for ; Sun, 7 Apr 2019 04:06:41 +0000 (UTC) Received: from pdx1-sub0-mail-a23.g.dreamhost.com (unknown [100.96.39.118]) (Authenticated sender: dreamhost) by relay.mailchannels.net (Postfix) with ESMTPA id C4D2D5C4DD4 for ; Sun, 7 Apr 2019 04:06:40 +0000 (UTC) X-Sender-Id: dreamhost|x-authsender|jess.wren@interference.cc Received: from pdx1-sub0-mail-a23.g.dreamhost.com (pop.dreamhost.com [64.90.62.162]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384) by 0.0.0.0:2500 (trex/5.17.2); Sun, 07 Apr 2019 04:06:41 +0000 X-MC-Relay: Neutral X-MailChannels-SenderId: dreamhost|x-authsender|jess.wren@interference.cc X-MailChannels-Auth-Id: dreamhost X-Broad-Spill: 43c3696e5347400d_1554610000915_1690499454 X-MC-Loop-Signature: 1554610000915:2405914135 X-MC-Ingress-Time: 1554610000915 Received: from pdx1-sub0-mail-a23.g.dreamhost.com (localhost [127.0.0.1]) by pdx1-sub0-mail-a23.g.dreamhost.com (Postfix) with ESMTP id 6D84780621 for ; Sat, 6 Apr 2019 21:06:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=interference.cc; h=from:to :subject:message-id:date:mime-version:content-type; s= interference.cc; bh=W+B5JygguNAvQk4IAbXph06ZdYs=; b=VvE9p50VZBO7 lJ1vaSFAbczpdyuWhib/Yq0v5mCk8v8FvJpp8qOewBpdWkb0TuLHlukbDcU/dVpN H32tzK7iTBOFiDYMeSpKLYA9Orxo5zeHtBbwnuYsb30TqcAcHueIfuQ+O9S1L+3L i0SgYK25p2+3WvgvZUo9bb+v0bVUP9M= Received: from [10.0.0.112] (c-73-193-24-7.hsd1.wa.comcast.net [73.193.24.7]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) (Authenticated sender: jess.wren@interference.cc) by pdx1-sub0-mail-a23.g.dreamhost.com (Postfix) with ESMTPSA id C5ED782854 for ; Sat, 6 Apr 2019 21:06:39 -0700 (PDT) X-DH-BACKEND: pdx1-sub0-mail-a23 From: Jess Wren To: pgsql-general@lists.postgresql.org Subject: How to use full-text search URL parser to filter query results by domain name? Message-ID: <2a782f3e-7473-43ce-aad5-1de31cf2b31a@interference.cc> Date: Sat, 6 Apr 2019 21:06:26 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.6.1 MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="------------CD40192BAFC47A250264FDD8" Content-Language: en-US X-VR-OUT-STATUS: OK X-VR-OUT-SCORE: 59 X-VR-OUT-SPAMCAUSE: gggruggvucftvghtrhhoucdtuddrgeduuddruddtgdektdcutefuodetggdotefrodftvfcurfhrohhfihhlvgemucggtfgfnhhsuhgsshgtrhhisggvpdfftffgtefojffquffvnecuuegrihhlohhuthemuceftddtnecuogfuuhhsphgvtghtffhomhgrihhnucdlgeelmdenfghrlhcuvffnffculddutddmnecujfgurhephffvuffkffgfgggtsegrtderredtfeejnecuhfhrohhmpeflvghsshcuhghrvghnuceojhgvshhsrdifrhgvnhesihhnthgvrhhfvghrvghntggvrdgttgeqnecuffhomhgrihhnpehpohhsthhgrhgvshhqlhdrohhrghdpfhhoohdrtghomhenucfkphepjeefrdduleefrddvgedrjeenucfrrghrrghmpehmohguvgepshhmthhppdhhvghloheplgdutddrtddrtddrudduvdgnpdhinhgvthepjeefrdduleefrddvgedrjedprhgvthhurhhnqdhprghthheplfgvshhsucghrhgvnhcuoehjvghsshdrfihrvghnsehinhhtvghrfhgvrhgvnhgtvgdrtggtqedpmhgrihhlfhhrohhmpehjvghsshdrfihrvghnsehinhhtvghrfhgvrhgvnhgtvgdrtggtpdhnrhgtphhtthhopehpghhsqhhlqdhgvghnvghrrghlsehlihhsthhsrdhpohhsthhgrhgvshhqlhdrohhrghenucevlhhushhtvghrufhiiigvpedt List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --------------CD40192BAFC47A250264FDD8 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable I am trying to understand how to use the full-text search parser for URLS and hostnames to filter results from a text field containing URLS based on domain, and also how to index text columns for fast lookup/matching based on domain. I have a PostgreSQL database containing documents and links downloaded by a web crawler, with the following tables: =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 pages =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 ---------- =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 id:=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0 Integer (primary key) =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 url:=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0 String=C2=A0 (unique) =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 title:=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0 String =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 text:=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0 String =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 html:=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0 String =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 last_visit:=C2=A0 DateTime =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 word_pos:=C2=A0=C2=A0=C2=A0 TS= VECTOR =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=20 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 links =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 ---------- =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 id=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0 Integer (primary key) =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 source:=C2=A0=C2=A0=C2=A0 Stri= ng =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 target:=C2=A0=C2=A0=C2=A0 Stri= ng=C2=A0=20 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 link_text: String =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 UNIQUE(source,target) =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=20 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 crawls =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 --------- =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 id:=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0 Integer (primary key) =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 query:=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0 String =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=20 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 crawl_results =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 ------------- =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 id:=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0 Integer (primary key) =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 score:=C2=A0=C2=A0=C2=A0 Integ= er (constraint 0<=3Dscore<=3D1) =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 crawl_id: Integer (foreign key= , crawls.id) =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 page_id:=C2=A0 Integer (foreig= n key, pages.id) The `source` and `target` fields in the `links` table contain URLs. I am running the following query to extract scored links from the top-ranking search results, for pages that haven't been fetched yet: =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 WITH top_results AS=20 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (SELEC= T page_id, score FROM crawl_results=20 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 WHERE = crawl_id=3D$1=20 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 ORDER = BY score LIMIT 100) =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 SELECT top_results.score, l.ta= rget =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 FROM top_results=20 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 JOIN p= ages p ON top_results.page_id=3Dp.id =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 JOIN l= inks l on p.url=3Dl.source=20 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 WHERE NOT EXISTS (SELECT pp.id= FROM pages pp WHERE l.target=3Dpp.url) However, *I would like to filter these results so that only one row is returned for a given domain (the one with the lowest score)*. So for instance, if I get (0.3, 'http://www.foo.com/bar') and (0.8, 'http://www.foo.com/zor'), I only want the first because it has same domain `foo.com` and has the lower score. I was able to find documentation for the builtin full text search parsers , which can parse URLS and extract the hostname. For instance, I can extract the hostname from a URL as follows: =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 SELECT token FROM ts_parse('de= fault', 'http://www.foo.com') WHERE tokid =3D 6; =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=20 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 token=C2= =A0=C2=A0=C2=A0=20 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 ------------- =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 www.foo.com =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (1 row) However, I can't figure out how I would integrate this into the above query to filter out duplicate domains from the results. And because this is the docs for "testing and debugging text search ", I don't know if this use of `ts_parse()` is even related to how the URL parser is intended to be used in practice. How would I use the "host" parser in my query above to return one row per domain? Also, how would I appropriately index the "links" table for "host" and "url" token lookup? Thanks! --------------CD40192BAFC47A250264FDD8 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable

I am trying to understand how to use the full-text search parser for URLS and hostnames to filter results from a text field containing URLS based on domain, and also how to index text columns for fast lookup/matching based on domain.

I have a PostgreSQL database containing documents and links downloaded by a web crawler, with the following tables:

=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 pages
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 ----------
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 id:=C2=A0=C2=A0=C2=A0=
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 Integer (primary key)
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 url:=C2=A0=C2=A0=C2=A0=
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 String=C2=A0 (unique)
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 title:=C2=A0=C2=A0=C2=
=A0=C2=A0=C2=A0=C2=A0 String
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 text:=C2=A0=C2=A0=C2=A0=
=C2=A0=C2=A0=C2=A0=C2=A0 String
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 html:=C2=A0=C2=A0=C2=A0=
=C2=A0=C2=A0=C2=A0=C2=A0 String
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 last_visit:=C2=A0 Dat=
eTime
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 word_pos:=C2=A0=C2=A0=
=C2=A0 TSVECTOR
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 links
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 ----------
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 id=C2=A0=C2=A0=C2=A0=C2=
=A0=C2=A0=C2=A0=C2=A0=C2=A0 Integer (primary key)
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 source:=C2=A0=C2=A0=C2=
=A0 String
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 target:=C2=A0=C2=A0=C2=
=A0 String=C2=A0 
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 link_text: String
    
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 UNIQUE(source,target)=
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 crawls
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 ---------
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 id:=C2=A0=C2=A0=C2=A0=
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 Integer (primary key)
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 query:=C2=A0=C2=A0=C2=
=A0=C2=A0=C2=A0 String
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 crawl_results
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 -------------
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 id:=C2=A0=C2=A0=C2=A0=
=C2=A0=C2=A0=C2=A0 Integer (primary key)
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 score:=C2=A0=C2=A0=C2=
=A0 Integer (constraint 0<=3Dscore<=3D1)
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 crawl_id: Integer (fo=
reign key, crawls.id)
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 page_id:=C2=A0 Intege=
r (foreign key, pages.id)


The `source` and `target` fields in the `links` table contain URLs. I am running the following query to extract scored links from the top-ranking search results, for pages that haven't been fetched yet:

=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 WITH top_results AS <=
/pre>
    
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=
 (SELECT page_id, score FROM crawl_results 
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=
 WHERE crawl_id=3D$1 
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=
 ORDER BY score LIMIT 100)
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 SELECT top_results.sc=
ore, l.target
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 FROM top_results 
    
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=
 JOIN pages p ON top_results.page_id=3Dp.id
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=
 JOIN links l on p.url=3Dl.source 
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 WHERE NOT EXISTS (SEL=
ECT pp.id FROM pages pp WHERE l.target=3Dpp.url)


However, I would like to filter these results so that only one row is returned for a given domain (the one with the lowest score). So for instance, if I get (0.3, 'http://www.foo.com/bar') and (0.8, 'http://www.foo.com/zor'), I only want the first because it has same domain `foo.com` and has the lower score.

I was able to find documentation for the builtin full text search parsers, which can parse URLS and extract the hostname. For instance, I can extract the hostname from a URL as follows:

=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 SELECT token FROM ts_=
parse('default', 'http://www.foo.com') WHERE tokid =3D 6;
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=
 token=C2=A0=C2=A0=C2=A0 
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 -------------
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 www.foo.com
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (1 row)



However, I can't figure out how I would integrate this into the above query to filter out duplicate domains from the results. And because this is the docs for "testing and debugging text search", I don't know if this use of `ts_parse()` is even related to how the URL parser is intended to be used in practice.

How would I use the "host" parser in my query above to return one row per domain? Also, how would I appropriately index the "links" table for "host" and "url" token lookup?

Thanks!

--------------CD40192BAFC47A250264FDD8--