-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
18 changed files
with
630,166 additions
and
155 deletions.
There are no files selected for viewing
This file contains 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 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,35 @@ | ||
# Spring Data - Fragment SPI Example | ||
|
||
This project contains a sample using `spring.factories` to register implementation details for a repository extension for MongoDB Vector Search that lives outside of the project namespace. | ||
|
||
The project is divided into the `atlas-api`, providing the extension, and the `sample` using it. | ||
|
||
## atlas-api | ||
|
||
The `AtlasRepository` is the base interface containing a `vectorSearch` method that is implemented in `AtlasRepositoryFragment`. The configuration in `src/main/resources/META-INF/spring.factories` makes sure it is picked up by the spring data infrastructure. | ||
|
||
The implementation leverages `RepositoryMethodContext` to get hold of method invocation metadata to determine the collection name derived from the repositories domain type `<T>`. | ||
Since providing the metadata needs to be explicitly activated the `AtlasRepositoryFragment` uses the additional marker interface `RepositoryMetadataAccess` enabling the features for repositories extending the `AtlasRepository`. | ||
|
||
## sample | ||
|
||
The `MovieRepository` extends the `AtlasRepository` from the api project using a `Movie` type targeting the `movies` collection. No further configuration is needed to use the provided `vectorSearch` within the `MovieRepositoryTests`. | ||
|
||
The `Movies` class in `src/main/test` takes care of setting up required test data and indexes. | ||
|
||
## Running the sample | ||
|
||
The is using a local MongoDB Atlas instance bootstrapped by Testcontainers. | ||
Running the `MovieRepositoryTests` the `test/movies` collection will be populated with about 400 entries from the `mflix.embedded_movies.json` file. | ||
Please be patient while data is loaded into the database and the index created afterwards. | ||
Progress information will be printed to the log. | ||
```log | ||
INFO - com.example.data.mongodb.Movies: 73 - Loading movies mflix.embedded_movies.json | ||
INFO - com.example.data.mongodb.Movies: 90 - Created 420 movies in test.movies | ||
INFO - com.example.data.mongodb.Movies: 65 - creating vector index | ||
INFO - com.example.data.mongodb.Movies: 68 - index 'plot_vector_index' created | ||
``` | ||
Once data and index are available search result will be printed: | ||
```log | ||
INFO - ...mongodb.MovieRepositoryTests: 183 - Movie{id='66d6ee0937e07b74aa2939cc', ... | ||
``` |
This file contains 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
23 changes: 16 additions & 7 deletions
23
...b/fragment-spi/atlas-api/src/main/java/com/example/spi/mongodb/atlas/AtlasRepository.java
This file contains 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 |
---|---|---|
@@ -1,18 +1,27 @@ | ||
/* | ||
* Copyright 2024 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 com.example.spi.mongodb.atlas; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.beans.BeansException; | ||
import org.springframework.beans.factory.config.BeanPostProcessor; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.data.domain.Limit; | ||
import org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport; | ||
import org.springframework.stereotype.Component; | ||
|
||
/** | ||
* @author Christoph Strobl | ||
*/ | ||
public interface AtlasRepository<T> { | ||
|
||
List<T> vectorSearch(String index, String path, List<Double> vector, Limit limit); | ||
List<T> vectorSearch(String index, String path, List<Double> vector); | ||
} |
33 changes: 0 additions & 33 deletions
33
...las-api/src/main/java/com/example/spi/mongodb/atlas/AtlasRepositoryAutoConfiguration.java
This file was deleted.
Oops, something went wrong.
This file contains 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
38 changes: 0 additions & 38 deletions
38
...i/atlas-api/src/main/java/com/example/spi/mongodb/atlas/AtlasRepositoryPostProcessor.java
This file was deleted.
Oops, something went wrong.
16 changes: 0 additions & 16 deletions
16
...esources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
This file was deleted.
Oops, something went wrong.
This file contains 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 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 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 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
15 changes: 15 additions & 0 deletions
15
mongodb/fragment-spi/sample/src/main/java/com/example/data/mongodb/MovieRepository.java
This file contains 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.