전문(Full-Text) 검색과 메타데이터 필터

전문(Full-Text) 검색과 메타데이터 필터

컬렉션에서 데이터를 꺼내는 방법은 크게 두 가지예요. Query는 dense 벡터 유사도 기반의 최근접 이웃 검색이고, Get은 유사도 순위 없이 레코드를 조회할 때 써요.

출처: https://docs.trychroma.com/docs/querying-collections/full-text-search

Query

collection.query(
    query_texts=["thus spake zarathustra", "the oracle speaks"]
)

컬렉션에 임베딩 함수가 없으면 query_embeddings를 직접 줘야 하고, 쿼리 임베딩의 차원은 컬렉션의 임베딩 차원과 같아야 해요. Python은 query_images, query_uris도 지원해요.

collection.query(
    query_embeddings=[[11.1, 12.1, 13.1], [1.1, 2.3, 3.2]],
    n_results=100
)

기본은 쿼리당 10개 결과예요. n_results로 바꾸고, ids로 특정 ID만 검색하게 제한할 수 있어요.

collection.query(
    query_embeddings=[[11.1, 12.1, 13.1], [1.1, 2.3, 3.2]],
    n_results=100,
    ids=["id1", "id2"]
)

queryget 모두 메타데이터 필터링에 where, 전문 검색·정규식에 where_document를 지원해요.

collection.query(
    query_embeddings=[[11.1, 12.1, 13.1], [1.1, 2.3, 3.2]],
    n_results=100,
    where={"page": 10},  # metadata field 'page' equal to 10
    where_document={"$contains": "search string"}
)

Get

유사도 순위 없이 ID나 필터로 레코드를 꺼낼 때는 .get을 써요.

collection.get(ids=["id1", "id2"])   # by IDs
collection.get(limit=100, offset=0)  # with pagination

결과 구조

.query.get 결과는 column-major(필드별 배열) 형태로 돌아와요. .query는 입력 쿼리별로 그룹핑되고, .get은 평평한 레코드 리스트예요. 기본적으로 Query는 documents, metadatas, distances를, Get은 documents, metadatas를 반환해요. 반환 항목은 include로 조절하고, ids는 항상 포함돼요.

collection.query(
    query_texts=["my query"],
    include=["documents", "metadatas", "embeddings"],
)

더 알아보기