0

I need to use dynamic index per request in spring data elastic search. I got it working by creating a @RequestScope object

how does singleton bean handle dynamic index

I am trying to get it working without creating @RequestScope object. I set the attributes in request. But I don't know to read it out in SpEL

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
        HttpServletRequest httpRequest = (HttpServletRequest) request;

        httpRequest.setAttribute("indexName", "indexA");


@Document(indexName = "#${request.getAttribute('indexName')}", createIndex = false) --not working
public class MyDocument{

I also try @Document(indexName = "#${@request.getAttribute('indexName')}", createIndex = false)

Abe
  • 310
  • 3
  • 15
  • That will never work... How should elasticsearch know while starting how to create an index? There is no request at the moment that information is needed... – M. Deinum May 10 '22 at 17:33
  • It can get the value from environment @Document(indexName = "#{@environment.getProperty('indexName')}", createIndex = false). Or from @RequestScope bean @Document(indexName = "#{@indexConfig.getAIndexName()}", createIndex = false) I am wondering if it can get it from some predefined request bean. – Abe May 10 '22 at 20:47
  • No it cannot. The environment is availabe during startup, as that is static. So no trying to get that from anything other then staticly available (request scoped or the request for instance) will simply not work. – M. Deinum May 11 '22 at 05:36

1 Answers1

0

Instead of trying to get this into a SpEL expression for the @Document annotation you'd better get the name for the index from the request in the function where you want to use Spring Data Elasticsearch methods - I don't just at them moment with which parameter you would get this.

Then use this name to create a IndexCoordinates object with that name and pass this as parameter to the ElasticsearchOperations method you use.

P.J.Meisch
  • 18,013
  • 6
  • 50
  • 66