0

Let's say I have a URL as Follows:

www.somewebsite.com/dining/caseys+grille

I have a business_listings table in Postgres that contains a column business_name. I have a record in the table with 'Casey's Grille'

How can I query 'caseys+grill' against 'Casey's Grille'?

Would I need to use full text search? How would I go about doing this?

3
  • So is the use case searching for caseys+grill and find "Casey's Grille" and ""Caseys Griller" - or always find the one and only "Casey's Grille" when the slug "caseys+grill" is used? Commented Mar 19, 2020 at 10:36
  • What do you want to do with the part before the final '/'? Commented Mar 19, 2020 at 14:03
  • I have an idea...what if I have a column for the url slug itself? Commented Mar 19, 2020 at 16:21

3 Answers 3

1

Since you are not searching for regular words, but for proper names, and you probably also want to find results that are similar in spelling, you should use trigram GIN indexes and similarity search.

Sign up to request clarification or add additional context in comments.

Comments

1

This problem looks simple at first, but it is a can of worms.

The solution should consider all the use cases: is it only a matter of removing/rewriting special characters? Do you need to consider typos (is casey grill the same)? Do you need to consider distinctive marks (is Casey's Grill #2 the same)? Do you need to consider abbreviations (is NY Grill the same as New-York Grill?) Do you need to consider numbers (is 1st av. Grill the same as first avenue grill)?

If it is your database + website, the simplest is to record/compare the URL slug directly.

Else, or if you don't control the URL (like if it is the result of a search box), you may want to store/compare a parsed name. Using both the DB title and the URL slug, you transform the name to common elements. For example you change common abbreviations to their full text, you remove all special characters, you remove/add space, if your language has accents you can remove them, standardize the casing etc. Only you can find and apply the suitable transformations.

Then you can compare the two parsed named, using any suitable comparison method (trigram, plain equality, like queries etc)

Comments

0

I assume you actually want a single slug of the text value in business_name and you want this to be a unique identifier for this particular business.

You can create an additional column business_name_slug and create a unique index on this column.

Then you can create a before insert or update trigger that writes the slug created from business_name into this column.

The tricky part is to create a logic that

  • generates an url friendly version of the the business name (there should be some example in Blog Posts,Githuhib Gists etc., for example)
  • avoids naming collisions so your unique constraint will not raise an error when inserting/updating

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.