-
Notifications
You must be signed in to change notification settings - Fork 10
DOCSP-51350: Vector search queries #117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package org.example | ||
|
||
import com.mongodb.ConnectionString | ||
import com.mongodb.kotlin.client.MongoClient | ||
import com.mongodb.MongoClientSettings | ||
import com.mongodb.client.model.Aggregates.project | ||
import com.mongodb.client.model.Aggregates.vectorSearch | ||
import com.mongodb.client.model.search.FieldSearchPath | ||
import com.mongodb.client.model.Projections | ||
import com.mongodb.client.model.search.SearchPath.fieldPath | ||
import org.bson.BinaryVector | ||
import org.bson.conversions.Bson | ||
import com.mongodb.client.model.search.VectorSearchOptions.approximateVectorSearchOptions | ||
import org.bson.Document | ||
|
||
fun main() { | ||
val uri = "<connection string>" | ||
|
||
val settings = MongoClientSettings.builder() | ||
.applyConnectionString(ConnectionString(uri)) | ||
.retryWrites(true) | ||
.build() | ||
|
||
val mongoClient = MongoClient.create(settings) | ||
val database = mongoClient.getDatabase("sample_mflix") | ||
val collection = database.getCollection<Document>("embedded_movies") | ||
|
||
// start-vs | ||
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 numCandidates and limit | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. given |
||
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()) | ||
} | ||
// end-vs | ||
|
||
// start-vs-score | ||
val pipeline: List<Bson> = listOf( | ||
vectorSearch( | ||
fieldSearchPath, | ||
queryVector, | ||
indexName, | ||
5L, // limit parameter | ||
approximateVectorSearchOptions(150) | ||
), | ||
project( | ||
Projections.fields( | ||
Projections.excludeId(), | ||
Projections.include("title"), | ||
Projections.metaVectorSearchScore("score") | ||
) | ||
) | ||
) | ||
|
||
val results = collection.aggregate(pipeline) | ||
|
||
results.forEach { doc -> | ||
val score = doc.getDouble("vectorSearchScore") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we really need the above statement? I assume There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No not necessary, I meant to take that line out - fixed! |
||
println("Title: ${doc.getString("title")}, Score: ${doc.getDouble("score")}") | ||
} | ||
// end-vs-score | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops thank you