Connect to OpenSearch with C#
1. Install NEST package
Go to a package manager on visual studio and install NEST
Or you can use the library with NuGet like so:
1 |
Install-Package NEST |
2. Connect to Cluster
1 2 3 4 5 6 7 8 9 10 11 12 |
var uris = new Uri[] { new Uri("xxx.xxx.xxx.xxx:9200"), new Uri("xxx.xxx.xxx.xxx:9200"), new Uri("xxx.xxx.xxx.xxx:9200") }; var connectionPool = new SniffingConnectionPool(uris); var settings = new ConnectionSettings(connectionPool) .BasicAuthentication("icopensearch", "<Password>") .DefaultMappingFor<Testing>(i => i.IndexName("testing")); |
3. Indexing
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
var client = new ElasticClient(settings); var document = new Testing("this is a test", DateTime.Now); var indexResponse = client.Index(new IndexRequest<Testing>(document, IndexName.From<Testing>(), 1)); if (!indexResponse.IsValid) { Console.Write("Failed to index document. "); if (indexResponse.ServerError != null) { Console.WriteLine(indexResponse.ServerError); } else if (indexResponse.OriginalException != null) { Console.WriteLine(indexResponse.OriginalException); } else { Console.WriteLine("Error code: " + indexResponse.ApiCall.HttpStatusCode); } } else { Console.WriteLine($"Indexed document to index \"testing\" with id 1"); } |
4. Getting a document
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
var getResponse = client.Get<Testing>(new GetRequest<Testing>(1)); if (getResponse.OriginalException != null) { throw getResponse.OriginalException; } if (!getResponse.IsValid) { Console.Write("Failed to retrieve document. "); if (getResponse.ServerError != null) { Console.WriteLine(getResponse.ServerError); } else if (getResponse.OriginalException != null) { Console.WriteLine(getResponse.OriginalException); } else { Console.WriteLine("Error code: " + getResponse.ApiCall.HttpStatusCode); } } else { Console.WriteLine($"Retrieved document: " + $"{{Id: {getResponse.Id}, " + $"Description: {getResponse.Source.Description}, " + $"Timestamp: {getResponse.Source.Timestamp}}}"); } |
Full sample code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
using System; using Elasticsearch.Net; using Nest; namespace elasticsearch_client_example { public class Testing { public string Description; public DateTime Timestamp; public Testing(string Description, DateTime Timestamp) { this.Description = Description; this.Timestamp = Timestamp; } } public class Client { static void Main(string[] args) { var uris = new Uri[] { new Uri("xxx.xxx.xxx.xxx:9200"), new Uri("xxx.xxx.xxx.xxx:9200"), new Uri("xxx.xxx.xxx.xxx:9200") }; var connectionPool = new SniffingConnectionPool(uris); var settings = new ConnectionSettings(connectionPool) .BasicAuthentication("icopensearch", "<Password>") .DefaultMappingFor<Testing>(i => i.IndexName("testing")); var client = new ElasticClient(settings); var document = new Testing("this is a test", DateTime.Now); var indexResponse = client.Index(new IndexRequest<Testing>(document, IndexName.From<Testing>(), 1)); if (!indexResponse.IsValid) { Console.Write("Failed to index document. "); if (indexResponse.ServerError != null) { Console.WriteLine(indexResponse.ServerError); } else if (indexResponse.OriginalException != null) { Console.WriteLine(indexResponse.OriginalException); } else { Console.WriteLine("Error code: " + indexResponse.ApiCall.HttpStatusCode); } } else { Console.WriteLine($"Indexed document to index \"testing\" with id 1"); } var getResponse = client.Get<Testing>(new GetRequest<Testing>(1)); if (getResponse.OriginalException != null) { throw getResponse.OriginalException; } if (!getResponse.IsValid) { Console.Write("Failed to retrieve document. "); if (getResponse.ServerError != null) { Console.WriteLine(getResponse.ServerError); } else if (getResponse.OriginalException != null) { Console.WriteLine(getResponse.OriginalException); } else { Console.WriteLine("Error code: " + getResponse.ApiCall.HttpStatusCode); } } else { Console.WriteLine($"Retrieved document: " + $"{{Id: {getResponse.Id}, " + $"Description: {getResponse.Source.Description}, " + $"Timestamp: {getResponse.Source.Timestamp}}}"); } } } } |
By Instaclustr Support
Need Support?
Experiencing difficulties on the website or console?
Already have an account?
Need help with your cluster?
Contact Support
Why sign up?