07-3. 전문 검색과 analyzer 인증서¶
BM25 검색과 전문 검색 index의 세대 검증을 본다.
전문 검색: BM25 와 술어¶
전문 검색은 Lance의 inverted index 를 쓴다. 순위 함수 bm25 는 RankedAccess 로, 술어 search, fuzzy, match_text 는 스캔의 필터로 들어간다. 둘 다 결국 Lance FullTextSearchQuery 하나로 바뀐다.
코드 분석: build_fts_query¶
crates/omnigraph/src/engine/scan.rs scan.rs L950-L966
fuzzy 가 Lance 질의로 바뀌는 부분을 본다.
query,
max_edits,
ty: _,
} => {
let Some(prop) = extract_property(field) else {
return Ok(None);
};
let q = resolve_to_string(query, params)?;
let edits = match max_edits.as_deref() {
Some(expr) => u32::try_from(resolve_to_int(expr, params)?)
.map_err(|_| OmniError::manifest("fuzzy max_edits must fit U32"))?,
None => 2,
};
(
prop,
lance_index::scalar::FullTextSearchQuery::new_fuzzy(q, Some(edits)),
)
search 와 match_text 는 같은 분기에서 FullTextSearchQuery::new 가 되고, fuzzy 는 max_edits 를 생략하면 2 다. 어느 경우든 마지막에 with_column(prop) 으로 속성 하나에 묶인다 (scan.rs L983-L986).
술어와 순위의 결합¶
한 스캔에 bm25 순위와 search 술어가 함께 있으면 문제가 생긴다. Lance 스캔은 전문 검색 질의를 하나만 받고, 그 질의가 점수를 만든다. 술어를 순위 질의에 합치면 술어의 일치가 BM25 점수에 섞인다. scan.rs L171-L221 은 이를 다음 순서로 푼다.
| 단계 | 처리 | 함수 |
|---|---|---|
| 1 | 각 필터에서 전문 검색 질의 추출 | search_filter_query |
| 2 | 순위와 같은 속성, 같은 텍스트인 술어는 제외 | search_filter_is_ranking |
| 3 | 남은 술어 질의를 하나로 결합 | conjoin_fts_queries |
| 4 | 결합 질의로 id 목록만 따로 조회 | search_filter_member_ids |
| 5 | id 목록을 id IN (...) 으로 필터에 추가 |
NodeRead::resolve |
| 6 | 순위 질의만 Lance full_text_search 로 실행 |
NodeRead::configure |
search_filter_member_ids 의 주석은 목적을 "Filter membership without adding the filter's score to the BM25 ranking" 으로 적는다. 순위가 없는 스캔은 결합 질의를 그대로 실행한다. 정확한 문자열 술어 contains, starts_with 는 전문 검색이 아니며 index 로 빨라지지도 않는다. contains 의 needle 결합은 Aho-Corasick 자동자로 처리하고, 그 연산자는 6장에서 다룬다.
부분 범위와 untrained segment¶
index 범위가 부분이어도 쿼리는 정확하다. Lance가 index 에 없는 fragment 를 같은 analyzer 로 scan 하기 때문이다. analyzer 정보는 postings 가 없는 segment 에도 실린다. dataset_index.rs L488-L493 의 is_untrained_full_text 는 fragment bitmap 이 비어 있고 postings 가 없는 segment 를 가리키고, 주석에 따르면 이 segment 는 선언된 열의 analyzer 만 선언한다. 사용자 문서의 "Every write records a declared full-text index's analyzer before its postings are built" 는 쓰기 경로가 이 segment 를 남긴다는 뜻이고, 그 쓰기 쪽은 9장에서 다룬다. 이런 segment 조차 없는 오래된 table version 은 FullTextIndexRequired 로 거부된다.
전문 검색 index 의 analyzer 인증서¶
Lance 10 에서 11 로 올라가며 영어 stemmer 가 rust-stemmers 에서 frostem 으로 바뀌었다. RFC 0043 은 Lance 10 으로 만든 index 를 Lance 11 로 읽으면 organism, university 검색이 오류 없이 0건을 돌려준 실험을 기록한다 (0043-full-text-index-compatibility.md). Lance가 저장하는 tokenizer 매개변수로는 stemmer 세대를 알 수 없으므로, Omnigraph는 index 디렉터리 안에 자체 인증서를 쓴다.
| 인증서 필드 | 내용 |
|---|---|
format_version |
1 |
index_uuid |
Lance index UUID |
analyzer_generation |
lance11.0.0-frostem1.20260821.3-v1 |
artifact_digest |
index details 와 파일 목록의 SHA-256 |
인증서 구조는 fts_compat.rs L27-L34, 세대 상수와 크기 상한은 fts_compat.rs L19-L25 에 있다. 파일 상한은 64 KiB, payload 상한은 4096 byte 다.
코드 분석: artifact_digest¶
crates/omnigraph-core/src/fts_compat.rs fts_compat.rs L296-L316
인증서가 어떤 값을 index 의 정체로 보는지 본다.
fn artifact_digest(index: &IndexMetadata, files: &[&IndexFile]) -> Result<String> {
let details = index
.index_details
.as_ref()
.filter(|details| IndexDetails(Arc::clone(details)).supports_fts())
.ok_or_else(|| {
rebuild_required(index, "full-text index details are missing or unsupported")
})?;
let mut digest = Sha256::new();
digest.update(b"omnigraph.fts-artifact.v1\0");
digest.update(index.index_version.to_le_bytes());
hash_bytes(&mut digest, details.type_url.as_bytes());
hash_bytes(&mut digest, &details.value);
for file in files.iter().filter(|file| file.path != CERTIFICATE_FILE) {
hash_bytes(&mut digest, file.path.as_bytes());
digest.update(file.size_bytes.to_le_bytes());
}
// Names, table versions, fragment coverage, and base paths can change
// without changing the immutable postings. They are not analyzer identity.
Ok(format!("{:x}", digest.finalize()))
}
digest 는 index 버전, details, 인증서를 뺀 파일 경로와 크기만 넣는다. index 이름, table version, fragment 범위, base path 는 postings 를 바꾸지 않고도 변할 수 있으므로 넣지 않는다. 그래서 shallow clone 이나 branch 로 위치가 바뀐 index 도 같은 인증서로 검증된다. 파일 목록 자체는 inventory 가 경로 중복이나 0 byte 파일이 없는지 먼저 확인한다.
fts_compat.rs L59-L106 의 write_certificate 는 새로 만든 로컬 index 에만 인증서를 쓴다. 이미 인증서가 있거나 base_id 가 있는 index 는 거부하므로 기존 index 를 사후에 인증할 수 없다. index 빌드 전체는 13장에서 다룬다.
스캔 시점 검증¶
crates/omnigraph-core/src/dataset_index.rs dataset_index.rs L100-L115 는 전문 검색 질의가 읽는 열의 전문 index 와, 종류를 알 수 없는(details 없음) index 를 모두 검증한다. 알 수 없는 것을 통과시키지 않는 fail closed 판단이다. 필터 식 안의 contains_tokens 호출도 FtsFilterDemand 가 찾아 검증 대상에 넣고, 첫 인자가 일반 열이 아니면 모든 열을 대상으로 삼는다. 이 검증은 Lance scanner 를 넘기기 직전 table_store.rs L310-L317 의 validated 에서 실행되고, 전문 검색이 없는 보통 읽기는 할당 없이 바로 돌아간다.
fts_compat.rs L109-L181 의 verify_index 는 Lance index 파일 parser 를 쓰지 않는다. 주석은 손상된 footer 가 검증 전에 메모리를 할당할 수 있다는 점을 이유로 든다. 실제 객체 크기를 먼저 읽고, 파일 목록의 크기와 같고 상한 안일 때만 범위 읽기를 한다. 검증 성공은 Lance 세션의 파일 metadata 캐시에만 남고 영속 codec 이 없으므로 프로세스를 넘지 않는다. 실패는 FullTextIndexRebuildRequired 오류다.
복구는 명시적 재빌드다. omnigraph.rs L4002-L4008 의 rebuild_full_text_indices_on_as 는 선택한 branch 의 현재 행으로 전문 index 를 모두 다시 만들고, CLI 명령은 omnigraph rebuild-full-text-indexes 다. 다른 branch 와 과거 snapshot 은 원래 index 를 유지한다.
주의: RFC 0043 은 "Upgrade to Lance 11.0.0" 을 전제로 쓰였지만, 기준 커밋의 작업 공간은 Lance
=13.0.0을 고정한다 (Cargo.toml L53). 세대 상수 이름은 여전히lance11.0.0-...이고, fts_compat.rs L20-L22 주석은 이 값이 실행 중인 Lance 버전이 아니라 analyzer 의미를 가리키며 감사한 Lance 13 기본 analyzer 가 11 에서 쓴 인증서와 호환된다고 적는다.