PostgreSQL: Full Text Search - How to search partial words?

后端 未结 6 1228
情书的邮戳
情书的邮戳 2020-12-07 23:23

Following a question posted here about how I can increase the speed on one of my SQL Search methods, I was advised to update my table to make use of Full Text Search. This i

6条回答
  •  忘掉有多难
    2020-12-07 23:50

    Anthoni,

    Assuming you plan on using only ASCII encoding (could be difficult, I'm aware), a very viable option may be the Trigram (pg_trgm) module: http://www.postgresql.org/docs/9.0/interactive/pgtrgm.html

    Trigram utilizes built-in indexing methods such as Gist and Gin. The only modification you have to make is when defining your index, specify an Operator Class of either gist_trgm_ops or gin_trgm_ops.

    If the contrib modules aren't already installed, in Ubuntu it's as easy and running the following command from the shell:

    # sudo apt-get install postgresql-contrib
    

    After the contrib modules are made available, you must install the pg_trgm extension into the database in question. You do this by executing the following PostgreSQL query on the database you wish to install the module into:

    CREATE EXTENSION pg_trgm;
    

    After the pg_trgm extension has been installed, we're ready to have some fun!

    -- Create a test table.
    CREATE TABLE test (my_column text)
    -- Create a Trigram index.
    CREATE INDEX test_my_colun_trgm_idx ON test USING gist (my_column gist_trgm_ops);
    -- Add a couple records
    INSERT INTO test (my_Column) VALUES ('First Entry'), ('Second Entry'), ('Third Entry')
    -- Query using our new index --
    SELECT my_column, similarity(my_column, 'Frist Entry') AS similarity FROM test WHERE my_column % 'Frist Entry' ORDER BY similarity DESC
    

提交回复
热议问题