개요
이 가이드 에서는 코틀린 동기 (Kotlin Sync) 운전자 사용하여 Atlas Vector Search 쿼리를 수행하는 방법을 학습 수 있습니다. Aggregates
빌더 클래스는 $vectorSearch 파이프라인 단계를 만드는 데 사용할 수 있는 vectorSearch()
헬퍼 메서드를 제공합니다.
벡터 검색 수행
Atlas Vector Search 쿼리를 수행하려면 먼저 컬렉션 에 Atlas Vector Search 인덱스 만들어야 합니다. 프로그래밍 방식으로 벡터 검색 인덱스 만드는 방법을 학습하려면 Atlas Search 및 Vector Search 인덱스 가이드를 참조하세요.
그런 다음 집계 파이프라인 에서 vectorSearch()
메서드를 사용하여 Atlas Vector Search 쿼리 실행 수 있습니다. 이 메서드는 다음 매개변수를 허용합니다:
path
: 검색 할 필드queryVector
: 검색 쿼리 나타내는 벡터 임베딩indexName
: 사용할 Atlas Vector Search 인덱스 의 이름입니다.limit
: 반환할 최대 결과 수options
: (선택 사항) 벡터 검색 쿼리 구성하는 데 사용할 수 있는 옵션 설정하다 .
기본 벡터 검색 예제
이 예시 다음 조치를 수행하는 Atlas Vector Search 쿼리 실행합니다.
plot_embedding
벡터 필드 쿼리합니다.결과를
5
문서로 제한합니다.150
후보를 고려하는 근사치 근접 이웃(ANN) 벡터 검색 지정합니다. ANN 검색에 대해 자세히 학습하려면 MongoDB Atlas 문서에서 ANN 검색 을 참조하세요.
val vectorValues = FloatArray(1536) { i -> (i % 10).toFloat() * 0.1f } val queryVector = BinaryVector.floatVector(vectorValues) val indexName = "<vector search index>" // Specifies the path of the field to search val fieldSearchPath: FieldSearchPath = fieldPath("plot_embedding") // Creates the vector search pipeline stage with a limit and numCandidates val pipeline: List<Bson> = listOf( vectorSearch( fieldSearchPath, queryVector, indexName, 5L, approximateVectorSearchOptions(150) ), project( Projections.fields( Projections.excludeId(), Projections.include("title") ) ) ) val results = collection.aggregate(pipeline) results.forEach { doc -> println(doc.toJson()) }
{"title": "Berserk: The Golden Age Arc I - The Egg of the King"} {"title": "Rollerball"} {"title": "After Life"} {"title": "What Women Want"} {"title": "Truth About Demons"}
팁
쿼리 벡터 유형
앞의 예시 에서는 쿼리 벡터로 제공 BinaryVector
인스턴스 만들지만 Double
인스턴스의 List
을 만들 수도 있습니다. 그러나 저장 효율성 높이려면 BinaryVector
유형을 사용하는 것이 좋습니다.
벡터 검색 점수 예시
다음 예시 이전 예시 와 동일한 벡터 검색 쿼리 실행 하고 문서의 벡터 검색 메타 점수를 출력하는 방법을 보여 줍니다. 이 점수는 쿼리 벡터에 대한 각 문서 의 관련성을 나타냅니다.
val pipeline: List<Bson> = listOf( vectorSearch( fieldSearchPath, queryVector, indexName, 5L, approximateVectorSearchOptions(150) ), project( Projections.fields( Projections.excludeId(), Projections.include("title"), Projections.metaVectorSearchScore("score") ) ) ) val results = collection.aggregate(pipeline) results.forEach { doc -> println("Title: ${doc.getString("title")}, Score: ${doc.getDouble("score")}") }
Title: Berserk: The Golden Age Arc I - The Egg of the King, Score: 0.49899211525917053 Title: Rollerball, Score: 0.4976102113723755 Title: After Life, Score: 0.4965665936470032 Title: What Women Want, Score: 0.49622756242752075 Title: Truth About Demons, Score: 0.49614521861076355
팁
Vector Search 튜토리얼
Atlas Vector Search 쿼리를 실행 방법을 보여주는 더 많은 튜토리얼을 보려면 MongoDB Atlas 설명서에서 Atlas Vector Search 튜토리얼 을 참조하세요.
API 문서
이 가이드에서 사용되는 메서드 또는 유형에 대해 자세히 알아보려면 다음 API 문서를 참조하세요.