Search code examples
c#elasticsearchnest

ElasticSearch Nest Cross-Cluster Search C#


I need to perform a cross-cluster search using Nest but I'm not being able to find a way.

Uri elasticNode = new Uri(elasticSearchUri); ConnectionSettings nodeSettings = new ConnectionSettings(elasticNode).DefaultIndex(elasticIndexName); ElasticClient elasticClient = new ElasticClient(nodeSettings);

Find no documentation about it as well.

Any help would be appreciated.


Solution

  • To search across clusters, first need to have at least one remote cluster configured

    var pool = new SingleNodeConnectionPool(new Uri("http://example.com:9200"));
    var settings = new ConnectionSettings(pool);
    var client = new ElasticClient(settings);
    
    var putSettingsResponse = client.Cluster.PutSettings(s => s
        .Persistent(d => d
            .Add("cluster.remote.cluster_two.seeds", new[] { "127.0.0.1:9300" })
            .Add("cluster.remote.cluster_two.skip_unavailable", true)
        )
    );
    

    This configures a cluster named cluster_two at 127.0.0.1:9300 (use the transport layer port).

    Now, to search across both the cluster configured with the client and the remote cluster

    var searchResponse = client.Search<MyDocument>(s => s
        .Index("index_name,cluster_two:index_name")
        .Query(q => q
            .MatchAll()
        )
    );
    

    which searches across the index named "index_name" in the cluster at http://example.com:9200 and the index named "index_name" in remote cluster at 127.0.0.1