-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
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]> Aligned action examples with Python guide. Signed-off-by: Djcarrillo6 <[email protected]>
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
- [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"]["distribution"].ToString(), versionResponse.Body["version"]["number"].ToString()); // Distribution & Version number | ||
``` | ||
|
||
# PUT | ||
The following example creates a document via `PUT /movies/_doc/{id}` with a request body that specifies the document to create. | ||
|
||
```csharp | ||
var indexBody = new | ||
{ | ||
settings = new | ||
{ | ||
index = new | ||
{ | ||
number_of_shards = 4 | ||
} | ||
} | ||
}; | ||
|
||
var putResponse = await client.LowLevel.DoRequestAsync<DynamicResponse>(HttpMethod.PUT, "/movies", CancellationToken.None, PostData.Serializable(indexBody)); | ||
Console.WriteLine(putResponse.Body["acknowledged"].ToString()); // true | ||
``` | ||
|
||
## POST | ||
The following example creates a document via `POST /movies/_doc` with a request body that specifies the document to create. | ||
|
||
```csharp | ||
string q = "miller"; | ||
|
||
var query = new | ||
{ | ||
size = 5, | ||
query = new | ||
{ | ||
multi_match = new | ||
{ | ||
query = q, | ||
fields = new[] { "title^2", "director" } | ||
} | ||
} | ||
}; | ||
|
||
var postResponse = await client.LowLevel.DoRequestAsync<DynamicResponse>(HttpMethod.POST, "/movies/_search", CancellationToken.None, PostData.Serializable(query)); | ||
Console.WriteLine(postResponse.Body.ToString()); | ||
``` | ||
|
||
# DELETE | ||
The following example deletes a document via `DELETE /movies/_doc/{id}`. | ||
|
||
```csharp | ||
var deleteResponse = await client.LowLevel.DoRequestAsync<DynamicResponse>(HttpMethod.DELETE, "/movies", CancellationToken.None); | ||
Console.WriteLine(deleteResponse.Body["acknowledged"].ToString()); // true | ||
``` | ||
|
||
# Sample Code | ||
[Making Raw JSON Requests](/samples/Samples/Program.cs) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
using OpenSearch.Client; | ||
Check failure on line 8 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch (2.1.0)
Check failure on line 8 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch (2.1.0)
Check failure on line 8 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch (2.2.1)
Check failure on line 8 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch (2.2.1)
Check failure on line 8 in samples/Samples/Program.cs GitHub Actions / compile
Check failure on line 8 in samples/Samples/Program.cs GitHub Actions / compile
Check failure on line 8 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch (2.6.0)
Check failure on line 8 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch (2.6.0)
Check failure on line 8 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch (1.1.0)
Check failure on line 8 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch (1.1.0)
Check failure on line 8 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch (2.5.0)
Check failure on line 8 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch (2.5.0)
Check failure on line 8 in samples/Samples/Program.cs GitHub Actions / Canary
Check failure on line 8 in samples/Samples/Program.cs GitHub Actions / Canary
Check failure on line 8 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch (1.2.4)
Check failure on line 8 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch (1.2.4)
Check failure on line 8 in samples/Samples/Program.cs GitHub Actions / Unit
Check failure on line 8 in samples/Samples/Program.cs GitHub Actions / Unit
Check failure on line 8 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch (2.0.1)
Check failure on line 8 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch (2.0.1)
Check failure on line 8 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch (2.7.0)
Check failure on line 8 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch (2.7.0)
Check failure on line 8 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch (1.3.11)
Check failure on line 8 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch (1.3.11)
Check failure on line 8 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch Unreleased (1.x)
Check failure on line 8 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch Unreleased (1.x)
Check failure on line 8 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch (2.4.1)
Check failure on line 8 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch (2.4.1)
Check failure on line 8 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch (2.9.0)
Check failure on line 8 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch (2.9.0)
Check failure on line 8 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch (2.3.0)
Check failure on line 8 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch (2.3.0)
Check failure on line 8 in samples/Samples/Program.cs GitHub Actions / Create Release Artifacts
Check failure on line 8 in samples/Samples/Program.cs GitHub Actions / Create Release Artifacts
Check failure on line 8 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch (2.8.0)
Check failure on line 8 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch (2.8.0)
Check failure on line 8 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch Unreleased (main)
|
||
using OpenSearch.Net; | ||
Check failure on line 9 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch (2.1.0)
Check failure on line 9 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch (2.1.0)
Check failure on line 9 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch (2.2.1)
Check failure on line 9 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch (2.2.1)
Check failure on line 9 in samples/Samples/Program.cs GitHub Actions / compile
Check failure on line 9 in samples/Samples/Program.cs GitHub Actions / compile
Check failure on line 9 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch (2.6.0)
Check failure on line 9 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch (2.6.0)
Check failure on line 9 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch (1.1.0)
Check failure on line 9 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch (1.1.0)
Check failure on line 9 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch (2.5.0)
Check failure on line 9 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch (2.5.0)
Check failure on line 9 in samples/Samples/Program.cs GitHub Actions / Canary
Check failure on line 9 in samples/Samples/Program.cs GitHub Actions / Canary
Check failure on line 9 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch (1.2.4)
Check failure on line 9 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch (1.2.4)
Check failure on line 9 in samples/Samples/Program.cs GitHub Actions / Unit
Check failure on line 9 in samples/Samples/Program.cs GitHub Actions / Unit
Check failure on line 9 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch (2.0.1)
Check failure on line 9 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch (2.0.1)
Check failure on line 9 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch (2.7.0)
Check failure on line 9 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch (2.7.0)
Check failure on line 9 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch (1.3.11)
Check failure on line 9 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch (1.3.11)
Check failure on line 9 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch Unreleased (1.x)
Check failure on line 9 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch Unreleased (1.x)
Check failure on line 9 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch (2.4.1)
Check failure on line 9 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch (2.4.1)
Check failure on line 9 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch (2.9.0)
Check failure on line 9 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch (2.9.0)
Check failure on line 9 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch (2.3.0)
Check failure on line 9 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch (2.3.0)
Check failure on line 9 in samples/Samples/Program.cs GitHub Actions / Create Release Artifacts
Check failure on line 9 in samples/Samples/Program.cs GitHub Actions / Create Release Artifacts
Check failure on line 9 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch (2.8.0)
Check failure on line 9 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch (2.8.0)
Check failure on line 9 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch Unreleased (main)
|
||
using HttpMethod = OpenSearch.Net.HttpMethod; | ||
Check failure on line 10 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch (2.1.0)
Check failure on line 10 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch (2.1.0)
Check failure on line 10 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch (2.2.1)
Check failure on line 10 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch (2.2.1)
Check failure on line 10 in samples/Samples/Program.cs GitHub Actions / compile
Check failure on line 10 in samples/Samples/Program.cs GitHub Actions / compile
Check failure on line 10 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch (2.6.0)
Check failure on line 10 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch (2.6.0)
Check failure on line 10 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch (1.1.0)
Check failure on line 10 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch (1.1.0)
Check failure on line 10 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch (2.5.0)
Check failure on line 10 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch (2.5.0)
Check failure on line 10 in samples/Samples/Program.cs GitHub Actions / Canary
Check failure on line 10 in samples/Samples/Program.cs GitHub Actions / Canary
Check failure on line 10 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch (1.2.4)
Check failure on line 10 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch (1.2.4)
Check failure on line 10 in samples/Samples/Program.cs GitHub Actions / Unit
Check failure on line 10 in samples/Samples/Program.cs GitHub Actions / Unit
Check failure on line 10 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch (2.0.1)
Check failure on line 10 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch (2.0.1)
Check failure on line 10 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch (2.7.0)
Check failure on line 10 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch (2.7.0)
Check failure on line 10 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch (1.3.11)
Check failure on line 10 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch (1.3.11)
Check failure on line 10 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch Unreleased (1.x)
Check failure on line 10 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch Unreleased (1.x)
Check failure on line 10 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch (2.4.1)
Check failure on line 10 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch (2.4.1)
Check failure on line 10 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch (2.9.0)
Check failure on line 10 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch (2.9.0)
Check failure on line 10 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch (2.3.0)
Check failure on line 10 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch (2.3.0)
Check failure on line 10 in samples/Samples/Program.cs GitHub Actions / Create Release Artifacts
Check failure on line 10 in samples/Samples/Program.cs GitHub Actions / Create Release Artifacts
Check failure on line 10 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch (2.8.0)
Check failure on line 10 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch (2.8.0)
Check failure on line 10 in samples/Samples/Program.cs GitHub Actions / Integration OpenSearch Unreleased (main)
|
||
|
||
|
||
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"]["distribution"].ToString() + " " + versionResponse.Body["version"]["number"].ToString()); | ||
|
||
// PUT | ||
var indexBody = new | ||
{ | ||
settings = new | ||
{ | ||
index = new | ||
{ | ||
number_of_shards = 4 | ||
} | ||
} | ||
}; | ||
|
||
var putResponse = await client.LowLevel.DoRequestAsync<DynamicResponse>(HttpMethod.PUT, "/movies", CancellationToken.None, PostData.Serializable(indexBody)); | ||
|
||
// POST | ||
string q = "miller"; | ||
|
||
var query = new | ||
{ | ||
size = 5, | ||
query = new | ||
{ | ||
multi_match = new | ||
{ | ||
query = q, | ||
fields = new[] { "title^2", "director" } | ||
} | ||
} | ||
}; | ||
|
||
var postResponse = await client.LowLevel.DoRequestAsync<DynamicResponse>(HttpMethod.POST, "/movies/_search", CancellationToken.None, PostData.Serializable(query)); | ||
|
||
// DELETE | ||
var deleteResponse = await client.LowLevel.DoRequestAsync<DynamicResponse>(HttpMethod.DELETE, "/movies", CancellationToken.None); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<IsPackable>False</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="OpenSearch.Client" Version="1.*" /> | ||
</ItemGroup> | ||
|
||
</Project> |