davsclaus opened a new pull request, #3380:
URL: https://github.com/apache/camel-kamelets/pull/3380
Two new Kamelets, both **Preview**, that search an index as a step of a
route, such as looking up a customer or enriching an event:
`elasticsearch-search-action` (Camel `elasticsearch`) and
`opensearch-search-action` (Camel `opensearch`). Use from a YAML route as `to:
kamelet:elasticsearch-search-action`.
## Options
Same as the index sinks of the same system: `hostAddresses`, `clusterName`,
`indexName` (required), `user`, `password` (`format: password`), `enableSSL`
(default true), `certificate`, `connectionTimeout`, `socketTimeout`, plus
`sslContextParameters` for OpenSearch, as in `opensearch-index-sink`. New:
`size` (default 10), the largest number of documents in the body; it takes
precedence over a `size` in the search.
## Input
The body is the search:
- text that starts with `{`, or a Map (parsed JSON), is the Query DSL:
either a query alone (`{"match": {"city": "Oslo"}}`) or a `_search` request
body with a `query` (`sort`, `from`, `_source` and the rest work);
- any other text is searched for as words in all the fields (a `multi_match`
query, fixed in the template; the text goes in with `${escape(json)}`, so it
cannot add members to the request). An empty body finds nothing;
- bytes are read as text.
## Output
The body is a JSON array, as text, of the `_source` of the hits, e.g.
`[{"name": "Alice Smith", "city": "Oslo"}]`, or `[]`. The number of matches is
in `CamelElasticsearchTotalHits` / `CamelOpensearchTotalHits` (counted up to
10000, no value with `"track_total_hits": false`). `Content-Type` is
`application/json`. No client object leaves the Kamelet: the producer returns
`HitsMetadata`, the documents are read as Maps (`documentClass=java.util.Map`),
`forEach` takes their `_source`, and Jackson marshals the list. The old search
sources were removed in #3166 because Gson marshalled the Jackson `ObjectNode`
result as `{"_children": ...}`; this shape avoids that.
## Security
- The `CamelElasticsearch*` / `CamelOpensearch*` headers are removed before
the search (and after it), so a message cannot change the index, the operation,
the size, the document class (`CamelElasticsearchDocumentClass` names a class
to load) or turn on scrolling (`CamelElasticsearchUseScroll`). The test checks
that an `IndexName` header for another index has no effect.
- The body is the search by design, so whoever writes it can read every
document of the index. The Query DSL can also read from other indices (checked:
a `terms` lookup query takes its terms from a document of another index). The
description says so and asks to log in as a user that can read only that index.
- The plain-text form is not evaluated: the template is a fixed expression
and the text is JSON-escaped into it.
## Checked by running (both systems, ES 9.1.0 and OpenSearch 3.9.0)
Query alone, request body with `sort` and `size` (the option wins), plain
text with a quote in it, leading whitespace, empty body, bytes body, Map body,
`_source: false` (empty array, total still set), `track_total_hits` 1 and
false, 10005 documents (total 10000), invalid JSON, a missing index, a query
rejected by the server (all three fail the message), a
`CamelElasticsearchIndexName` header (ignored).
## Tests
`SearchIT` (existing class, packages `elasticsearch` and `opensearch`): new
`elasticsearch-search-action-route` and `opensearch-search-action-route` tests.
They index two customers and one document in another index, then search with a
Query DSL query while a header asks for the other index, with plain text, and
with a query that finds nothing, and check the body (JSON), its Java type, the
total and the Content-Type. Images from Docker Hub, pinned:
`elasticsearch:9.1.0`, `opensearchproject/opensearch:3.9.0`.
Local: `./mvnw -o clean install -DskipITs` passes; `./mvnw -o verify -pl
tests/camel-kamelets-itest -Dit.test=SearchIT`: Tests run: 6, Failures: 0,
Errors: 0.
Fixes #3376.
## Upgrade note
* New Kamelets `elasticsearch-search-action` and `opensearch-search-action`
(Preview): search an index of Elasticsearch or OpenSearch as a step of a route;
the body is the search (Query DSL as JSON, or plain words), and the result is a
JSON array of the documents found, with the number of matches in the
`CamelElasticsearchTotalHits` / `CamelOpensearchTotalHits` header.
_Claude Code on behalf of Claus Ibsen_
🤖 Generated with [Claude Code](https://claude.com/claude-code)
https://claude.ai/code/session_016Y4dDmBDsSWV117kccuNf9
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]