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 7817929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T06:24:07+00:00 2026-06-02T06:24:07+00:00

Background Users can type in a name and the system should match the text,

  • 0

Background

Users can type in a name and the system should match the text, even if the either the user input or the database field contains accented (UTF-8) characters. This is using the pg_trgm module.

Problem

The code resembles the following:

  SELECT
    t.label
  FROM
    the_table t
  WHERE
    label % 'fil'
  ORDER BY
    similarity( t.label, 'fil' ) DESC

When the user types fil, the query matches filbert but not filé powder. (Because of the accented character?)

Failed Solution #1

I tried to implement an unaccent function and rewrite the query as:

  SELECT
    t.label
  FROM
    the_table t
  WHERE
    unaccent( label ) % unaccent( 'fil' )
  ORDER BY
    similarity( unaccent( t.label ), unaccent( 'fil' ) ) DESC

This returns only filbert.

Failed Solution #2

As suggested:

CREATE EXTENSION pg_trgm;
CREATE EXTENSION unaccent;

CREATE OR REPLACE FUNCTION unaccent_text(text)
  RETURNS text AS
$BODY$
  SELECT unaccent($1); 
$BODY$
  LANGUAGE sql IMMUTABLE
  COST 1;

All other indexes on the table have been dropped. Then:

CREATE INDEX label_unaccent_idx 
ON the_table( lower( unaccent_text( label ) ) );

This returns only one result:

  SELECT
    t.label
  FROM
    the_table t
  WHERE
    label % 'fil'
  ORDER BY
    similarity( t.label, 'fil' ) DESC

Question

What is the best way to rewrite the query to ensure that both results are returned?

Thank you!

Related

http://wiki.postgresql.org/wiki/What%27s_new_in_PostgreSQL_9.0#Unaccent_filtering_dictionary

http://postgresql.1045698.n5.nabble.com/index-refuses-to-build-td5108810.html

  • 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-02T06:24:10+00:00Added an answer on June 2, 2026 at 6:24 am

    You are not using the operator class provided by the pg_trgm module. Create an index like this:

    CREATE INDEX label_Lower_unaccent_trgm_idx
    ON test_trgm USING gist (lower(unaccent_text(label)) gist_trgm_ops);
    

    Originally, I had a GIN index here, but a GiST is typically better suited for this kind of query because it can return values sorted by similarity. See:

    • Matching patterns between multiple columns
    • Finding similar strings with PostgreSQL quickly

    Your query has to match the index expression to be able to use it.

    SELECT label
    FROM   the_table
    WHERE  lower(unaccent_text(label)) % 'fil'
    ORDER  BY similarity(label, 'fil') DESC;  -- ok to use original string here
    

    However, "filbert" and "filé powder" are not actually very similar to "fil" according to the % operator. I suspect you really want:

    SELECT label
    FROM   the_table
    WHERE  lower(unaccent_text(label)) LIKE 'fil%'  -- !
    ORDER  BY similarity(label, 'fil') DESC;  -- ok to use original string here
    

    This finds all strings starting with the search string, and sorts the best matches according to the % operator first.

    The expression can use a GIN or GiST index since PostgreSQL 9.1! The manual:

    Beginning in PostgreSQL 9.1, these index types also support index
    searches for LIKE and ILIKE, for example

    If you actually meant to use the % operator:

    Try adapting the threshold for the similarity operator %:

    SET pg_trgm.similarity_threshold = 0.1;  -- Postgres 9.6 or later
    SELECT set_limit(0.1);  -- Postgres 9.5 or older
    

    Or even lower? The default is 0.3. Just to see whether the threshold filters additional matches.

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

Sidebar

Related Questions

Background: working a user q&a site where users logged,etc. What user/visitor data should be
Background story: when a user selects a portion of text in a text field
at the moment i have this <div id=beau> <div> <input type=text id=lun name=lun size=24
I have an application where the user can type in a color name, say
Background Schema: class Checkpoint(db.Model): id = db.Column(db.Integer, primary_key=True) creator = db.Column(db.Integer, db.ForeignKey('user.id')) name =
Background: I am making a facebook app where users post messages like in a
background on the topic ok ive been coding a messaging page which users come
Background: I am creating a REST api, that will require users to use only
Long-Winded Background I'm working on parallelising some code for cardiac electrophysiology simulations. Since users
Background: (Read before you flame...) I want a user to be able to SELECT

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.