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 1hD1VT-0007qr-Qv for pgsql-general@arkaria.postgresql.org; Sun, 07 Apr 2019 06:42:20 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hD1VS-0005ek-Fl for pgsql-general@arkaria.postgresql.org; Sun, 07 Apr 2019 06:42:18 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hD1VS-0005TB-13 for pgsql-general@lists.postgresql.org; Sun, 07 Apr 2019 06:42:18 +0000 Received: from mailout09.t-online.de ([194.25.134.84]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hD1VO-00078t-8j for pgsql-general@lists.postgresql.org; Sun, 07 Apr 2019 06:42:16 +0000 Received: from fwd33.aul.t-online.de (fwd33.aul.t-online.de [172.20.27.144]) by mailout09.t-online.de (Postfix) with SMTP id 6754D422C23E for ; Sun, 7 Apr 2019 08:42:10 +0200 (CEST) Received: from musicman.homeip.net (rAifQ0ZO8hF2DlsYrqL4RUHNjoh+3QxzlHs17+5lf736Vne3L1po7t-dzsx7faYQwn@[79.200.100.191]) by fwd33.t-online.de with (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384 encrypted) esmtp id 1hD1VH-4KKhwe0; Sun, 7 Apr 2019 08:42:07 +0200 Received: (qmail 1111 invoked by uid 1000); 7 Apr 2019 06:42:06 -0000 Date: 7 Apr 2019 06:42:06 -0000 Subject: Re: How to use full-text search URL parser to filter query results by domain name? To: pgsql-general@lists.postgresql.org From: hamann.w@t-online.de Message-Id: X-Mailer: TkMail 4.0beta9 Content-type: text/plain; charset=utf-8 In-Reply-To: <2a782f3e-7473-43ce-aad5-1de31cf2b31a@interference.cc> Content-Transfer-Encoding: quoted-printable X-ID: rAifQ0ZO8hF2DlsYrqL4RUHNjoh+3QxzlHs17+5lf736Vne3L1po7t-dzsx7faYQwn X-TOI-MSGID: 3f6fbf01-c924-47fc-8cdc-5b134016391c List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk >> 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= 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 S= tring >> = >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 target:=C2=A0=C2=A0=C2=A0 S= tring=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 In= teger (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 (for= eign 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-ranki= ng >> 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 = >> = >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (SE= LECT 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 WHE= RE 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 ORD= ER BY score LIMIT 100) >> = >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 SELECT top_results.score, l= =2Etarget >> = >> =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 JOI= N 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 JOI= N links l on p.url=3Dl.source = >> = >> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 WHERE NOT EXISTS (SELECT pp= =2Eid 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: >> = Hi, I have no real idea about solving the complete problem, and would probabl= y try something with a temp table first. For extracting the hostname from a url you could use select regex_replace('https?://(.*=3D)/.*', '\\1', url) instead of the fulltext parser Best regards Wolfgang