0

I am using:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.6.RELEASE</version>
    <relativePath/>
</parent>

...

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-releasetrain</artifactId>
            <version>Moore-SR6</version>
            <scope>import</scope>
            <type>pom</type>
        </dependency>
    </dependencies>
</dependencyManagement>

Elasticsearch version: 6.8

I am defining my @Document class like:

@Document(indexName = "test-spring", type = "_doc")
public class Domain
{
    @Id
    private String id;

    @Field(name = "domain_name", type = Text)
    private String domainName;

    ........getters and setters
}

The ES document gets indexed but with field domainName, I would expect to be "domain_name" as it is specified in the @Field annotation.

I have also tried @JsonProperty("domain_name") in such case the field is ignored.

is this a known issue?

thank you.

Edit (copied pom):

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.6.RELEASE</version>
        <relativePath/>
    </parent>
    <groupId>com.test</groupId>
    <artifactId>spring-es-test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-es-test</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <elasticsearch.version>6.8.7</elasticsearch.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.data</groupId>
                <artifactId>spring-data-releasetrain</artifactId>
                <version>Moore-SR6</version>
                <scope>import</scope>
                <type>pom</type>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.awaitility</groupId>
            <artifactId>awaitility</artifactId>
            <version>3.1.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.6</version>
        </dependency>
        <dependency>
            <groupId>com.opencsv</groupId>
            <artifactId>opencsv</artifactId>
            <version>5.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
6
  • How do you configure the connection to Elasticsearch? Do you have a custom configuration for Spring Data Elasticsearch? And is the index already created in Elasticsearch or is this autocreated? Commented Mar 29, 2020 at 11:40
  • I tried both autocreating it and creating it myself before indexing. The configuration to elasticsearch is just: elasticsearch: rest: uris: localhost:9200 read-timeout: 10s Commented Mar 29, 2020 at 13:08
  • can you please add the relevant part of your application configuration? Commented Mar 29, 2020 at 14:25
  • this is all config I have: ``` spring: jpa: hibernate: ddl-auto: none datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://${MYSQL_HOST:localhost}:3306/database username: username password: password elasticsearch: rest: uris: localhost:9200 read-timeout: 10s username: password: management: endpoint: health: show-details: "ALWAYS" ``` that's all I have configured. Documents get indexed but with java class attribute names not the ones define in @Field Commented Mar 29, 2020 at 15:07
  • please add the complete maven dependencies in your question (and not as comment), otherwise it's not possible to reproduce this. Commented Mar 29, 2020 at 16:55

1 Answer 1

1

A little technical background: Spring Data Elasticsearch (in that version) internally uses an implementation of the interface EntityMapper to convert the entities into the JSON format needed for Elasticsearch and back.

The Spring Boot autoconfiguration instantiates an ElasticsearchRestTemplate which internally uses the DefaultEntityMapper that is just a thin wrapper around Jackson. This implementation causes many problems, one of these hit you, and that's the custom naming of properties.

So from version 3.2 on there is an alternative implementation available, the ElasticsearchEntityMapper, but this must be explicitly configured. You will need to add a configuration class like this to your application:

@Configuration
public class RestClientConfig extends AbstractElasticsearchConfiguration {

    @Override
    @Primary
    @Bean
    public RestHighLevelClient elasticsearchClient() {

        ClientConfiguration clientConfiguration = ClientConfiguration.builder() //
                .connectedTo("localhost:9200") //
                .build();

        return RestClients.create(clientConfiguration).rest();
    }

    @Bean
    @Override
    public EntityMapper entityMapper() {

        ElasticsearchEntityMapper entityMapper = new ElasticsearchEntityMapper(
                elasticsearchMappingContext(), new DefaultConversionService()
        );
        entityMapper.setConversions(elasticsearchCustomConversions());

        return entityMapper;
    }
}

Please note, that here the Elasticsearch host is given without the http:// prefix, you may want to add a different property to your configuration and autowire this with a $Value property into this configuration class.

Note:

From the next major version on (4.0), the old Jackson based EntityMapper will not be available anymore, and this custom configuration of the EntityMapper will not be needed.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.