For the sake of this question I have two tables :
CREATE TABLE room (
id serial primary key,
lang varchar(12) NOT NULL default 'english'
);
CREATE TABLE message (
id bigserial primary key,
room integer references room(id),
content text NOT NULL
);
and I want to have a full text search with a language dependant tokenization : the tokenization of messages and their search must depend on the language of the room.
The totally unoptimized and unindexed search would be like this :
select message.id, content, lang from message, room
where message.room=room.id
and to_tsvector(lang::regconfig, content)
@@ plainto_tsquery(lang::regconfig,'what I search')
and room=33;
A search query is always done in one unique room (so the language is homogeneous).
Now my question is how to do this efficiently ? I can't directly build an expression index as the expressions used in indexes must be "immutable" (rely only on the indexed row).
Is the creation of a new column containing to_tsvector(lang::regconfig, content)
(and maintained with a trigger) the only reasonable solution if I want to have an index ?
Is that the most efficient ?