Skip to content
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

Annotation CompoundIndexes not creating indexes #434

Open
killesk opened this issue Dec 2, 2022 · 14 comments
Open

Annotation CompoundIndexes not creating indexes #434

killesk opened this issue Dec 2, 2022 · 14 comments
Assignees

Comments

@killesk
Copy link

killesk commented Dec 2, 2022

When I declare a class and set some compound indexs like so:

`
@document("positionPoints")
@CompoundIndexes(
CompoundIndex(name = "optimizingType_point", def = "{'optimizingType': 1, 'point': 1}"),
CompoundIndex(name = "optimizingType_routeId_position", def = "{'optimizingType': 1, 'routeId': 1, 'position': 1}"))
class PositionPoint {

@GeoSpatialIndexed(type = GeoSpatialIndexType.GEO_2DSPHERE)
var point: DoubleArray`

The CompoundIndexes don't appear to be created.

I get the following error

com.mongodb.MongoCommandException: Command failed with error 27 (IndexNotFound): '$geoNear requires a 2d or 2dsphere index, but none were found' on server localhost:35543. The full response is {"ok": 0.0, "errmsg": "$geoNear requires a 2d or 2dsphere index, but none were found", "code": 27, "codeName": "IndexNotFound"}

I'm using
spring-boot-starter-parent == 2.7.5
de.flapdoodle.embed.mongo == 3.5.3
spring.mongodb.embedded.version == 6.0.2

Any suggestions on how to have my compound index created automatically?

@michaelmosmann
Copy link
Member

@killesk .. currently i do not work myself with mongodb .. but i think spring should create this index.. maybe this blogposts has an hint: https://www.baeldung.com/spring-data-mongodb-index-annotations-converter

@killesk
Copy link
Author

killesk commented Dec 2, 2022

Hi Michael, Spring creates this index when running against a "real" mongodb, but does not when using this embedded mongo lib.

@michaelmosmann
Copy link
Member

@killesk .. hmm.. this is interesting..

(to be sure: i start a real mongodb.. its not a mock, fake, or some in memory magic.. )

@michaelmosmann
Copy link
Member

@killesk there is an autoIndexCreation property in MongoProperties .. are you sure that this is not set in production? (org.springframework.boot.autoconfigure.mongo.MongoProperties#autoIndexCreation called from here: org.springframework.boot.autoconfigure.data.mongo.MongoDataConfiguration#mongoMappingContext)

@michaelmosmann
Copy link
Member

@killesk is this still an issue?

@michaelmosmann
Copy link
Member

@killesk .. i close this issue.. just reopen, if this still is an problem.

@serbanpop
Copy link

@michaelmosmann I'm still experiencing the same issue:

@Document("requests") @CompoundIndex(name = "requests_ref-id_version_idx", def = "{ 'referenceId': 1, 'version': -1 }", unique = true) public class Request { ... }
The Spring property to auto-create indexes is set as well and tested against a production database: spring.data.mongodb.auto-index-creation=true

I'm using:
spring-boot-starter-data-mongodb-reactive:3.1.11
de.flapdoodle.embed.mongo:4.18.1

Programmatically creating the index from within the test works.

@michaelmosmann
Copy link
Member

@serbanpop i guess you do not have a public repo with some sample code? I will see if i can recreate this issue somehow.

@serbanpop
Copy link

serbanpop commented Jan 3, 2025

@michaelmosmann Unfortunately, it's proprietary code, but I can share the setup for starting the embedded Mongo - I hope this helps.

public class EmbeddedMongo {

    public static TransitionWalker.ReachedState<RunningMongodProcess> withEmbeddedMongo() {
        return Mongod.instance()
            .withDistributionBaseUrl(Start.to(DistributionBaseUrl.class)
                .initializedWith(DistributionBaseUrl.of("https://artifactory.url")))
            .withDownloadPackage(DownloadPackage.withDefaults()
                .withDownloadConfig(DownloadConfig.defaults()
                .withProxyFactory(proxyFactory())))
            .withNet(Start.to(Net.class)
                .initializedWith(Net.of("localhost", 27017, false)))
            .withMongodArguments(Start.to(MongodArguments.class)
                .initializedWith(MongodArguments.defaults()))
            .withAuth(true)
            .start(V7_0_14, withAdminCredentials());
    }

    public static Listener withAdminCredentials() {
        return ClientActions.setupAuthentication(new SyncClientAdapter(), "test-db",
            AuthenticationSetup.of(UsernamePassword.of("i-am-admin", "admin-password".toCharArray()))
         );  
    }

Within the tests, the configs rely on a pre-running static process:

private static TransitionWalker.ReachedState<RunningMongodProcess> runningMongo;

@BeforeAll
static void beforeAll() {
    runningMongo = withEmbeddedMongo();
}

@AfterAll
static void tearDown() {
    runningMongo.close();
}

@michaelmosmann
Copy link
Member

@serbanpop so you dont use the spring integration? I am not sure which part of spring data handles the index creation, but I would guess that I could make a difference.. I will see.

@serbanpop
Copy link

@michaelmosmann I'm not starting the Embedded Mongo using @DataMongoTest but programmatically. The test is annotated with @SpringBootTest for convenience to load all the converters and codecs

@michaelmosmann
Copy link
Member

@serbanpop i will see if i can find a way to deal with that..

@michaelmosmann
Copy link
Member

@serbanpop can you create a new issue for that or should we reopen this one?

@serbanpop
Copy link

@michaelmosmann can we reopen this one to avoid duplicates, please?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants