검색 패턴과 기초

검색 패턴과 기초 (Search patterns and basics)

Weaviate에서 데이터를 검색하는 방법은 크게 세 갈래예요. 벡터 유사도 검색, 키워드 검색, 그리고 둘을 섞은 하이브리드 검색. 이 페이지는 그 기초가 되는 검색 문법을 다룹니다—어떤 객체 프로퍼티메타데이터를 반환할지 제어하는 방법까지요.

출처: 공식문서

객체 나열하기

아무 파라미터 없이 객체를 가져올 수 있어요. UUID 오름차순으로 반환됩니다.

jeopardy = client.collections.use("JeopardyQuestion")
response = jeopardy.query.fetch_objects()
for o in response.objects:
    print(o.properties)

query가 반환할 정보(객체 프로퍼티, 객체 ID, 객체 메타데이터)를 명시할 수도 있어요.

limit으로 반환 객체 제한

limit으로 반환할 최대 객체 수를 정합니다.

response = jeopardy.query.fetch_objects(limit=1)

limitoffset으로 페이지네이션

결과 집합 중간에서 시작하려면 offset을 정의하고, offset부터 반환할 개수를 limit으로 정합니다.

response = jeopardy.query.fetch_objects(
    limit=1,
    offset=1
)

전체 DB를 페이지네이션하려면 offset/limit 대신 cursor를 쓰는 게 좋아요.

객체 properties 조회

어떤 객체 프로퍼티를 반환할지 지정할 수 있어요. 기본값은 모든 프로퍼티와 객체 UUID입니다. Blob과 reference 프로퍼티는 별도 지정하지 않으면 제외됩니다(Go 클라이언트 제외).

response = jeopardy.query.fetch_objects(
    limit=1,
    return_properties=["question", "answer", "points"]
)

객체 vector 조회

객체 벡터를 조회할 수 있어요. named vectors를 쓸 때도 적용됩니다.

response = jeopardy.query.fetch_objects(
    include_vector=True,
    limit=1
)
print(response.objects[0].vector["default"])

객체 id 조회

객체 id(uuid)를 조회할 수 있어요. (v4 클라이언트는 기본적으로 포함합니다.)

response = jeopardy.query.fetch_objects(limit=1)
for o in response.objects:
    print(o.uuid)

크로스 레퍼런스된 프로퍼티 조회

크로스 레퍼런스된 객체의 프로퍼티를 조회하려면, 크로스 레퍼런스 프로퍼티·대상 컬렉션·조회할 프로퍼티를 지정합니다.

from weaviate.classes.query import QueryReference

response = jeopardy.query.fetch_objects(
    return_references=[
        QueryReference(
            link_on="hasCategory",
            return_properties=["title"]
        ),
    ],
    limit=2
)

메타데이터 값 조회

객체 메타데이터 필드를 지정해 반환할 수 있어요.

from weaviate.classes.query import MetadataQuery

response = jeopardy.query.fetch_objects(
    limit=1,
    return_metadata=MetadataQuery(creation_time=True)
)
for o in response.objects:
    print(o.properties)
    print(o.metadata.creation_time)

전체 메타데이터 필드 목록은 GraphQL: Additional properties를 참고하세요.

:::tip 쿼리 성능 디버깅 query profiling으로 어떤 검색 쿼리의 샤드별 시간 분해를 볼 수 있어요. MetadataQueryquery_profile=True를 추가하면 각 단계가 얼마나 걸리는지 확인됩니다. :::

멀티테넌시

멀티테넌시가 활성화되면 각 쿼리에 tenant 파라미터를 지정합니다.

mt_collection = client.collections.use("WineReviewMT")
collection_tenant_a = mt_collection.with_tenant("tenantA")

response = collection_tenant_a.query.fetch_objects(
    return_properties=["review_body", "title"],
    limit=1,
)

복제(Replication)

복제가 활성화된 컬렉션은 쿼리에서 일관성 레벨을 지정할 수 있어요. CRUD 쿼리와 검색 모두에 적용됩니다.

더 알아보기 (Learn more)