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;
}
The ElasticsearchTemplate class is deprecated as it uses the TransportClient to access Elasticsearch, which itself is deprecated since Elasticsearch version 7. + Users should switch to ElasticsearchRestTemplate or ReactiveElasticsearchTemplate .
Elasticsearch is built on top of Apache Lucene and was first released by Elasticsearch N.V. (now Elastic) in 2010. According to the website of Elastic, it is a distributed open-source search and analytics engine for all types of data, including textual, numerical, geospatial, structured, and unstructured.
ElasticSearch is well known as a search engine, also working well as document based NoSQL. Spring Data ElasticSearch adds basic Reactive support.
The Spring Data Elasticsearch project provides integration with the Elasticsearch search engine. Key functional areas of Spring Data Elasticsearch are a POJO centric model for interacting with a Elastichsearch Documents and easily writing a Repository style data access layer.
Reference your bean with the name and method:
@Document(indexName = "#{@esConfig.getIndexName()}")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With