Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8660025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:09:10+00:00 2026-06-12T16:09:10+00:00

I have a large postgres table of locations (shops, landmarks, etc.) which the user

  • 0

I have a large postgres table of locations (shops, landmarks, etc.) which the user can search in various ways. When the user wants to do a search for the name of a place, the system currently does (assuming the search is on cafe):

lower(location_name) LIKE '%cafe%'

as part of the query. This is hugely inefficient. Prohibitively so. It is essential I make this faster. I’ve tried indexing the table on

gin(to_tsvector('simple', location_name))

and searching with

(to_tsvector('simple',location_name) @@ to_tsquery('simple','cafe'))

which works beautifully, and cuts down the search time by a couple of orders of magnitude.

However, the location names can be in any language, including languages like Chinese, which aren’t whitespace delimited. This new system is unable to find any Chinese locations, unless I search for the exact name, whereas the old system could find matches to partial names just fine.

So, my question is: Can I get this to work for all languages at once, or am I on the wrong track?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T16:09:11+00:00Added an answer on June 12, 2026 at 4:09 pm

    If you want to optimize arbitrary substring matches, one option is to use the pg_tgrm module. Add an index:

    CREATE INDEX table_location_name_trigrams_key ON table
      USING gin (location_name gin_trgm_ops);
    

    This will break “Simple Cafe” into “sim”, “imp”, “mpl”, etc., and add an entry to the index for each trigam in each row. The query planner can then automatically use this index for substring pattern matches, including:

    SELECT * FROM table WHERE location_name ILIKE '%cafe%';
    

    This query will look up “caf” and “afe” in the index, find the intersection, fetch those rows, then check each row against your pattern. (That last check is necessary since the intersection of “caf” and “afe” matches both “simple cafe” and “unsafe scaffolding”, while “%cafe%” should only match one). The index becomes more effective as the input pattern gets longer since it can exclude more rows, but it’s still not as efficient as indexing whole words, so don’t expect a performance improvement over to_tsvector.

    Catch is, trigrams don’t work at all for patterns that under three characters. That may or may not be a deal-breaker for your application.


    Edit: I initially added this as a comment.

    I had another thought last night when I was mostly asleep. Make a cjk_chars function that takes an input string, regexp_matches the entire CJK Unicode ranges, and returns an array of any such characters or NULL if none. Add a GIN index on cjk_chars(location_name). Then query for:

    WHERE CASE
      WHEN cjk_chars('query') IS NOT NULL THEN
        cjk_chars(location_name) @> cjk_chars('query')
        AND location_name LIKE '%query%'
      ELSE
        <tsvector/trigrams>
      END
    

    Ta-da, unigrams!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a pretty large table (20M records) which has a 3 column index
I have a postgres table that contains a very large number of numerals of
I have a Spring-MVC, Hibernate, (Postgres 9 db) Web app. An admin user can
I have large string which I split by newlines. How can I remove all
I have a large table in Postgres. The table name is bigtable and the
I have large table, with around 200 fields. Around a 100 of those fields
I have large database table, approximately 5GB, now I wan to getCurrentSnapshot of Database
In my application I have large area (≈5000x5000pts) and I must allow user to
I have large table. consisting of only 3 columns (id(INT),bookmarkID(INT),tagID(INT)).I have two BTREE indexes
I am developing an application which doesn't have large requirements for data storage. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.