-
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. (#…
…406) * Added guide for using raw JSON REST requests with low-level client. Signed-off-by: Djcarrillo6 <djcarrillo6@yahoo.com> Added sample for using raw JSON REST requests with low-level client. Signed-off-by: Djcarrillo6 <djcarrillo6@yahoo.com> Added sample for using raw JSON REST requests with low-level client #2 Signed-off-by: Djcarrillo6 <djcarrillo6@yahoo.com> Created new samples project & PR review changes. Signed-off-by: Djcarrillo6 <djcarrillo6@yahoo.com> Aligned action examples with Python guide. Signed-off-by: Djcarrillo6 <djcarrillo6@yahoo.com> PR change requests. Signed-off-by: Djcarrillo6 <djcarrillo6@yahoo.com> Changed to raw JSON in sample code. Signed-off-by: Djcarrillo6 <djcarrillo6@yahoo.com> Added new section to the guide for using PostData.Serializable() Signed-off-by: Djcarrillo6 <djcarrillo6@yahoo.com> * Expand description of PostData types Signed-off-by: Thomas Farr <tsfarr@amazon.com> * Complete working sample Signed-off-by: Thomas Farr <tsfarr@amazon.com> --------- Signed-off-by: Djcarrillo6 <djcarrillo6@yahoo.com> Signed-off-by: Thomas Farr <tsfarr@amazon.com> Co-authored-by: Thomas Farr <tsfarr@amazon.com>
- Loading branch information
1 parent
35a0fd8
commit 1924be9
Showing
5 changed files
with
233 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,131 @@ | ||
- [Making Raw JSON REST Requests](#making-raw-json-rest-requests) | ||
- [HTTP Methods](#http-methods) | ||
- [GET](#get) | ||
- [PUT](#put) | ||
- [POST](#post) | ||
- [DELETE](#delete) | ||
- [Using Different Types Of PostData](#using-different-types-of-postdata) | ||
- [PostData.String](#postdatastring) | ||
- [PostData.Bytes](#postdatabytes) | ||
- [PostData.Serializable](#postdataserializable) | ||
- [PostData.MultiJson](#postdatamultijson) | ||
|
||
# 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. | ||
|
||
The OpenSearch client implements many high-level REST DSLs that invoke OpenSearch APIs. However you may find yourself in a situation that requires you to invoke an API that is not supported by the client. You can use `client.LowLevel.DoRequest` to do so. See [samples/Samples/RawJsonSample/Program.cs](../samples/Samples/RawJsonSample/Program.cs) for a complete working sample. | ||
|
||
## HTTP Methods | ||
|
||
### GET | ||
The following example returns the server version information via `GET /`. | ||
|
||
```csharp | ||
var info = await client.LowLevel.DoRequestAsync<DynamicResponse>(HttpMethod.GET, "/", CancellationToken.None); | ||
Console.WriteLine($"Welcome to {info.Body.version.distribution} {info.Body.version.number}!"); | ||
``` | ||
|
||
### PUT | ||
The following example creates an index. | ||
|
||
```csharp | ||
var indexBody = new { settings = new { index = new { number_of_shards = 4 } } }; | ||
|
||
var createIndex = await client.LowLevel.DoRequestAsync<DynamicResponse>(HttpMethod.PUT, "/movies", CancellationToken.None, PostData.Serializable(indexBody)); | ||
Debug.Assert(createIndex.Success && (bool)createIndex.Body.acknowledged, createIndex.DebugInformation); | ||
``` | ||
|
||
### POST | ||
The following example searches for a document. | ||
|
||
```csharp | ||
const string q = "miller"; | ||
|
||
var query = new | ||
{ | ||
size = 5, | ||
query = new { multi_match = new { query = q, fields = new[] { "title^2", "director" } } } | ||
}; | ||
|
||
var search = await client.LowLevel.DoRequestAsync<DynamicResponse>(HttpMethod.POST, $"/{indexName}/_search", CancellationToken.None, PostData.Serializable(query)); | ||
Debug.Assert(search.Success, search.DebugInformation); | ||
|
||
foreach (var hit in search.Body.hits.hits) Console.WriteLine(hit["_source"]["title"]); | ||
``` | ||
|
||
### DELETE | ||
The following example deletes an index. | ||
|
||
```csharp | ||
var deleteDocument = await client.LowLevel.DoRequestAsync<DynamicResponse>(HttpMethod.DELETE, $"/{indexName}/_doc/{id}", CancellationToken.None); | ||
Debug.Assert(deleteDocument.Success, deleteDocument.DebugInformation); | ||
``` | ||
|
||
## Using Different Types Of PostData | ||
The OpenSearch .NET client provides a `PostData` class that is used to provide the request body for a request. The `PostData` class has several static methods that can be used to create a `PostData` object from different types of data. | ||
|
||
### PostData.String | ||
The following example shows how to use the `PostData.String` method to create a `PostData` object from a string. | ||
|
||
```csharp | ||
string indexBody = @" | ||
{{ | ||
""settings"": { | ||
""index"": { | ||
""number_of_shards"": 4 | ||
} | ||
} | ||
}}"; | ||
|
||
await client.LowLevel.DoRequestAsync<DynamicResponse>(HttpMethod.PUT, "/movies", CancellationToken.None, PostData.String(indexBody)); | ||
``` | ||
|
||
### PostData.Bytes | ||
The following example shows how to use the `PostData.Bytes` method to create a `PostData` object from a byte array. | ||
|
||
```csharp | ||
byte[] indexBody = Encoding.UTF8.GetBytes(@" | ||
{{ | ||
""settings"": { | ||
""index"": { | ||
""number_of_shards"": 4 | ||
} | ||
} | ||
}}"); | ||
|
||
await client.LowLevel.DoRequestAsync<DynamicResponse>(HttpMethod.PUT, "/movies", CancellationToken.None, PostData.Bytes(indexBody)); | ||
``` | ||
|
||
### PostData.Serializable | ||
The following example shows how to use the `PostData.Serializable` method to create a `PostData` object from a serializable object. | ||
|
||
```csharp | ||
var indexBody = new | ||
{ | ||
settings = new | ||
{ | ||
index = new | ||
{ | ||
number_of_shards = 4 | ||
} | ||
} | ||
}; | ||
|
||
await client.LowLevel.DoRequestAsync<DynamicResponse>(HttpMethod.PUT, "/movies", CancellationToken.None, PostData.Serializable(indexBody)); | ||
``` | ||
|
||
### PostData.MultiJson | ||
The following example shows how to use the `PostData.MultiJson` method to create a `PostData` object from a collection of serializable objects. | ||
The `PostData.MultiJson` method is useful when you want to send multiple documents in a bulk request. | ||
|
||
```csharp | ||
var bulkBody = new object[] | ||
{ | ||
new { index = new { _index = "movies", _id = "1" } }, | ||
new { title = "The Godfather", director = "Francis Ford Coppola", year = 1972 }, | ||
new { index = new { _index = "movies", _id = "2" } }, | ||
new { title = "The Godfather: Part II", director = "Francis Ford Coppola", year = 1974 } | ||
}; | ||
|
||
await client.LowLevel.DoRequestAsync<DynamicResponse>(HttpMethod.POST, "/_bulk", CancellationToken.None, PostData.MultiJson(bulkBody)); | ||
``` |
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,71 @@ | ||
/* 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 System.Diagnostics; | ||
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("http://localhost:9200"); | ||
var config = new ConnectionSettings(node) | ||
.ServerCertificateValidationCallback(CertificateValidations.AllowAll) | ||
.BasicAuthentication("admin", "admin") | ||
.DisableDirectStreaming(); | ||
|
||
var client = new OpenSearchClient(config); | ||
|
||
var info = await client.LowLevel.DoRequestAsync<DynamicResponse>(HttpMethod.GET, "/", CancellationToken.None); | ||
Console.WriteLine($"Welcome to {info.Body.version.distribution} {info.Body.version.number}!"); | ||
|
||
// Create an index | ||
|
||
const string indexName = "movies"; | ||
|
||
var indexBody = new { settings = new { index = new { number_of_shards = 4 } } }; | ||
|
||
var createIndex = await client.LowLevel.DoRequestAsync<DynamicResponse>(HttpMethod.PUT, $"/{indexName}", CancellationToken.None, PostData.Serializable(indexBody)); | ||
Debug.Assert(createIndex.Success && (bool)createIndex.Body.acknowledged, createIndex.DebugInformation); | ||
|
||
// Add a document to the index | ||
var document = new { title = "Moneyball", director = "Bennett Miller", year = 2011}; | ||
|
||
const string id = "1"; | ||
|
||
var addDocument = await client.LowLevel.DoRequestAsync<DynamicResponse>(HttpMethod.PUT, $"/{indexName}/_doc/{id}", CancellationToken.None, PostData.Serializable(document)); | ||
Debug.Assert(addDocument.Success, addDocument.DebugInformation); | ||
|
||
// Refresh the index | ||
var refresh = await client.LowLevel.DoRequestAsync<DynamicResponse>(HttpMethod.POST, $"/{indexName}/_refresh", CancellationToken.None); | ||
Debug.Assert(refresh.Success, refresh.DebugInformation); | ||
|
||
// Search for a document | ||
const string q = "miller"; | ||
|
||
var query = new | ||
{ | ||
size = 5, | ||
query = new { multi_match = new { query = q, fields = new[] { "title^2", "director" } } } | ||
}; | ||
|
||
var search = await client.LowLevel.DoRequestAsync<DynamicResponse>(HttpMethod.POST, $"/{indexName}/_search", CancellationToken.None, PostData.Serializable(query)); | ||
Debug.Assert(search.Success, search.DebugInformation); | ||
|
||
foreach (var hit in search.Body.hits.hits) Console.WriteLine(hit["_source"]["title"]); | ||
|
||
// Delete the document | ||
var deleteDocument = await client.LowLevel.DoRequestAsync<DynamicResponse>(HttpMethod.DELETE, $"/{indexName}/_doc/{id}", CancellationToken.None); | ||
Debug.Assert(deleteDocument.Success, deleteDocument.DebugInformation); | ||
|
||
// Delete the index | ||
var deleteIndex = await client.LowLevel.DoRequestAsync<DynamicResponse>(HttpMethod.DELETE, $"/{indexName}", CancellationToken.None); | ||
Debug.Assert(deleteIndex.Success && (bool)deleteIndex.Body.acknowledged, deleteIndex.DebugInformation); | ||
} | ||
} |
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,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="$(SolutionRoot)\src\OpenSearch.Client\OpenSearch.Client.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |