Search code examples
c#.net-coreopensearchamazon-opensearch

How to use C# .NET Core to save and access documents from OpenSearch?


I am trying to save and fetch data from OpenSearch using C# .NET Core.

What I did:

  1. On my local system I have setup OpenSearch by following: Using OpenSearch with Docker Compose.
  2. Upon using the command: docker-compose up -d, containers are successfully running and I am able to login to OpenSearch Dashboard via my browser.
  3. Created a fresh .NET Core project and added OpenSearch.Client (1.6.0) package to it.
  4. Here is the code snippet used to save an object of type 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);
  }
}
  1. Tried disabling certificates as well.
  2. Browsed through the samples provided by the opensearch-project on GitHub.
  3. And of course, went through the official OpenSearch documentation.

What I am I missing?

I don't know, yet.

What I want to do:

To save and fetch documents from OpenSearch (running on my local system) for a POC, without getting involved with certificates and their errors??


Solution

  • 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:

    1. First I installed Docker Engine and Docker Compose on WSL 2 in Windows 11
    2. Then download and run OpenSearch
    3. On your WSL 2 shell, execute: ip addr, the IP address mentioned in eth0 is where the OpenSearch is running.
    4. To verify, execute: curl https://ip.address.from.eth0:9200 -ku 'admin:admin' on WSL 2 shell.
    5. And voilà, your will get a response that looks like this (if you have correctly installed docker, docker compose and OpenSearch):
    {
      "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.