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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:59:44+00:00 2026-06-14T00:59:44+00:00

Somehow I’ve tricked myself into writing a full text search implementation on a database.

  • 0

Somehow I’ve tricked myself into writing a full text search implementation on a database. I have a table that represents all the entities in my database, a table that represents all the tags, and a table representing the many to many relationship of tags to entities.

I wrote a query that groups all tag names for a given entity and concatenates them into a string, which I then transform into a ts_vector. That query looks like this:

SELECT e.id, to_tsvector(c.publicname || ' ' || string_agg(cv.name, ' '))
FROM categoryvalue cv, entitycategoryvalue ecv, entity e 
WHERE ccv.categoryvalueid = cv.id AND e.id = ecv.entityid
GROUP BY e.id;

The query returns results in this schema:

id | to_tsvector
1  | tag_a, tag_b, tag_c
2  | tag_b, tag_d, tag_e

Which is exactly what I’d like to match a ts_query against. I’m a noob at SQL though, and I am wondering if there is a way I can create a table that is continually updated with the results of the query I’ve written?

EDIT: I documented my eventual solution and system in this blog post http://tech.pro/tutorial/1142/building-faceted-search-with-postgresql

  • 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-14T00:59:45+00:00Added an answer on June 14, 2026 at 12:59 am

    I think you want a VIEW.

    CREATE VIEW my_tsearch_table AS
    SELECT e.id, to_tsvector(c.publicname || ' ' || string_agg(cv.name, ' '))
    FROM categoryvalue cv, entitycategoryvalue c=ecv, entity e 
    WHERE ccv.categoryvalueid = cv.id AND e.id = ecv.celebrityid
    GROUP BY e.id;
    

    Note, however, that you cannot add indexes to a view. This may be a problem with full-text search, as it’s quite expensive to generate all those tsvectors for every search.

    If you need to index that table, you are looking for a materialized view.

    PostgreSQL does not support automatically maintained materialized views; you can’t just CREATE MATERIALIZED VIEW and then add some indexes to it.

    What you can do is manually maintain a materialized view using an ordinary table, an ordinary view created with your query, and some trigger functions.

    Add a trigger function on each table that contributes to the view, and have that trigger function update (or insert into, or delete from, as appropriate) the materialized view based on updates made to that table. This can be complicated to get right in a concurrent environment, though, and it can be prone to lock-ordering deadlocks.

    An alternative to a trigger-maintained view is to live with the materialized view getting a little out of date. Periodically create a new table, copy your ordinary view into the new table, add the desired indexes, then drop the old materialized view table and rename the new one to replace it.

    See also:

    • http://wiki.postgresql.org/wiki/Materialized_Views
    • http://tech.jonathangardner.net/wiki/PostgreSQL/Materialized_Views
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

somehow I have the feeling that I miss the forest for the trees. I
Somehow I managed to get QtCreator's text mode into --COMMAND-- or --INSERT-- mode. I
Somehow or the other, I learned that I can simply pass in parameters into
Somehow this customer's database didn't update correctly, and now they have some number columns
Somehow Visual Studio search has stopped working for me. Anytime I search Entire Solution
Somehow I had the impression that ASP.Net differentiates URLs based on the number of
Somehow my master and my origin/master branch have diverged. I actually don't want them
Somehow am not successful with creating the query that I want. DB is to
Somehow, Expression Blend has entered full-screen mode (no window chrome), but I can still
Somehow forms and controls created through Visual Studio and the designer have the great

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.