Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

123
Views
Postgres Search Strategy

I have data that is segmented into:-

  • postcode
  • town
  • place

My aim is to construct a search function that will look for any text given using the above segmentations in order of precedence. ie. First look for postcode, then town then place

Can this be done efficiently without having to full scan postcode/town before getting on to place? I can identify that the text is a postcode using a REGEX, town and place are more difficult.

I am happy to code this as a PLPGSQL function and made some progress with a strategy along these lines:-

WITH POSTCODES AS (

    SELECT postcode FROM postcode WHERE postcode ~* $1

), TOWNS AS (

    SELECT town FROM towns WHERE (SELECT * FROM POSTCODES LIMIT 1) IS NULL AND town ~* $1 

), PLACES AS (

   SELECT place FROM places WHERE (SELECT * FROM TOWNS LIMIT 1) IS NULL AND place ~* $1

) 
SELECT postcode as res FROM POSTCODE
UNION ALL 
SELECT town as res FROM TOWNS
UNION ALL
SELECT place as res FROM PLACES 
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

I solved this using weightings and combining my data into a single table with columns POSTCODE, TOWN, PLACE which my data allows me to do.

I can then create an additional column tsv as:-

setweight(to_tsvector(COALESCE(postcode,'')), 'A') ||
setweight(to_tsvector(COALESCE(town,''))    , 'B') ||
setweight(to_tsvector(COALESCE(place,''))   , 'C')

And search with:-

 WHERE (tsv @@ plainto_tsquery('SN1 3PF'))
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!