opensearch-java
opensearch-java copied to clipboard
Creating API objects from JSON data
Is your feature request related to a problem?
It is currently not possible to create API objects from JSON data.
What solution would you like?
For example we would like to do something like code below:
InputStream input = this.getClass()
.getResourceAsStream("some-index.json");
CreateIndexRequest req = CreateIndexRequest.of(b -> b
.index("some-index")
.withJson(input)
);
boolean created = client.indices().create(req).acknowledged();
What alternatives have you considered?
This withJson() method is currently supported on all API objects of the elasticsearch java client, and after moving to opensearch we feel this enhancement is greatly needed in opensearch java client too.
@assafcoh duplicate of https://github.com/opensearch-project/opensearch-java/issues/257?
@owaiskazi19 this issue is a "general" request for supporting creation of all API objects from JSON. The CreateIndexRequest code I mentioned above is just one example for one API.
Specifically, on our project, we are mostly interested in being able to create queries from JSON, something like below:
Reader queryJson = new StringReader(
"{" +
" \"query\": {" +
" \"range\": {" +
" \"@timestamp\": {" +
" \"gt\": \"now-1w\"" +
" }" +
" }" +
" }," +
" \"size\": 100" + ,
" \"aggregations\": {" +
" \"hours\": {" +
" \"date_histogram\": {" +
" \"field\": \"@timestamp\"," +
" \"interval\": \"hour\"" +
" }," +
" \"aggregations\": {" +
" \"max-cpu\": {" +
" \"max\": {" +
" \"field\": \"host.cpu.usage\"" +
" }" +
" }" +
" }" +
" }" +
" }" +
"}");
SearchRequest aggRequest = SearchRequest.of(b -> b
.withJson(queryJson)
);
Map<String, Aggregate> aggs = client
.search(aggRequest, Void.class)
.aggregations();
This sounds like a good idea. Feel free to create a PR.
The functionality proposed here would be quite helpful.
As @assafcoh pointed out, it would be a "general" solution. It would also apply to more special feature requests such as asking for custom JSON input for mapping information (#362) or for documents (#257).
There is something like that in the Elastic Java client. For example the builders of TypeMapping and IndexSettings implement the interface WithJson.
That allows for code such as the following:
InputStream mapping = /* ... */;
InputStream settings = /* ... */;
CreateIndexRequest req =
CreateIndexRequest.of(
cir ->
cir.index(INDEX_NAME)
.mappings(m -> m.withJson(mapping))
.settings(s -> s.withJson(settings)));
As that client is licensed under the Apache 2.0 license, it should be possible to reuse that (or a similar) approach also for the OpenSearch Java client.
As that client is licensed under the Apache 2.0 license, it should be possible to reuse that (or a similar) approach also for the OpenSearch Java client.
Please feel free to contribute! When making PRs we trust you to make sure you're not copying any non-APLv2 code.