tsvector와 tsquery
PostgreSQL 전문 검색은 문서를 정규화한 tsvector와 검색 조건인 tsquery를 @@로 맞춰. GIN 색인을 두면 많은 문서에서도 빠르게 후보를 찾을 수 있어.
검색어를 만들고 순위를 매겨
to_tsvector('english', text), to_tsquery('english', 'postgres & performance')가 기본이고, 사용자 입력에는 plainto_tsquery나 websearch_to_tsquery가 편해. ts_rank와 ts_rank_cd는 관련도를, ts_headline은 일치 구절을 보여줘.
검색 벡터는 저장해 재사용해
tsvector를 STORED 생성 열로 만들고 GIN 색인을 걸면 쓸 때 한 번 계산하고 모든 검색에서 재사용해. 수백만 문서를 ms 단위로 찾는 기반이 돼.