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

[FEATURE] Make raw JSON REST requests to OpenSearch #193

Closed
Tracked by #62
dblock opened this issue Oct 16, 2023 · 8 comments
Closed
Tracked by #62

[FEATURE] Make raw JSON REST requests to OpenSearch #193

dblock opened this issue Oct 16, 2023 · 8 comments
Assignees
Labels
enhancement New feature or request

Comments

@dblock
Copy link
Member

dblock commented Oct 16, 2023

Is your feature request related to a problem?

Coming from opensearch-project/opensearch-clients#62, add support for making raw JSON requests.

What solution would you like?

Add a high level DSL, e.g. client.get and client.post.

@samuelorji
Copy link
Contributor

@Xtansia , I'll like to pick this up. How do you suppose I go about this in a way that aligns with the long term vision of the project

@Xtansia
Copy link
Collaborator

Xtansia commented Oct 16, 2023

@samuelorji I would say a first step is to investigate and document what's already possible in this style in the client, for example we currently accept arbitrary JSON for all the defined endpoints, so maybe try to understand how one of the existing operations works. Then you can share your findings here in the issue, and determine an appropriate design that is similar to the existing ones, but allows setting an arbitrary path and http method etc. It should still use a similar "transport" call that the existing ones use like https://github.com/opensearch-project/opensearch-rs/blob/94e7b724b957259f4268151fe2d08dd5d2a3eb25/opensearch/src/root/mod.rs#L4385C5-L4438C6

@russcam
Copy link

russcam commented Oct 19, 2023

It's already possible to do this with the rust client with

use opensearch::OpenSearch;
use opensearch::http::headers::HeaderMap;
use opensearch::http::Method;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = OpenSearch::default();
    let body = b"{\"query\":{\"match_all\":{}}}";
    let response = client
        .send(
            Method::Post,
            "/_some_api_that_may_exist",
            HeaderMap::new(),
            None,
            Some(body.as_ref()),
            None
        )
        .await?;

    let string_response = response.text().await?;

    Ok(())
}

This can send a raw request to any endpoint, and you can use the *Parts enums to build a valid path to a known endpoint e.g. for search to a specific index

use opensearch::{OpenSearch, SearchParts};
use opensearch::http::headers::HeaderMap;
use opensearch::http::Method;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = OpenSearch::default();
    let body = b"{\"query\":{\"match_all\":{}}}";
    let response = client
        .send(
            Method::Post,
            SearchParts::Index(&["tweets"]).url().as_ref(),
            HeaderMap::new(),
            None,
            Some(body.as_ref()),
            None,
        )
        .await?;
    
    Ok(())
}

If you're wanting to support raw JSON strings for each concrete API builder that accepts JSON, the api_generator can be updated to introduce a new function, similar to body(...), that accepts a string slice and returns some API builder such as Search<'a, 'b, B>, where B implements Body trait (which String and &str do); the body(...) function accepts T where T implements serde's Serialize and returns Search<'a, 'b, JsonBody<T>>, so is intentionally constrained, but the underlying types support any Body.

@dblock
Copy link
Member Author

dblock commented Oct 23, 2023

The first example is what I'm after (no specialized classes like SearchParts), so documenting that would be a great start!

@samuelorji
Copy link
Contributor

Will add this to the user guide

@dblock
Copy link
Member Author

dblock commented Oct 30, 2023

Closing via #196.

@dblock dblock closed this as completed Oct 30, 2023
@Xtansia
Copy link
Collaborator

Xtansia commented Nov 2, 2023

@dblock Shouldn't this stay open as we want a cleaner high-level wrapper as well?

@dblock
Copy link
Member Author

dblock commented Nov 3, 2023

I've been going back and forth. I think for this client it should be closed, client.send is perfectly fine for an interface, not sure we'd benefit much from a client.post or client.get, but reopen if you feel otherwise!

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

No branches or pull requests

4 participants