-- Slice 1 generic lexical search support.
-- Adds PostgreSQL full-text and trigram search infrastructure for DOC-side search.

CREATE EXTENSION IF NOT EXISTS pg_trgm with schema doc;

ALTER TABLE doc.doc_text_representation
    ADD COLUMN IF NOT EXISTS search_config VARCHAR(64);

ALTER TABLE doc.doc_text_representation
    ADD COLUMN IF NOT EXISTS search_vector tsvector;

CREATE INDEX IF NOT EXISTS idx_doc_text_repr_search_vector
    ON doc.doc_text_representation
    USING GIN (search_vector);

CREATE INDEX IF NOT EXISTS idx_doc_document_title_trgm
    ON doc.doc_document
    USING GIN (title doc.gin_trgm_ops);

CREATE INDEX IF NOT EXISTS idx_doc_document_summary_trgm
    ON doc.doc_document
    USING GIN (summary doc.gin_trgm_ops);

CREATE INDEX IF NOT EXISTS idx_doc_text_repr_text_trgm
    ON doc.doc_text_representation
    USING GIN (text_body doc.gin_trgm_ops);
