Search code examples
elasticsearchdocker-composenest

How to connect to elastic search from NEST


I'm currently running elastic search and kibana version 7.0.0 with a docker compose file in my project

version: '3.4'

services:
  search.api:
    image: ${DOCKER_REGISTRY-}searchapi
    build:
      context: .
      dockerfile: Search.API/Dockerfile

  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:7.0.0
    container_name: elasticsearch
    environment:
      - discovery.type=single-node
      - cluster.name=docker-cluster
      - bootstrap.memory_lock=true
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
    ulimits:
      memlock:
        soft: -1
        hard: -1
    ports:
      - "9200:9200"
      - "9300:9300"
    volumes:
      - es_data:/elasticsearch/data
    networks:
      - esnetwork
  kibana:
    image: docker.elastic.co/kibana/kibana:7.0.0
    container_name: kibana
    environment:
      - "ELASTICSEARCH_URL=http://elasticsearch:9200"
    ports:
      - "5601:5601"
    networks:
      - esnetwork
    depends_on:
      - elasticsearch
networks:
    esnetwork:
        driver: bridge

volumes:
  es_data:

I am able to connect to kibana on port 5601 (index and search data) as well as elastic search on port 9200.

I'm trying to use NEST to connect to elastic here is my basic configuration

public static IServiceCollection AddElasticsearch(this IServiceCollection services, IConfiguration configuration)
{
    var settings = new ConnectionSettings(new Uri("http://localhost:9200"));
    var client = new ElasticClient(settings);

    var pingResponse = client.Ping(new PingRequest());
    Log.Debug("Ping client {0}",pingResponse);

    services.AddSingleton<IElasticClient>(client);

    return services;
}

I retrieve the following exception when performing any command through NEST such as ping, healthcheck or search etc. this leads me to believe there is some network related issue in the docker containers (linux containers) but i'm stumped at the moment

Invalid NEST response built from a unsuccessful low level call on POST: /clients/_search?typed_keys=true
# Audit trail of this API call:
 - [1] BadRequest: Node: http://localhost:9200/ Took: 00:00:00.8555510
# OriginalException: System.Net.Http.HttpRequestException: Cannot assign requested address ---> System.Net.Sockets.SocketException: Cannot assign requested address
   at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken)
   --- End of inner exception stack trace ---
   at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken)
   at System.Threading.Tasks.ValueTask`1.get_Result()
   at System.Net.Http.HttpConnectionPool.CreateConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   at System.Threading.Tasks.ValueTask`1.get_Result()
   at System.Net.Http.HttpConnectionPool.WaitForCreatedConnectionAsync(ValueTask`1 creationTask)
   at System.Threading.Tasks.ValueTask`1.get_Result()
   at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
   at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   at System.Net.Http.HttpClient.FinishSendAsyncBuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
   at Elasticsearch.Net.HttpConnection.RequestAsync[TResponse](RequestData requestData, CancellationToken cancellationToken)
# Request:
<Request stream not captured or already read to completion by serializer. Set DisableDirectStreaming() on ConnectionSettings to force it to be set on the response.>
# Response:
<Response stream not captured or already read to completion by serializer. Set DisableDirectStreaming() on ConnectionSettings to force it to be set on the response.>

Solution

  • You need to have your search.api service in your docker-compose file and your elastic service on the same network. Also add that the search.api depends on the elastic search service.

    services:
      search.api:
        image: ${DOCKER_REGISTRY-}searchapi
        build:
          context: .
          dockerfile: Search.API/Dockerfile'
        depends_on:
          - elasticsearch
        networks:
          - esnetwork