-
Notifications
You must be signed in to change notification settings - Fork 49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add higher-level HTTP DSL methods #447
Merged
+1,669
−318
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
bf47cac
Lay foundations
Xtansia b6d9bae
Implement generation
Xtansia 2e81ca2
Run generator
Xtansia a290e44
Update samples
Xtansia 490046b
Update guide
Xtansia 062a686
Add changelog entry
Xtansia 58315a8
PR comments
Xtansia fced4d7
Fix naming tests
Xtansia b9a9ab2
Why is the ordering of these statements load-bearing???
Xtansia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,76 @@ | ||
/* 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; | ||
|
||
namespace Samples.RawJson; | ||
|
||
public class RawJsonHighLevelSample : Sample | ||
{ | ||
public RawJsonHighLevelSample() : base("raw-json-high-level", "A sample demonstrating how to use the high-level client to perform raw JSON requests") { } | ||
|
||
protected override async Task Run(IOpenSearchClient client) | ||
{ | ||
var info = await client.Http.GetAsync<DynamicResponse>("/"); | ||
Debug.Assert(info.Success, info.DebugInformation); | ||
Console.WriteLine($"Welcome to {info.Body.version.distribution} {info.Body.version.number}!"); | ||
|
||
const string indexName = "movies"; | ||
|
||
// Check if the index already exists | ||
|
||
var indexExists = await client.Http.HeadAsync<DynamicResponse>($"/{indexName}"); | ||
Debug.Assert(indexExists.HttpStatusCode == 404, indexExists.DebugInformation); | ||
Console.WriteLine($"Index Exists: {indexExists.HttpStatusCode == 200}"); | ||
|
||
// Create an index | ||
|
||
var indexBody = new { settings = new { index = new { number_of_shards = 4 } } }; | ||
|
||
var createIndex = await client.Http.PutAsync<DynamicResponse>($"/{indexName}", d => d.SerializableBody(indexBody)); | ||
Debug.Assert(createIndex.Success && (bool)createIndex.Body.acknowledged, createIndex.DebugInformation); | ||
Console.WriteLine($"Create Index: {createIndex.Success && (bool)createIndex.Body.acknowledged}"); | ||
|
||
// Add a document to the index | ||
var document = new { title = "Moneyball", director = "Bennett Miller", year = 2011 }; | ||
|
||
const string id = "1"; | ||
|
||
var addDocument = await client.Http.PutAsync<DynamicResponse>( | ||
$"/{indexName}/_doc/{id}", | ||
d => d | ||
.SerializableBody(document) | ||
.QueryString(qs => qs.Add("refresh", true))); | ||
Debug.Assert(addDocument.Success, addDocument.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.Http.PostAsync<DynamicResponse>($"/{indexName}/_search", d => d.SerializableBody(query)); | ||
Debug.Assert(search.Success, search.DebugInformation); | ||
|
||
foreach (var hit in search.Body.hits.hits) Console.WriteLine($"Search Hit: {hit["_source"]["title"]}"); | ||
|
||
// Delete the document | ||
var deleteDocument = await client.Http.DeleteAsync<DynamicResponse>($"/{indexName}/_doc/{id}"); | ||
Debug.Assert(deleteDocument.Success, deleteDocument.DebugInformation); | ||
Console.WriteLine($"Delete Document: {deleteDocument.Success}"); | ||
|
||
// Delete the index | ||
var deleteIndex = await client.Http.DeleteAsync<DynamicResponse>($"/{indexName}"); | ||
Debug.Assert(deleteIndex.Success && (bool)deleteIndex.Body.acknowledged, deleteIndex.DebugInformation); | ||
Console.WriteLine($"Delete Index: {deleteIndex.Success && (bool)deleteIndex.Body.acknowledged}"); | ||
} | ||
} |
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,76 @@ | ||
/* 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 OpenSearch.Net.Specification.HttpApi; | ||
|
||
namespace Samples.RawJson; | ||
|
||
public class RawJsonLowLevelSample : Sample | ||
{ | ||
public RawJsonLowLevelSample() : base("raw-json-low-level", "A sample demonstrating how to use the low-level client to perform raw JSON requests") { } | ||
|
||
protected override async Task Run(IOpenSearchClient client) | ||
{ | ||
var info = await client.LowLevel.Http.GetAsync<DynamicResponse>("/"); | ||
Debug.Assert(info.Success, info.DebugInformation); | ||
Console.WriteLine($"Welcome to {info.Body.version.distribution} {info.Body.version.number}!"); | ||
|
||
const string indexName = "movies"; | ||
|
||
// Check if the index already exists | ||
|
||
var indexExists = await client.LowLevel.Http.HeadAsync<DynamicResponse>($"/{indexName}"); | ||
Debug.Assert(indexExists.HttpStatusCode == 404, indexExists.DebugInformation); | ||
Console.WriteLine($"Index Exists: {indexExists.HttpStatusCode == 200}"); | ||
|
||
// Create an index | ||
|
||
var indexBody = new { settings = new { index = new { number_of_shards = 4 } } }; | ||
|
||
var createIndex = await client.LowLevel.Http.PutAsync<DynamicResponse>($"/{indexName}", PostData.Serializable(indexBody)); | ||
Debug.Assert(createIndex.Success && (bool)createIndex.Body.acknowledged, createIndex.DebugInformation); | ||
Console.WriteLine($"Create Index: {createIndex.Success && (bool)createIndex.Body.acknowledged}"); | ||
|
||
// 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.Http.PutAsync<DynamicResponse>( | ||
$"/{indexName}/_doc/{id}", | ||
PostData.Serializable(document), | ||
new HttpPutRequestParameters{ QueryString = {{"refresh", true}} }); | ||
Debug.Assert(addDocument.Success, addDocument.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.Http.PostAsync<DynamicResponse>($"/{indexName}/_search", PostData.Serializable(query)); | ||
Debug.Assert(search.Success, search.DebugInformation); | ||
|
||
foreach (var hit in search.Body.hits.hits) Console.WriteLine($"Search Hit: {hit["_source"]["title"]}"); | ||
|
||
// Delete the document | ||
var deleteDocument = await client.LowLevel.Http.DeleteAsync<DynamicResponse>($"/{indexName}/_doc/{id}"); | ||
Debug.Assert(deleteDocument.Success, deleteDocument.DebugInformation); | ||
Console.WriteLine($"Delete Document: {deleteDocument.Success}"); | ||
|
||
// Delete the index | ||
var deleteIndex = await client.LowLevel.Http.DeleteAsync<DynamicResponse>($"/{indexName}"); | ||
Debug.Assert(deleteIndex.Success && (bool)deleteIndex.Body.acknowledged, deleteIndex.DebugInformation); | ||
Console.WriteLine($"Delete Index: {deleteIndex.Success && (bool)deleteIndex.Body.acknowledged}"); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for fixing this :)