I am trying to save and fetch data from OpenSearch using C# .NET Core.
docker-compose up -d
, containers are successfully running and I am able to login to OpenSearch Dashboard via my browser.OpenSearch.Client (1.6.0)
package to it.Student
to an already created (I have also tried by not created the index) index: students
public class OpenSearchController : ControllerBase
{
private const String _admin = "admin";
private const String _indexStudents = "pmg-students";
//---------------tried these multiple URLs but none of them seems to work---------------
private const String _localhostUrl = "https://localhost:9200/";
private const String _node1Address = "https://opensearch-node1:9200";
private const String _node2Address = "https://opensearch-node2:9200";
private const String _myServer = "http://myserver:9200";
public IActionResult Add(Student student)
{
//----- When using this config, I get error: 'OriginalException: OpenSearch.Net.OpenSearchClientException: Request failed to execute. Call: Status code 404 from: PUT /students/_doc/1'
var node = new Uri(_localhostUrl);
var config = new ConnectionSettings(node)
.ServerCertificateValidationCallback((o, certificate, chain, errors) => true)
.ServerCertificateValidationCallback(CertificateValidations.AllowAll)
.BasicAuthentication(_admin, _admin)
.DisableDirectStreaming();
//----- When using this config, I get error: 'The remote certificate is invalid according to the validation procedure: RemoteCertificateNameMismatch'
var node = new Uri(_localhostUrl);
var config = new ConnectionSettings(node).DefaultIndex(_indexStudents);
//----- updated as per Ralf's comment, error: 'OriginalException: OpenSearch.Net.OpenSearchClientException: Request failed to execute. Call: Status code 404 from: PUT /students/_doc/1'
var node = new Uri(_localhostUrl);
var config = new ConnectionSettings(node)
.ServerCertificateValidationCallback((o, certificate, chain, errors) => true)
.ServerCertificateValidationCallback(CertificateValidations.AllowAll)
.DefaultIndex(_indexStudents);
var client = new OpenSearchClient(config);
var response = client.Index(student, i => i.Index(_indexStudents));
return new JsonResult(response);
}
}
I don't know, yet.
To save and fetch documents from OpenSearch (running on my local system) for a POC, without getting involved with certificates and their errors??
After investing much time I was able to figure out how to hit the OpenSearch URL on Windows 11 via C# .NET. And it has nothing to do with C# .NET.
All this time I was trying to access the https://localhost:9200
on Windows 11, while OpenSearch was running on the 9200
port of a different IP address.
This is how I did it:
ip addr
, the IP address mentioned in eth0
is where the OpenSearch is running.curl https://ip.address.from.eth0:9200 -ku 'admin:admin'
on WSL 2 shell.{
"name" : "a937e018cee5",
"cluster_name" : "docker-cluster",
"cluster_uuid" : "GLAjAG6bTeWErFUy_d-CLw",
"version" : {
"distribution" : "opensearch",
"number" : <version>,
"build_type" : <build-type>,
"build_hash" : <build-hash>,
"build_date" : <build-date>,
"build_snapshot" : false,
"lucene_version" : <lucene-version>,
"minimum_wire_compatibility_version" : "7.10.0",
"minimum_index_compatibility_version" : "7.0.0"
},
"tagline" : "The OpenSearch Project: https://opensearch.org/"
}
This means that OpenSearch is running on the specified IP address, and now you can use it in your .NET application and it will work.