如何从 NEST 连接到弹性搜索
How to connect to elastic search from NEST
我目前 运行 弹性搜索和 kibana 版本 7.0.0 在我的项目中有一个 docker 撰写文件
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、healthcheck 或搜索等)时检索到以下异常。这让我相信 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.>
您需要在您的 docker-compose 文件中包含您的 search.api 服务,并且您的弹性服务在同一网络上。还要补充一点,search.api 依赖于弹性搜索服务。
services:
search.api:
image: ${DOCKER_REGISTRY-}searchapi
build:
context: .
dockerfile: Search.API/Dockerfile'
depends_on:
- elasticsearch
networks:
- esnetwork
我目前 运行 弹性搜索和 kibana 版本 7.0.0 在我的项目中有一个 docker 撰写文件
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、healthcheck 或搜索等)时检索到以下异常。这让我相信 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.>
您需要在您的 docker-compose 文件中包含您的 search.api 服务,并且您的弹性服务在同一网络上。还要补充一点,search.api 依赖于弹性搜索服务。
services:
search.api:
image: ${DOCKER_REGISTRY-}searchapi
build:
context: .
dockerfile: Search.API/Dockerfile'
depends_on:
- elasticsearch
networks:
- esnetwork