Search code examples
spring-bootspring-annotationsspring-data-elasticsearchspring-el

SpEL used in @Document indexName with spring data elasticsearch and spring boot is not being parsed


looking for some help using the SpEL inside @Document annotation in reference to:

spring-data-elasticsearch:3.2.3.RELEASE and spring boot 2.2.1 RELEASE

i am having trouble googling for help with this problem as the keywords pick up unrelated questions (i have seen the other (unanswered) question about dynamic indexName).

i would like to set the

@Document(indexName = "${es.index-name}", ...)

with the value for the indexName derived from a property (es.index-name) value written in my application.properties.

it is instead using the literal String value "${es.index-name}" as the index name!

i have also tried creating a @Component called EsConfig

with a field indexName annotated with @Value("${es.index-name}")

and then trying to access this component property value using SpEL:

@Document(indexName = "#{esConfig.indexName}", ...)

but this does not work either (still parsing as literal String and complains of uppercase). i have confirmed through the debugger that the EsConfig component IS parsing the SpEL correctly and providing the right value. but it fails when reaching @Document

here are the full code snippets:

using @Document with SpEL accessing application.properties

import lombok.Data;
import org.springframework.data.elasticsearch.annotations.Document;

import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Data
@Document(indexName = "${es.index-name}", type = "tests")
public class TestDocument {
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private String id;
}

EsConfig data source Component (tried with and without using Lombok)

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component("esConfig")
public class EsConfig {
  @Value("${es.index-name}")
  private String indexName;

  public String getIndexName() {
    return indexName;
  }

  public void setIndexName(String indexName) {
    this.indexName = indexName;
  }
}

using @Document with SpEL accessing EsConfig indexName property

@Data
@Document(indexName = "#{esConfig.indexName}", type = "tests")
public class TestDocument {
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private String id;
}

Solution

  • Reference your bean with the name and method:

    @Document(indexName = "#{@esConfig.getIndexName()}")