opensearch-java icon indicating copy to clipboard operation
opensearch-java copied to clipboard

Creating API objects from JSON data

Open assafcoh opened this issue 2 years ago • 5 comments

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 avatar Dec 25 '22 16:12 assafcoh

@assafcoh duplicate of https://github.com/opensearch-project/opensearch-java/issues/257?

owaiskazi19 avatar Dec 25 '22 18:12 owaiskazi19

@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();

assafcoh avatar Dec 26 '22 12:12 assafcoh

This sounds like a good idea. Feel free to create a PR.

wbeckler avatar Jan 04 '23 16:01 wbeckler

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.

upwards-gravity avatar Jul 03 '23 15:07 upwards-gravity

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.

dblock avatar Jul 05 '23 16:07 dblock