HeLi
HeLi

Reputation: 21

adding indexes for spring data neo4j doesn't work

Hi all I am trying to add indexes for neo4j Database through the code. I have my configuration class setup like this.:

@Configuration
public class CreateIndex {

@Bean
public org.neo4j.ogm.config.Configuration configuration() {
   org.neo4j.ogm.config.Configuration configuration = new 
              org.neo4j.ogm.config.Configuration();

 configuration.autoIndexConfiguration()
 .setAutoIndex(AutoIndexMode.ASSERT.getName());

 configuration.driverConfiguration().setDriverClassName
 ("org.neo4j.ogm.drivers.embedded.driver.EmbeddedDriver");

 return configuration;
 }

  @Bean
  public SessionFactory sessionFactory() {
    return new SessionFactory(configuration(), "com.domain");
  }
}

and I have my domain class where I added the @Index annotation .

    @NodeEntity
    @Getter
    @NoArgsConstructor
    @Component
    public class Movie {
      @GraphId
      private Long id;

      @Index(unique = true)
      private String movieId;
    }

and I have my dependencies as

    <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-data-neo4j</artifactId>
         <version>1.4.0.RELEASE</version>
     </dependency>

In my logs I see that my Index query is being executed

n.o.d.e.request.EmbeddedRequest        : Request: CALL db.constraints() with params {}
2017-11-02 16:30:11.780  INFO 40073 --- [           main] o.n.o.d.e.request.EmbeddedRequest        : Request: CALL db.indexes() with params {}
2017-11-02 16:30:11.789  INFO 40073 --- [           main] o.n.o.d.e.request.EmbeddedRequest        : Request: CREATE INDEX ON :MOVIE(movieId ) with params {}

But when I try to see if the indexes exists or not by using :schema that doesn't return anything.

I was trying to execute the query using NEO4JTemplate but it is been deprecated you know how can I execute the queries on startup.

For example I want to add the index query by code during start of the application

CREATE INDEX ON :MOVIE(movieId) 

This works if I add manually, but how to add to the configuration class.

Thanks in advance

Upvotes: 0

Views: 336

Answers (1)

František Hartman
František Hartman

Reputation: 15086

Your configuration is mostly correct.

You don't say where you run the :schema command, but my guess is that it is a different database than one used in your app, because following configuration:

org.neo4j.ogm.config.Configuration configuration = new 
          org.neo4j.ogm.config.Configuration();
configuration.autoIndexConfiguration()
    .setAutoIndex(AutoIndexMode.ASSERT.getName());
configuration.driverConfiguration()
    .setDriverClassName("org.neo4j.ogm.drivers.embedded.driver.EmbeddedDriver");

will create impermanent database in temp directory, which gets deleted after application shutdown. The reason is that no URI is specified.

Specify database URI and check the schema in that database:

configuration.driverConfiguration()
    .setURI("....");

Upvotes: 1

Related Questions