如何从NEST连接到弹性搜索

时间:2019-04-16 07:15:38

标签: elasticsearch docker-compose nest

我当前在项目中使用docker compose文件运行弹性搜索和kibana版本7.0.0

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:

我能够在端口5601上连接到kibana(索引和搜索数据),以及在端口9200上进行弹性搜索。

我正在尝试使用NEST连接到Elastic,这是我的基本配置

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;
}

通过NEST执行任何命令(例如ping,运行状况检查或搜索等)时,我检索到以下异常。这使我相信docker容器(linux容器)中存在一些与网络相关的问题,但目前我很困惑

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.>

2 个答案:

答案 0 :(得分:2)

您需要在docker-compose文件中拥有search.api服务,并在同一网络上拥有弹性服务。还应补充说明search.api取决于弹性搜索服务。

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

答案 1 :(得分:0)

嗨,我有同样的问题,但是我的API和弹性搜索具有相同的网络。 docker-compose.yml中的此配置有问题吗?

tp3-客户:     图片:tp3-客户     重新启动:失败     端口:       -5011:80
    取决于:       -tp3-customer-sqlserver-db       -elasticsearch     网络:       -麋鹿