Repository navigation
GH-3527: Add Elasticsearch channel adapter - #11540
Open
sharanggupta wants to merge 3 commits into
Open
sharanggupta wants to merge 3 commits into
sharanggupta wants to merge 3 commits into
Conversation
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]>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #3527
This is the first step agreed in #3527: a new
spring-integration-elasticsearchmodule with a reactiveElasticsearchMessageHandleron top of Spring Data Elasticsearch'sReactiveElasticsearchOperations. The Java DSL and an inbound channel adapter would follow in separate PRs.ElasticsearchMessageHandlerIt extends
AbstractReactiveMessageHandler, likeReactiveMongoDbStoringMessageHandler, and performs one of two operations. LikesetQueryType()andsetQueryTypeExpression()of theR2dbcMessageHandler, the operation is set withsetType()or evaluated for each message withsetTypeExpression(), so one handler can index some messages and delete others, for example the changes captured from a database:INDEX(the default) saves the payload:@Documentannotation;Map, or aStringwith JSON, is saved into the configured index, with the value of itsidfield as its id, or an id that Elasticsearch generates when it has none;Collectionare 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.DELETEdeletes the document with the id of theidExpression. Without one, the payload is the entity to delete. AStringorMappayload needs anidExpression, so a JSON document payload is never mistaken for an id.The
indexNameandindexNameExpressionoptions set the index. They override the@Documentindex of an entity, and they are required forMapand JSON documents and for a deletion by id. Errors from the expressions, the JSON parsing and the checks are signalled through the returnedMono.Spring Data's reactive template takes the id of a
Map, and so of aDocumentparsed from JSON, from itsidkey, so a redeliveredMapor JSON document with anidfield 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.ReactiveElasticsearchOperationshas noindex(IndexQuery), so the id expression is only evaluated forDELETE. 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.3and "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 oncemainmoves to 7.3, or now if you prefer.Testing
Test first:
ElasticsearchMessageHandlerTestsfailed 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:Mapand JSON documents, with and without anidfield, and a collection ofMaps;nullelement, aMapwithout an index or with non-Stringkeys, aCollectionto delete, aStringto delete without an id expression, and an id expression that evaluates tonull.The test client is built from
Rest5ClientsandRest5ClientTransport, becauseElasticsearchClients.createReactive(ClientConfiguration)builds the legacyRestClient, which is an optional dependency of Spring Data Elasticsearch../gradlew :spring-integration-elasticsearch:check :spring-integration-elasticsearch:javadocpasses.