dadoonet / hsearch-es-demo

Hibernate Search and Elasticsearch demo
Apache License 2.0
21 stars 10 forks source link

Problem when trying to use spring-data #1

Open tbounsiar opened 6 years ago

tbounsiar commented 6 years ago

Hello everybody,

I am trying to configure hibernate search with spring boot data jpa and elastic-search, so this my confs

group "taharb.org"
version "1.0-SNAPSHOT"

apply plugin: "java"

ext {
    springBootVersion = "1.5.8.RELEASE"
    hibernateSearchVersion = "5.6.3.Final"
}

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    testCompile("junit:junit:4.12")

    compile("org.springframework.boot:spring-boot-starter-web:$springBootVersion")
    compile("org.springframework.boot:spring-boot-starter-data-jpa:$springBootVersion")

    compile("org.hibernate:hibernate-search-orm:$hibernateSearchVersion")
    compile("org.hibernate:hibernate-search-elasticsearch:$hibernateSearchVersion")
    compile("mysql:mysql-connector-java:6.0.6")
}

My model is this one

@Entity
@Indexed(index = "client_index")
public class Client {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;
    @Field(index = Index.YES, analyze = Analyze.YES, store = Store.NO)
    private String firstName;
    @Field(index = Index.YES, analyze = Analyze.YES, store = Store.NO)
    private String lastName;
    @Field
    @JsonFormat(pattern="yyyy-MM-dd")
    private Date birthDate;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public Date getBirthDate() {
        return birthDate;
    }

    public void setBirthDate(Date birthDate) {
        this.birthDate = birthDate;
    }
}

hibernate.properties

hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=true
hibernate.format_sql=true
hibernate.hbm2ddl.auto=create

hibernate.search.default.indexmanager=elasticsearch
hibernate.search.default.elasticsearch.index_schema_management_strategy=RECREATE_DELETE
hibernate.search.default.elasticsearch.required_index_status=yellow
hibernate.search.default.indexBase=D:/NoSQL/data/elasticsearch/manager/indexes

My Config Class

@Configuration
@PropertySource({"classpath:manager-database.properties"/*, "classpath:hibernate.properties"*/})
@EnableJpaRepositories(basePackages = {"org.taharb.manager.web.dao"},
        entityManagerFactoryRef = "managerEntityManager",
        transactionManagerRef = "managerTransactionManager")
public class ManagerDatabase {

    private final Environment environment;

    @Autowired
    public ManagerDatabase(Environment environment) {
        this.environment = environment;
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean managerEntityManager() {
        LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
        entityManagerFactoryBean.setDataSource(managerDataSource());
        entityManagerFactoryBean.setPackagesToScan(new String[]{"org.taharb.manager.web.model"});
        entityManagerFactoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());

        Properties properties = new Properties();
        try {
            properties.load(ManagerDatabase.class.getClassLoader().getResourceAsStream("hibernate.properties"));
        } catch (Exception e) {

        }
        entityManagerFactoryBean.setJpaProperties(properties);

        return entityManagerFactoryBean;
    }

    @Bean
    public DataSource managerDataSource() {

        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(environment.getProperty("manager.jdbc.driverClassName"));
        dataSource.setUrl(environment.getProperty("manager.jdbc.url"));
        dataSource.setUsername(environment.getProperty("manager.jdbc.username"));
        dataSource.setPassword(environment.getProperty("manager.jdbc.password"));

        return dataSource;
    }

    @Bean
    public PlatformTransactionManager managerTransactionManager() {
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(managerEntityManager().getObject());
        return transactionManager;
    }
}

and Finaly the search service

@Service
public class ClientSearch {

    private final EntityManagerFactory entityManagerFactory;

    @Autowired
    public ClientSearch(@Qualifier("managerEntityManager") EntityManagerFactory entityManagerFactory) {
        this.entityManagerFactory = entityManagerFactory;
    }

    public List<Client> searchByKeyword(final String keyword) {
        EntityManager entityManager = entityManagerFactory.createEntityManager();
        FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(entityManager);
        QueryBuilder queryBuilder = fullTextEntityManager.getSearchFactory().buildQueryBuilder().forEntity(Client.class).get();
        return fullTextEntityManager
                .createFullTextQuery(queryBuilder.keyword()
                        .onFields("firstName", "lastName")
                        .matching(keyword)
                        .createQuery(), Client.class).getResultList();
    }
}

So when i try to start the application i get this error


2017-10-23 23:06:09.505 ERROR 13452 --- [           main] o.s.boot.SpringApplication               : Application startup failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'managerEntityManager' defined in class path resource [org/taharb/manager/configuration/ManagerDatabase.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1078) ~[spring-context-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:857) ~[spring-context-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543) ~[spring-context-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
    at org.taharb.manager.configuration.Application.main(Application.java:12) [classes/:na]
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.persistenceException(EntityManagerFactoryBuilderImpl.java:954) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:882) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
    at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:60) ~[spring-orm-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:353) ~[spring-orm-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:370) ~[spring-orm-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:359) ~[spring-orm-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    ... 16 common frames omitted
Caused by: org.hibernate.search.exception.SearchException: HSEARCH400020: Could not create mapping for entity type org.taharb.manager.web.model.Client
    at org.hibernate.search.elasticsearch.schema.impl.ElasticsearchSchemaAccessor.putMapping(ElasticsearchSchemaAccessor.java:235) ~[hibernate-search-elasticsearch-5.6.3.Final.jar:5.6.3.Final]
    at org.hibernate.search.elasticsearch.schema.impl.DefaultElasticsearchSchemaCreator.createMappings(DefaultElasticsearchSchemaCreator.java:88) ~[hibernate-search-elasticsearch-5.6.3.Final.jar:5.6.3.Final]
    at org.hibernate.search.elasticsearch.impl.ElasticsearchIndexManager.initializeIndex(ElasticsearchIndexManager.java:298) ~[hibernate-search-elasticsearch-5.6.3.Final.jar:5.6.3.Final]
    at org.hibernate.search.elasticsearch.impl.ElasticsearchIndexManager.initializeIndex(ElasticsearchIndexManager.java:254) ~[hibernate-search-elasticsearch-5.6.3.Final.jar:5.6.3.Final]
    at org.hibernate.search.elasticsearch.impl.ElasticsearchIndexManager.setSearchFactory(ElasticsearchIndexManager.java:245) ~[hibernate-search-elasticsearch-5.6.3.Final.jar:5.6.3.Final]
    at org.hibernate.search.indexes.impl.IndexManagerHolder.setActiveSearchIntegrator(IndexManagerHolder.java:195) ~[hibernate-search-engine-5.6.3.Final.jar:5.6.3.Final]
    at org.hibernate.search.engine.impl.MutableSearchFactoryState.setActiveSearchIntegrator(MutableSearchFactoryState.java:230) ~[hibernate-search-engine-5.6.3.Final.jar:5.6.3.Final]
    at org.hibernate.search.spi.SearchIntegratorBuilder.buildNewSearchFactory(SearchIntegratorBuilder.java:230) ~[hibernate-search-engine-5.6.3.Final.jar:5.6.3.Final]
    at org.hibernate.search.spi.SearchIntegratorBuilder.buildSearchIntegrator(SearchIntegratorBuilder.java:120) ~[hibernate-search-engine-5.6.3.Final.jar:5.6.3.Final]
    at org.hibernate.search.hcore.impl.HibernateSearchSessionFactoryObserver.sessionFactoryCreated(HibernateSearchSessionFactoryObserver.java:75) ~[hibernate-search-orm-5.6.3.Final.jar:5.6.3.Final]
    at org.hibernate.internal.SessionFactoryObserverChain.sessionFactoryCreated(SessionFactoryObserverChain.java:35) ~[hibernate-core-5.1.7.Final.jar:5.1.7.Final]
    at org.hibernate.internal.SessionFactoryImpl.(SessionFactoryImpl.java:521) ~[hibernate-core-5.1.7.Final.jar:5.1.7.Final]
    at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:465) ~[hibernate-core-5.1.7.Final.jar:5.1.7.Final]
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:879) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
    ... 22 common frames omitted
Caused by: org.hibernate.search.exception.SearchException: HSEARCH400007: Elasticsearch request failed.
 Request:
========
Operation: PutMapping
URI:client_index/org.taharb.manager.web.model.Client/_mapping
Data:
{
  "properties": {
    "birthDate": {
      "type": "date",
      "boost": 1.0,
      "index": "not_analyzed",
      "store": false
    },
    "firstName": {
      "type": "string",
      "boost": 1.0,
      "index": "analyzed",
      "store": false,
      "analyzer": "default"
    },
    "id": {
      "type": "string",
      "boost": 1.0,
      "index": "not_analyzed",
      "store": true
    },
    "lastName": {
      "type": "string",
      "boost": 1.0,
      "index": "analyzed",
      "store": false,
      "analyzer": "default"
    }
  },
  "dynamic": "strict"
}
Response:
=========
Status: 400
Error message: {"root_cause":[{"type":"illegal_argument_exception","reason":"The [string] type is removed in 5.0 and automatic upgrade failed because parameters [boost] are not supported for automatic upgrades. You should now use either a [text] or [keyword] field instead for field [firstName]"}],"type":"illegal_argument_exception","reason":"The [string] type is removed in 5.0 and automatic upgrade failed because parameters [boost] are not supported for automatic upgrades. You should now use either a [text] or [keyword] field instead for field [firstName]"}
Cluster name: null
Cluster status: 400

    at org.hibernate.search.elasticsearch.impl.DefaultBackendRequestResultAssessor.checkSuccess(DefaultBackendRequestResultAssessor.java:94) ~[hibernate-search-elasticsearch-5.6.3.Final.jar:5.6.3.Final]
    at org.hibernate.search.elasticsearch.client.impl.JestClient.executeRequest(JestClient.java:183) ~[hibernate-search-elasticsearch-5.6.3.Final.jar:5.6.3.Final]
    at org.hibernate.search.elasticsearch.client.impl.JestClient.executeRequest(JestClient.java:176) ~[hibernate-search-elasticsearch-5.6.3.Final.jar:5.6.3.Final]
    at org.hibernate.search.elasticsearch.schema.impl.ElasticsearchSchemaAccessor.putMapping(ElasticsearchSchemaAccessor.java:232) ~[hibernate-search-elasticsearch-5.6.3.Final.jar:5.6.3.Final]
    ... 35 common frames omitted

Please any help to solve this, Thank you Regards

emmanuelbernard commented 6 years ago

Hi, I believe you use Elasticsearch 5. You should use Hibernate Search 5.8 if you are targeting Elasticsearch >2.x http://hibernate.org/search/releases/5.8/#whats-new