Skip to content

Latest commit

 

History

History
152 lines (116 loc) · 5.31 KB

json.md

File metadata and controls

152 lines (116 loc) · 5.31 KB

Making Raw JSON REST Requests

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 the methods defined within client.Http to do so. See samples/Samples/RawJson/RawJsonHighLevelSample.cs and samples/Samples/RawJson/RawJsonLowLevelSample.cs for complete working samples.

Older versions of the client that do not support the client.Http namespace can use the client.LowLevel.DoRequest method instead.

HTTP Methods

GET

The following example returns the server version information via GET /.

var info = await client.Http.GetAsync<DynamicResponse>("/");

Console.WriteLine($"Welcome to {info.Body.version.distribution} {info.Body.version.number}!");

HEAD

The following example checks if an index exists via HEAD /movies.

var indexExists = await client.Http.HeadAsync<VoidResponse>("/movies");

Console.WriteLine($"Index Exists: {indexExists.HttpStatusCode == 200}");

PUT

The following example creates an index.

var indexBody = new { settings = new { index = new { number_of_shards = 4 } } };

var createIndex = await client.Http.PutAsync<DynamicResponse>("/movies", d => d.SerializableBody(indexBody));

Console.WriteLine($"Create Index: {createIndex.Success && (bool)createIndex.Body.acknowledged}");

POST

The following example searches 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.Http.PostAsync<DynamicResponse>("/movies/_search", d => d.SerializableBody(query));

foreach (var hit in search.Body.hits.hits) Console.WriteLine($"Search Hit: {hit["_source"]["title"]}");

DELETE

The following example deletes an index.

var deleteIndex = await client.Http.DeleteAsync<DynamicResponse>("/movies");

Console.WriteLine($"Delete Index: {deleteIndex.Success && (bool)deleteIndex.Body.acknowledged}");

Request Bodies

For the methods that take a request body (PUT/POST/PATCH) it is possible use several different types to specify the body. The high-level methods provided in OpenSearch.Client provide overloaded fluent-methods for setting the body. While the lower level methods in OpenSearch.Net accept instances of PostData.
The PostData class has several static methods that can be used to create a PostData object from different types of data.

String

The following example shows how to pass a string as a request body:

string indexBody = @"
{{
    ""settings"": {
        ""index"": {
            ""number_of_shards"": 4
        }
    }
}}";

await client.Http.PutAsync<DynamicResponse>("/movies", d => d.Body(indexBody));

Bytes

The following example shows how to pass a byte array as a request body:

byte[] indexBody = Encoding.UTF8.GetBytes(@"
{{
    ""settings"": {
        ""index"": {
            ""number_of_shards"": 4
        }
    }
}}");

await client.Http.PutAsync<DynamicResponse>("/movies", d => d.Body(indexBody));

Serializable

The following example shows how to pass an object that will be serialized to JSON as a request body:

var indexBody = new
{
    settings = new
    {
        index = new
        {
            number_of_shards = 4
        }
    }
};

await client.Http.PutAsync<DynamicResponse>("/movies", d => d.SerializableBody(indexBody));

Multi JSON

The following example shows how to pass a collection of objects (or strings) that will be serialized to JSON and then newline-delimited as a request body. This formatting is primarily used when you want to make a bulk request.

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.Http.PostAsync<DynamicResponse>("/_bulk", d => d.MultiJsonBody(indexBody));

Response Bodies

There are a handful of response type implementations that can be used to retrieve the response body. These are specified as the generic argument to the request methods. The content of the body will then be available via the Body property of the response object. The following response types are available:

  • VoidResponse: The response body will not be read. Useful when you only care about the response status code, such as a HEAD request to check an index exists.
  • StringResponse: The response body will be read into a string.
  • BytesResponse: The response body will be read into a byte array.
  • DynamicResponse: The response body will be deserialized as JSON into a dynamic object.