전문 검색
전문 검색 (Full-Text Search)
DuckDB는 fts 확장을 통해 전문(full-text) 검색을 지원합니다. 전문 검색 인덱스를 만들면 긴 문자열 안에서 개별 단어가 등장하는 모든 위치를 빠르게 찾을 수 있어요.
출처: 공식문서
예시: 셰익스피어 코퍼스
셰익스피어 희곡들로 전문 검색 인덱스를 만드는 예시를 보여드릴게요.
CREATE TABLE corpus AS
SELECT * FROM 'https://blobs.duckdb.org/data/shakespeare.parquet';
DESCRIBE corpus;
| column_name | column_type | null | key | default | extra |
|---|---|---|---|---|---|
| line_id | VARCHAR | YES | NULL | NULL | NULL |
| play_name | VARCHAR | YES | NULL | NULL | NULL |
| line_number | VARCHAR | YES | NULL | NULL | NULL |
| speaker | VARCHAR | YES | NULL | NULL | NULL |
| text_entry | VARCHAR | YES | NULL | NULL | NULL |
각 행의 텍스트는 text_entry에, 각 행의 고유 키는 line_id에 담겨 있습니다.
전문 검색 인덱스 만들기
먼저 인덱스를 만듭니다. 테이블 이름, 고유 id 컬럼, 그리고 인덱스할 컬럼(들)을 지정하세요. 여기서는 희곡 텍스트가 들어 있는 단일 컬럼 text_entry만 인덱스할게요.
PRAGMA create_fts_index('corpus', 'line_id', 'text_entry');
이제 이 테이블은 Okapi BM25 랭킹 함수로 쿼리할 준비가 됐습니다. 매치가 없는 행은 NULL 점수를 반환해요.
셰익스피어는 버터에 대해 뭐라고 했을까요?
SELECT
fts_main_corpus.match_bm25(line_id, 'butter') AS score,
line_id, play_name, speaker, text_entry
FROM corpus
WHERE score IS NOT NULL
ORDER BY score DESC;
| score | line_id | play_name | speaker | text_entry |
|---|---|---|---|---|
| 4.427313429798464 | H4/2.4.494 | Henry IV | Carrier | As fat as butter. |
| 3.836270302568675 | H4/1.2.21 | Henry IV | FALSTAFF | prologue to an egg and butter. |
| 3.836270302568675 | H4/2.1.55 | Henry IV | Chamberlain | They are up already, and call for eggs and butter; |
| 3.3844488405497115 | H4/4.2.21 | Henry IV | FALSTAFF | toasts-and-butter, with hearts in their bellies no |
| 3.3844488405497115 | H4/4.2.62 | Henry IV | PRINCE HENRY | already made thee butter. But tell me, Jack, whose |
| 3.3844488405497115 | AWW/4.1.40 | Alls well that ends well | PAROLLES | butter-womans mouth and buy myself another of |
| 3.3844488405497115 | AYLI/3.2.93 | As you like it | TOUCHSTONE | right butter-womens rank to market. |
| 3.3844488405497115 | KL/2.4.132 | King Lear | Fool | kindness to his horse, buttered his hay. |
| 3.0278411214953107 | AWW/5.2.9 | Alls well that ends well | Clown | henceforth eat no fish of fortunes buttering. |
| 3.0278411214953107 | MWW/2.2.260 | Merry Wives of Windsor | FALSTAFF | Hang him, mechanical salt-butter rogue! I will |
| 3.0278411214953107 | MWW/2.2.284 | Merry Wives of Windsor | FORD | rather trust a Fleming with my butter, Parson Hugh |
| 3.0278411214953107 | MWW/3.5.7 | Merry Wives of Windsor | FALSTAFF | Ill have my brains taen out and buttered, and give |
| 3.0278411214953107 | MWW/3.5.102 | Merry Wives of Windsor | FALSTAFF | to heat as butter; a man of continual dissolution |
| 2.739219044070792 | H4/2.4.115 | Henry IV | PRINCE HENRY | Didst thou never see Titan kiss a dish of butter? |
일반 인덱스와 달리 전문 검색 인덱스는 기본 데이터가 바뀌어도 자동 갱신되지 않습니다. 그래서 적절한 시점에 PRAGMA drop_fts_index(my_fts_index)로 내렸다가 다시 만들어야 해요.
코퍼스 테이블 만들기에 대한 참고
더 자세한 내용은 “Generating a Shakespeare corpus for full-text searching from JSON” 블로그 포스트를 참고하세요.
- 컬럼은 line_id, play_name, line_number, speaker, text_entry입니다.
- 전문 검색이 동작하려면 각 행에 고유 키가 필요해요.
line_id의KL/2.4.132는 King Lear, Act 2, Scene 4, Line 132를 의미합니다.
더 알아보기 (Learn more)
fts확장은 Okapi BM25 랭킹을 사용해 관련도 순으로 정렬해 줘요.- 전문 검색 인덱스는 데이터 변경 시 자동 갱신되지 않으므로, 데이터가 바뀌면 인덱스를 drop 후 재생성해야 합니다.