简体   繁体   中英

retrieving keys from a GIN index on a tsvector column in Postgres 9.1

I've got a collection of documents that I'd like to do some full text searching on in Postgres 9.1, so adding them as the contents of a tsvector and then creating a GIN index on this column seems straightforward enough. My understanding of the GIN index is that the keys used are (depedning on the text search configuration used) either the list of original words or their normalized lexemes from the collection of original documents. However, for other functionality we've developed I'd really like to get that list of keys out of the GIN index to put in another column in a different table for use. Is this possible?

Edit: I've re-posted this on the pgsql-general mailing list .

I'm assuming you've created an expression index as per the documentation , like:

CREATE INDEX pgweb_idx ON pgweb USING gin(to_tsvector('english', body));

If so, as far as I know you can't directly extract values from the index, as:

SELECT to_tsvector('english', body) FROM pgweb;

doesn't seem to be able to use the index even when forced (for testing only) with set enable_seqscan = off . It's possible that this may work in PostgreSQL 9.2 with index-only scans; I haven't upgraded my machine yet so I can't say.

You may be able to use the index to speed other queries by JOIN ing on the table with the indexed data and using the same expression index, though.

If you intend to use the tsvector for something else, I'd suggest changing your approach. Add a new column of type tsvector to your table. Use a BEFORE INSERT OR UPDATE OR DELETE ... FOR EACH ROW trigger to automatically keep the tsvector column up to date with the column being indexed. Then create your GIN index on the new tsvector column. That way you can query the column directly, not use it only as an expression index. If you read further down the "Creating Indexes" section I already linked to above, you'll see an explanation of how to do this and how to use a trigger to automatically update a tsvector column .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM