Abhishek Agarwal
Abhishek Agarwal

Reputation: 922

Elasticsearch with cassandra integration

I'm using a jhipster project with cassandra as its repository. Now i also want to integrate elasticsearch with the same project. But getting errors which seem to be that the cassandra is trying to read @Document as its annotation.

Caused by: org.springframework.data.cassandra.mapping.VerifierMappingExceptions: com.shoptell.backoffice.repository.dto.IndexedMergeProductInfoDTO:
Cassandra entities must have the @Table, @Persistent or @PrimaryKeyClass Annotation

    at org.springframework.data.cassandra.mapping.BasicCassandraPersistentEntityMetadataVerifier.verify(BasicCassandraPersistentEntityMetadataVerifier.java:45)
    at org.springframework.data.cassandra.mapping.BasicCassandraPersistentEntity.verify(BasicCassandraPersistentEntity.java:198)
    at org.springframework.data.mapping.context.AbstractMappingContext.addPersistentEntity(AbstractMappingContext.java:297)
    at org.springframework.data.mapping.context.AbstractMappingContext.addPersistentEntity(AbstractMappingContext.java:256)
    at org.springframework.data.mapping.context.AbstractMappingContext.initialize(AbstractMappingContext.java:372)
    at org.springframework.data.cassandra.mapping.BasicCassandraMappingContext.initialize(BasicCassandraMappingContext.java:79)
    at org.springframework.data.mapping.context.AbstractMappingContext.afterPropertiesSet(AbstractMappingContext.java:362)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1633)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1570)
    ... 144 more

IndexedMergeProductInfoDTO.java

import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;

@Document(indexName="mergeIndex",type="indexMergeProductInfo")
public class IndexedMergeProductInfoDTO {
    @Id
    private String id;
    private String name;
    private String metaCategory;
    private String categoryName; 
    private String subCategoryName; 
    private String productBrand; 
    private String productSubBrand; 
    private String series; 
    private String model;

CassandraDataAutoConfiguration.java

import com.datastax.driver.core.Cluster;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.boot.autoconfigure.AutoConfigurationPackages;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.cassandra.config.CassandraEntityClassScanner;
import org.springframework.data.cassandra.config.CassandraSessionFactoryBean;
import org.springframework.data.cassandra.convert.CassandraConverter;
import org.springframework.data.cassandra.convert.MappingCassandraConverter;
import org.springframework.data.cassandra.core.CassandraAdminOperations;
import org.springframework.data.cassandra.core.CassandraAdminTemplate;
import org.springframework.data.cassandra.mapping.BasicCassandraMappingContext;
import org.springframework.data.cassandra.mapping.CassandraMappingContext;

import javax.inject.Inject;

/**
 * {@link org.springframework.boot.autoconfigure.EnableAutoConfiguration Auto-configuration} for Spring Data's Cassandra support.
 * <p/>
 * Registers a {@link org.springframework.data.cassandra.config.CassandraSessionFactoryBean} a {@link org.springframework.data.cassandra.core.CassandraAdminOperations} a {@link org.springframework.data.cassandra.mapping.CassandraMappingContext} and a
 * {@link org.springframework.data.cassandra.convert.CassandraConverter} beans if no other beans of the same type are configured.
 * <p/>
 */
@Configuration
@ConditionalOnClass({Cluster.class, CassandraAdminOperations.class})
@EnableConfigurationProperties(CassandraProperties.class)
@AutoConfigureAfter(CassandraAutoConfiguration.class)
public class CassandraDataAutoConfiguration {

    @Inject
    BeanFactory beanFactory;

    @Inject
    private CassandraProperties properties;

    @Inject
    private Cluster cluster;

    @Bean
    @ConditionalOnMissingBean
    public CassandraSessionFactoryBean session() throws Exception {
        CassandraSessionFactoryBean session = new CassandraSessionFactoryBean();
        session.setCluster(this.cluster);
        session.setConverter(cassandraConverter());
        session.setKeyspaceName(properties.getKeyspaceName());
        return session;
    }

    @Bean
    @ConditionalOnMissingBean
    public CassandraAdminOperations cassandraTemplate() throws Exception {
        return new CassandraAdminTemplate(session().getObject(), cassandraConverter());
    }

    @Bean
    @ConditionalOnMissingBean
    public CassandraMappingContext cassandraMapping() throws ClassNotFoundException {
        BasicCassandraMappingContext bean = new BasicCassandraMappingContext();
        bean.setInitialEntitySet(CassandraEntityClassScanner.scan(AutoConfigurationPackages.get(beanFactory)));
        bean.setBeanClassLoader(beanFactory.getClass().getClassLoader());
        return bean;
    }

    @Bean
    @ConditionalOnMissingBean
    public CassandraConverter cassandraConverter() throws Exception {
        return new MappingCassandraConverter(cassandraMapping());
    }
}

What are the things need to be done to get elasticsearch running?

Upvotes: 0

Views: 747

Answers (1)

Julien Dubois
Julien Dubois

Reputation: 3688

JHipster can generate a Cassandra + Elasticsearch project -> from my understanding you didn't select that option when generating your project, but if you want to add Elasticsearch, just have a look at how we do it on a new project.

You could also re-generate your project, and add in your .yo-rc.json file:

"searchEngine": "elasticsearch"

Upvotes: 1

Related Questions