Skip to content

GH-3527: Add Elasticsearch channel adapter - #11540

Open
sharanggupta wants to merge 3 commits into
spring-projects:mainfrom
sharanggupta:GH-3527
Open

sharanggupta wants to merge 3 commits into
spring-projects:mainfrom
sharanggupta:GH-3527

Conversation

@sharanggupta

@sharanggupta sharanggupta commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #3527

This is the first step agreed in #3527: a new spring-integration-elasticsearch module with a reactive ElasticsearchMessageHandler on top of Spring Data Elasticsearch's ReactiveElasticsearchOperations. The Java DSL and an inbound channel adapter would follow in separate PRs.

ElasticsearchMessageHandler

It extends AbstractReactiveMessageHandler, like ReactiveMongoDbStoringMessageHandler, and performs one of two operations. Like setQueryType() and setQueryTypeExpression() of the R2dbcMessageHandler, the operation is set with setType() or evaluated for each message with setTypeExpression(), so one handler can index some messages and delete others, for example the changes captured from a database:

  • INDEX (the default) saves the payload:
    • an entity is saved with its mapping, into the index of its @Document annotation;
    • a Map, or a String with JSON, is saved into the configured index, with the value of its id field as its id, or an id that Elasticsearch generates when it has none;
    • the elements of a Collection are saved the same way, with one bulk request. Without an index name, they must be entities of exactly the same class, so that none lands in another class's index.
  • DELETE deletes the document with the id of the idExpression. Without one, the payload is the entity to delete. A String or Map payload needs an idExpression, so a JSON document payload is never mistaken for an id.

The indexName and indexNameExpression options set the index. They override the @Document index of an entity, and they are required for Map and JSON documents and for a deletion by id. Errors from the expressions, the JSON parsing and the checks are signalled through the returned Mono.

Spring Data's reactive template takes the id of a Map, and so of a Document parsed from JSON, from its id key, so a redelivered Map or JSON document with an id field replaces the document it indexed. Only a document without one gets a generated id, and the docs say that such a message is indexed again when it is redelivered. ReactiveElasticsearchOperations has no index(IndexQuery), so the id expression is only evaluated for DELETE. If an id from elsewhere in the message, such as a header, matters for raw documents, it could come later.

Version

I used @since 7.3 and "starting with version 7.3" in the docs, since this is for the next version, as you said on the issue. I didn't add a "What's New" entry, because the current one is for 7.2. I can add it once main moves to 7.3, or now if you prefer.

Testing

Test first: ElasticsearchMessageHandlerTests failed to compile, then failed on the missing behaviour before each part was implemented. It runs 19 tests against an Elasticsearch 9.5.5 Testcontainers container (the client version of Spring Data Elasticsearch 6.2), with security disabled. With security on, the first requests sometimes failed with 503 "primary shard for [.security-7] is unavailable". The tests cover:

  • entities, collections and an index name expression;
  • Map and JSON documents, with and without an id field, and a collection of Maps;
  • deletion by entity and by id expression, and a type expression that indexes and then deletes with the same handler;
  • the errors for entities of different classes or of a subclass without an index, a null element, a Map without an index or with non-String keys, a Collection to delete, a String to delete without an id expression, and an id expression that evaluates to null.

The test client is built from Rest5Clients and Rest5ClientTransport, because ElasticsearchClients.createReactive(ClientConfiguration) builds the legacy RestClient, which is an optional dependency of Spring Data Elasticsearch.

./gradlew :spring-integration-elasticsearch:check :spring-integration-elasticsearch:javadoc passes.

Fixes: spring-projects#3527

There is no Elasticsearch support in Spring Integration, so flows have
to call the Elasticsearch client from a service activator.

* Add the `spring-integration-elasticsearch` module, based on Spring Data
  Elasticsearch, as the first step agreed in the issue
* Add the reactive `ElasticsearchMessageHandler`: with the `INDEX` type
  it saves an entity, a `Map` or a JSON `String` payload, and the
  elements of a `Collection` payload with one bulk request; with the
  `DELETE` type it deletes the document of the payload or of the id
  expression
* Let the index name come from an expression, for documents without an
  entity mapping or to override the index of an entity
* Document the adapter and add it to the endpoint summary

The Java DSL and an inbound channel adapter are left for next steps.

Signed-off-by: Sharang Gupta <[email protected]>
Like `setQueryType()` and `setQueryTypeExpression()` of the
`R2dbcMessageHandler`, the operation can now be evaluated for each
message, so that one handler can index some messages and delete others,
for example the changes captured from a database.

* Replace the `Type` constructor argument with `setType()` and
  `setTypeExpression()`
* Evaluate the `idExpression` only for the `DELETE` operation
* Require an `idExpression` to delete by id, instead of taking a
  `String` payload as the id, which a JSON document payload could be
* Reject `Map` payloads with keys that are not `String` field names
* Link the Spring Data Elasticsearch object mapping documentation

Signed-off-by: Sharang Gupta <[email protected]>
Spring Data Elasticsearch indexes a Map, and so the Document parsed
from a JSON String, with the value of its `id` key as the document id.
So a redelivered Map or JSON document with an `id` field replaces the
document it indexed, and only a document without one gets an id that
Elasticsearch generates.

Correct the Javadoc and the reference docs, which said that the id was
always generated, and cover it with a test.

Signed-off-by: Sharang Gupta <[email protected]>
@artembilan artembilan added the status: next Mark accepted PR but for the next major/minor label Oct 29, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status: next Mark accepted PR but for the next major/minor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Elasticsearch support

2 participants