From cc3e190f205ba121f78b99e94eae16bdc9be9177 Mon Sep 17 00:00:00 2001 From: Djcarrillo6 Date: Sun, 22 Oct 2023 14:56:19 -0700 Subject: [PATCH] Added guide for using raw JSON REST requests with low-level client. Signed-off-by: Djcarrillo6 Added sample for using raw JSON REST requests with low-level client. Signed-off-by: Djcarrillo6 Added sample for using raw JSON REST requests with low-level client #2 Signed-off-by: Djcarrillo6 Created new samples project & PR review changes. Signed-off-by: Djcarrillo6 --- OpenSearch.sln | 9 ++++++ USER_GUIDE.md | 6 ++++ guides/json.md | 52 ++++++++++++++++++++++++++++++++++ samples/Samples/Program.cs | 49 ++++++++++++++++++++++++++++++++ samples/Samples/Samples.csproj | 14 +++++++++ 5 files changed, 130 insertions(+) create mode 100644 guides/json.md create mode 100644 samples/Samples/Program.cs create mode 100644 samples/Samples/Samples.csproj diff --git a/OpenSearch.sln b/OpenSearch.sln index 8fa85ddfc4..de0aba5da9 100644 --- a/OpenSearch.sln +++ b/OpenSearch.sln @@ -98,6 +98,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenSearch.Stack.ArtifactsA EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{22DF419F-9A90-4317-957D-E239EB3F95DF}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{E89FE975-FA94-405F-B748-BF2EC8AFFEEE}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Samples", "samples\Samples\Samples.csproj", "{0D084660-06BF-4F3A-A041-DAAB4837378F}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -137,6 +141,7 @@ Global {22DF419F-9A90-4317-957D-E239EB3F95DF} = {87ABA679-F3F4-48CE-82B3-1AAE5D0A5935} {C80D225C-F072-4B24-9ACE-82EFD9362237} = {22DF419F-9A90-4317-957D-E239EB3F95DF} {1F5A7B1A-2566-481F-91B5-A63D7F939973} = {22DF419F-9A90-4317-957D-E239EB3F95DF} + {0D084660-06BF-4F3A-A041-DAAB4837378F} = {E89FE975-FA94-405F-B748-BF2EC8AFFEEE} EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution {5B393962-7586-49BA-BD99-3B1E35F48E94}.Debug|Any CPU.ActiveCfg = Debug|Any CPU @@ -231,5 +236,9 @@ Global {E7C0BDC2-28AD-4582-8FEA-0F6327A42C0E}.Debug|Any CPU.Build.0 = Debug|Any CPU {E7C0BDC2-28AD-4582-8FEA-0F6327A42C0E}.Release|Any CPU.ActiveCfg = Release|Any CPU {E7C0BDC2-28AD-4582-8FEA-0F6327A42C0E}.Release|Any CPU.Build.0 = Release|Any CPU + {0D084660-06BF-4F3A-A041-DAAB4837378F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {0D084660-06BF-4F3A-A041-DAAB4837378F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {0D084660-06BF-4F3A-A041-DAAB4837378F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {0D084660-06BF-4F3A-A041-DAAB4837378F}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection EndGlobal diff --git a/USER_GUIDE.md b/USER_GUIDE.md index 24ebaddce9..f97263fe7e 100644 --- a/USER_GUIDE.md +++ b/USER_GUIDE.md @@ -303,3 +303,9 @@ var client = new OpenSearchLowLevelClient(config); ``` Note the main difference here is that we are instantiating an `OpenSearchLowLevelClient` rather than `OpenSearchClient`, and `ConnectionConfiguration` instead of `ConnectionSettings`. + + +## Advanced Features + +- [Making Raw JSON Requests](guides/json.md) + - [Sample Code](/samples/Samples/Program.cs) \ No newline at end of file diff --git a/guides/json.md b/guides/json.md new file mode 100644 index 0000000000..2854200007 --- /dev/null +++ b/guides/json.md @@ -0,0 +1,52 @@ +- [Making Raw JSON REST Requests](#making-raw-json-rest-requests) + - [GET](#get) + - [PUT](#put) + - [POST](#post) + - [DELETE](#delete) + +# Making Raw JSON REST Requests +OpenSearch exposes a REST API that you can use to interact with OpenSearch. The OpenSearch .NET client provides a low-level API that allows you to send raw JSON requests to OpenSearch. This API is useful if you want to use a feature that is not yet supported by the OpenSearch .NET client, but it supported by the OpenSearch REST API. + +## GET +The following example returns the server version information via `GET /`. + +```csharp +var versionResponse = await client.LowLevel.DoRequestAsync(HttpMethod.GET, "/", CancellationToken.None); +Console.WriteLine(versionResponse.Body["version"]["number"].ToString()); // Version number +``` + +## POST +The following example creates a document via `POST /movies/_doc` with a request body that specifies the document to create. + +```csharp +var postResponse = await client.LowLevel.DoRequestAsync(HttpMethod.POST, "/movies/_doc", CancellationToken.None, PostData.Serializable( + new + { + title = "The Dark Knight 6", + director = "Christopher Nolan", + year = 2012 + })); +Console.WriteLine(postResponse.Body["result"].ToString()); // created +``` + +# PUT +The following example creates a document via `PUT /movies/_doc/{id}` with a request body that specifies the document to create. + +```csharp +var putResponse = await client.LowLevel.DoRequestAsync(HttpMethod.PUT, "/movies/_doc/1", CancellationToken.None, PostData.Serializable( + new + { + title = "The Dark Knight 6", + director = "Christopher Nolan", + year = 2023 + })); +Console.WriteLine(putResponse.Body["result"].ToString()); // created +``` + +# DELETE +The following example deletes a document via `DELETE /movies/_doc/{id}`. + +```csharp +var deleteResponse = await client.LowLevel.DoRequestAsync(HttpMethod.DELETE, "/movies/_doc/1", CancellationToken.None); +Console.WriteLine(deleteResponse.Body["result"].ToString()); // deleted +``` \ No newline at end of file diff --git a/samples/Samples/Program.cs b/samples/Samples/Program.cs new file mode 100644 index 0000000000..313cc6177b --- /dev/null +++ b/samples/Samples/Program.cs @@ -0,0 +1,49 @@ +using OpenSearch.Client; +using OpenSearch.Net; +using HttpMethod = OpenSearch.Net.HttpMethod; + + +public class Program +{ + public static async Task Main(string[] args) + { + var node = new Uri("https://localhost:9200"); + var config = new ConnectionSettings(node) + .ServerCertificateValidationCallback(CertificateValidations.AllowAll) + .BasicAuthentication("admin", "admin") + .DisableDirectStreaming(); + + var client = new OpenSearchClient(config); + + + // Sample Code: Making Raw JSON Requests + + // GET + var versionResponse = await client.LowLevel.DoRequestAsync(HttpMethod.GET, "/", CancellationToken.None); + Console.WriteLine(versionResponse.Body["version"]["number"].ToString()); // Version number + + // Post + var postResponse = await client.LowLevel.DoRequestAsync(HttpMethod.POST, "/movies/_doc", CancellationToken.None, PostData.Serializable( + new + { + title = "The Dark Knight", + director = "Christopher Nolan", + year = 2012 + })); + Console.WriteLine(postResponse.Body["result"].ToString()); // created + + // PUT + var putResponse = await client.LowLevel.DoRequestAsync(HttpMethod.PUT, "/movies/_doc/1", CancellationToken.None, PostData.Serializable( + new + { + title = "The Dark Knight 2", + director = "Christopher Nolan", + year = 2012 + })); + Console.WriteLine(putResponse.Body["result"].ToString()); // created + + // DELETE + var deleteResponse = await client.LowLevel.DoRequestAsync(HttpMethod.DELETE, "/movies/_doc/1", CancellationToken.None); + Console.WriteLine(deleteResponse.Body["result"].ToString()); // deleted + } +} \ No newline at end of file diff --git a/samples/Samples/Samples.csproj b/samples/Samples/Samples.csproj new file mode 100644 index 0000000000..9f6abbee9d --- /dev/null +++ b/samples/Samples/Samples.csproj @@ -0,0 +1,14 @@ + + + + Exe + net6.0 + enable + enable + + + + + + +