Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating API objects from JSON data #302

Open
assafcoh opened this issue Dec 25, 2022 · 5 comments · May be fixed by #985
Open

Creating API objects from JSON data #302

assafcoh opened this issue Dec 25, 2022 · 5 comments · May be fixed by #985
Labels
enhancement New feature or request

Comments

@assafcoh
Copy link

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 assafcoh added enhancement New feature or request untriaged labels Dec 25, 2022
@owaiskazi19
Copy link
Member

@assafcoh duplicate of #257?

@assafcoh
Copy link
Author

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

@wbeckler
Copy link

wbeckler commented Jan 4, 2023

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

@wbeckler wbeckler removed the untriaged label Jan 4, 2023
@upwards-gravity
Copy link

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.

@dblock
Copy link
Member

dblock commented Jul 5, 2023

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.

@JM-Lab JM-Lab linked a pull request May 14, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants