pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Jess Wren <jess.wren@interference.cc>
To: pgsql-general@lists.postgresql.org
Subject: How to use full-text search URL parser to filter query results by domain name?
Date: Sat, 6 Apr 2019 21:06:26 -0700
Message-ID: <2a782f3e-7473-43ce-aad5-1de31cf2b31a@interference.cc> (raw)

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:

        pages

        ----------

        id:          Integer (primary key)

        url:         String  (unique)

        title:       String

        text:        String

        html:        String

        last_visit:  DateTime

        word_pos:    TSVECTOR

        

        links

        ----------

        id         Integer (primary key)

        source:    String

        target:    String  

        link_text: String

        UNIQUE(source,target)

        

        crawls

        ---------

        id:         Integer (primary key)

        query:      String

        

        crawl_results

        -------------

        id:       Integer (primary key)

        score:    Integer (constraint 0<=score<=1)

        crawl_id: Integer (foreign key, crawls.id)

        page_id:  Integer (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:

        WITH top_results AS 

            (SELECT page_id, score FROM crawl_results 

            WHERE crawl_id=$1 

            ORDER BY score LIMIT 100)

        SELECT top_results.score, l.target

        FROM top_results 

            JOIN pages p ON top_results.page_id=p.id

            JOIN links l on p.url=l.source 

        WHERE NOT EXISTS (SELECT pp.id FROM pages pp WHERE l.target=pp.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 <https://www.postgresql.org/docs/11/textsearch-parsers.html;,
which can parse URLS and extract the hostname. For instance, I can
extract the hostname from a URL as follows:

        SELECT token FROM ts_parse('default', 'http://www.foo.com';) WHERE tokid = 6;

        

            token    

        -------------

        www.foo.com

        (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
<https://www.postgresql.org/docs/11/textsearch-debugging.html#TEXTSEARCH-PARSER-TESTING>";,
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!

view thread (6+ messages)  latest in thread

Message-ID: <2a782f3e-7473-43ce-aad5-1de31cf2b31a@interference.cc>
Permalink:  ../2a782f3e-7473-43ce-aad5-1de31cf2b31a@interference.cc/
Also on:    postgresql.org/message-id/2a782f3e-7473-43ce-aad5-1de31cf2b31a@interference.cc

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: jess.wren@interference.cc
  Subject: Re: How to use full-text search URL parser to filter query results by domain name?
  In-Reply-To: <2a782f3e-7473-43ce-aad5-1de31cf2b31a@interference.cc>
  References: <2a782f3e-7473-43ce-aad5-1de31cf2b31a@interference.cc>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox