-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Explore Search Results #4960
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
Closed
Closed
Explore Search Results #4960
Changes from 6 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
16ebc51
Prepare issue branch.
mp911de c80f735
Explore returning Search Results.
mp911de 642396c
Add reactive search support.
mp911de a2833e2
Documentation.
mp911de b6d9efa
Documentation.
mp911de 6657508
Polishing.
mp911de cc7449d
Review findings.
mp911de 7fb9b2a
hacking
christophstrobl 1ac2c9f
are we done?
christophstrobl 72806aa
Update documentation
christophstrobl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
...java/org/springframework/data/mongodb/repository/query/VectorSearchDelegateUnitTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
/* | ||
* Copyright 2025 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.springframework.data.mongodb.repository.query; | ||
|
||
import static org.assertj.core.api.Assertions.*; | ||
import static org.mockito.Mockito.*; | ||
|
||
import java.lang.reflect.Method; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import org.springframework.data.domain.Limit; | ||
import org.springframework.data.domain.Score; | ||
import org.springframework.data.domain.SearchResults; | ||
import org.springframework.data.domain.Vector; | ||
import org.springframework.data.mapping.model.ValueExpressionEvaluator; | ||
import org.springframework.data.mongodb.core.aggregation.VectorSearchOperation; | ||
import org.springframework.data.mongodb.core.convert.MappingMongoConverter; | ||
import org.springframework.data.mongodb.core.convert.NoOpDbRefResolver; | ||
import org.springframework.data.mongodb.core.mapping.MongoMappingContext; | ||
import org.springframework.data.mongodb.repository.VectorSearch; | ||
import org.springframework.data.mongodb.util.json.ParameterBindingContext; | ||
import org.springframework.data.mongodb.util.json.ParameterBindingDocumentCodec; | ||
import org.springframework.data.projection.SpelAwareProxyProjectionFactory; | ||
import org.springframework.data.repository.Repository; | ||
import org.springframework.data.repository.core.RepositoryMetadata; | ||
import org.springframework.data.repository.core.support.AnnotationRepositoryMetadata; | ||
import org.springframework.data.repository.query.ValueExpressionDelegate; | ||
|
||
/** | ||
* Unit tests for {@link VectorSearchDelegate}. | ||
* | ||
* @author Mark Paluch | ||
*/ | ||
class VectorSearchDelegateUnitTests { | ||
|
||
MappingMongoConverter converter = new MappingMongoConverter(NoOpDbRefResolver.INSTANCE, new MongoMappingContext()); | ||
|
||
@Test | ||
void shouldConsiderDerivedLimit() throws ReflectiveOperationException { | ||
|
||
Method method = VectorSearchRepository.class.getMethod("searchTop10ByEmbeddingNear", Vector.class, Score.class); | ||
|
||
MongoQueryMethod queryMethod = getMongoQueryMethod(method); | ||
MongoParametersParameterAccessor accessor = getAccessor(queryMethod, Vector.of(1, 2), Score.of(1)); | ||
|
||
VectorSearchDelegate.QueryMetadata query = createQueryMetadata(queryMethod, accessor); | ||
|
||
assertThat(query.query().getLimit()).isEqualTo(10); | ||
assertThat(query.numCandidates()).isEqualTo(10 * 20); | ||
} | ||
|
||
@Test | ||
void shouldNotSetNumCandidates() throws ReflectiveOperationException { | ||
|
||
Method method = VectorSearchRepository.class.getMethod("searchTop10EnnByEmbeddingNear", Vector.class, Score.class); | ||
|
||
MongoQueryMethod queryMethod = getMongoQueryMethod(method); | ||
MongoParametersParameterAccessor accessor = getAccessor(queryMethod, Vector.of(1, 2), Score.of(1)); | ||
|
||
VectorSearchDelegate.QueryMetadata query = createQueryMetadata(queryMethod, accessor); | ||
|
||
assertThat(query.query().getLimit()).isEqualTo(10); | ||
assertThat(query.numCandidates()).isNull(); | ||
} | ||
|
||
@Test | ||
void shouldConsiderProvidedLimit() throws ReflectiveOperationException { | ||
|
||
Method method = VectorSearchRepository.class.getMethod("searchTop10ByEmbeddingNear", Vector.class, Score.class, | ||
Limit.class); | ||
|
||
MongoQueryMethod queryMethod = getMongoQueryMethod(method); | ||
MongoParametersParameterAccessor accessor = getAccessor(queryMethod, Vector.of(1, 2), Score.of(1), Limit.of(11)); | ||
|
||
VectorSearchDelegate.QueryMetadata query = createQueryMetadata(queryMethod, accessor); | ||
|
||
assertThat(query.query().getLimit()).isEqualTo(11); | ||
assertThat(query.numCandidates()).isEqualTo(11 * 20); | ||
} | ||
|
||
private VectorSearchDelegate.QueryMetadata createQueryMetadata(MongoQueryMethod queryMethod, | ||
MongoParametersParameterAccessor accessor) { | ||
|
||
VectorSearchDelegate delegate = new VectorSearchDelegate(queryMethod, converter, ValueExpressionDelegate.create()); | ||
|
||
return delegate.createQuery(mock(ValueExpressionEvaluator.class), queryMethod.getResultProcessor(), accessor, | ||
Object.class, new ParameterBindingDocumentCodec(), mock(ParameterBindingContext.class)); | ||
} | ||
|
||
private MongoQueryMethod getMongoQueryMethod(Method method) { | ||
RepositoryMetadata metadata = AnnotationRepositoryMetadata.getMetadata(method.getDeclaringClass()); | ||
return new MongoQueryMethod(method, metadata, new SpelAwareProxyProjectionFactory(), converter.getMappingContext()); | ||
} | ||
|
||
@NotNull | ||
private static MongoParametersParameterAccessor getAccessor(MongoQueryMethod queryMethod, Object... values) { | ||
return new MongoParametersParameterAccessor(queryMethod, values); | ||
} | ||
|
||
interface VectorSearchRepository extends Repository<WithVector, String> { | ||
|
||
@VectorSearch(indexName = "cos-index", searchType = VectorSearchOperation.SearchType.ANN) | ||
SearchResults<WithVector> searchTop10ByEmbeddingNear(Vector vector, Score similarity); | ||
|
||
@VectorSearch(indexName = "cos-index", searchType = VectorSearchOperation.SearchType.ENN) | ||
SearchResults<WithVector> searchTop10EnnByEmbeddingNear(Vector vector, Score similarity); | ||
|
||
@VectorSearch(indexName = "cos-index", searchType = VectorSearchOperation.SearchType.ANN) | ||
SearchResults<WithVector> searchTop10ByEmbeddingNear(Vector vector, Score similarity, Limit limit); | ||
|
||
} | ||
|
||
static class WithVector { | ||
|
||
Vector embedding; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
seems to have ended up in the wrong directory. should have been
src/test/java
, right?