-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added guide for using raw JSON REST requests with low-level client.
Signed-off-by: Djcarrillo6 <[email protected]> Added sample for using raw JSON REST requests with low-level client. Signed-off-by: Djcarrillo6 <[email protected]> Added sample for using raw JSON REST requests with low-level client #2 Signed-off-by: Djcarrillo6 <[email protected]> Created new samples project & PR review changes. Signed-off-by: Djcarrillo6 <[email protected]>
- Loading branch information
1 parent
b014ff4
commit cc3e190
Showing
5 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<DynamicResponse>(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<DynamicResponse>(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<DynamicResponse>(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<DynamicResponse>(HttpMethod.DELETE, "/movies/_doc/1", CancellationToken.None); | ||
Console.WriteLine(deleteResponse.Body["result"].ToString()); // deleted | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<DynamicResponse>(HttpMethod.GET, "/", CancellationToken.None); | ||
Console.WriteLine(versionResponse.Body["version"]["number"].ToString()); // Version number | ||
|
||
// Post | ||
var postResponse = await client.LowLevel.DoRequestAsync<DynamicResponse>(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<DynamicResponse>(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<DynamicResponse>(HttpMethod.DELETE, "/movies/_doc/1", CancellationToken.None); | ||
Console.WriteLine(deleteResponse.Body["result"].ToString()); // deleted | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="OpenSearch.Client" Version="1.*" /> | ||
</ItemGroup> | ||
|
||
</Project> |