- .NET Clients: other versions:
- Introduction
- Breaking changes
- API Conventions
- Elasticsearch.Net - Low level client
- NEST - High level client
- Troubleshooting
- Search
- Query DSL
- Full text queries
- Term level queries
- Exists Query Usage
- Fuzzy Date Query Usage
- Fuzzy Numeric Query Usage
- Fuzzy Query Usage
- Ids Query Usage
- Prefix Query Usage
- Date Range Query Usage
- Long Range Query Usage
- Numeric Range Query Usage
- Term Range Query Usage
- Regexp Query Usage
- Term Query Usage
- Terms List Query Usage
- Terms Lookup Query Usage
- Terms Query Usage
- Terms Set Query Usage
- Type Query Usage
- Wildcard Query Usage
- Compound queries
- Joining queries
- Geo queries
- Geo Bounding Box Query Usage
- Geo Distance Query Usage
- Geo Polygon Query Usage
- Geo Shape Circle Query Usage
- Geo Shape Envelope Query Usage
- Geo Shape Geometry Collection Query Usage
- Geo Shape Indexed Shape Query Usage
- Geo Shape Line String Query Usage
- Geo Shape Multi Line String Query Usage
- Geo Shape Multi Point Query Usage
- Geo Shape Multi Polygon Query Usage
- Geo Shape Point Query Usage
- Geo Shape Polygon Query Usage
- Specialized queries
- Span queries
- NEST specific queries
- Aggregations
- Metric Aggregations
- Average Aggregation Usage
- Cardinality Aggregation Usage
- Extended Stats Aggregation Usage
- Geo Bounds Aggregation Usage
- Geo Centroid Aggregation Usage
- Max Aggregation Usage
- Median Absolute Deviation Aggregation Usage
- Min Aggregation Usage
- Percentile Ranks Aggregation Usage
- Percentiles Aggregation Usage
- Scripted Metric Aggregation Usage
- Stats Aggregation Usage
- Sum Aggregation Usage
- Top Hits Aggregation Usage
- Value Count Aggregation Usage
- Weighted Average Aggregation Usage
- Bucket Aggregations
- Adjacency Matrix Usage
- Auto Date Histogram Aggregation Usage
- Children Aggregation Usage
- Composite Aggregation Usage
- Date Histogram Aggregation Usage
- Date Range Aggregation Usage
- Filter Aggregation Usage
- Filters Aggregation Usage
- Geo Distance Aggregation Usage
- Geo Hash Grid Aggregation Usage
- Global Aggregation Usage
- Histogram Aggregation Usage
- Ip Range Aggregation Usage
- Missing Aggregation Usage
- Nested Aggregation Usage
- Parent Aggregation Usage
- Range Aggregation Usage
- Reverse Nested Aggregation Usage
- Sampler Aggregation Usage
- Significant Terms Aggregation Usage
- Significant Text Aggregation Usage
- Terms Aggregation Usage
- Pipeline Aggregations
- Average Bucket Aggregation Usage
- Bucket Script Aggregation Usage
- Bucket Selector Aggregation Usage
- Bucket Sort Aggregation Usage
- Cumulative Sum Aggregation Usage
- Derivative Aggregation Usage
- Extended Stats Bucket Aggregation Usage
- Max Bucket Aggregation Usage
- Min Bucket Aggregation Usage
- Moving Average Ewma Aggregation Usage
- Moving Average Holt Linear Aggregation Usage
- Moving Average Holt Winters Aggregation Usage
- Moving Average Linear Aggregation Usage
- Moving Average Simple Aggregation Usage
- Moving Function Aggregation Usage
- Percentiles Bucket Aggregation Usage
- Serial Differencing Aggregation Usage
- Stats Bucket Aggregation Usage
- Sum Bucket Aggregation Usage
- Matrix Aggregations
- Metric Aggregations
NOTE: You are looking at documentation for an older release. For the latest information, see the current release documentation.
Modifying the default connection
editModifying the default connection
editThe client abstracts sending the request and creating a response behind IConnection
and the default
implementation uses
-
System.Net.WebRequest
for Desktop CLR -
System.Net.Http.HttpClient
for Core CLR
The reason for different implementations is that WebRequest
and ServicePoint
are not directly available
on netstandard 1.3.
The Desktop CLR implementation using WebRequest
is the most mature implementation, having been tried and trusted
in production since the beginning of NEST. For this reason, we aren’t quite ready to it give up in favour of
a HttpClient
implementation across all CLR versions.
In addition to production usage, there are also a couple of important toggles that are easy to set against a
ServicePoint
that are not possible to set as yet on HttpClient
.
Finally, another limitation is that HttpClient
has no synchronous code paths, so supporting these means
doing hacky async patches which definitely need time to bake.
So why would you ever want to pass your own IConnection
? Let’s look at a couple of examples
Using InMemoryConnection
editInMemoryConnection
is an in-built IConnection
that makes it easy to write unit tests against. It can be
configured to respond with default response bytes, HTTP status code and an exception when a call is made.
InMemoryConnection
doesn’t actually send any requests or receive any responses from Elasticsearch;
requests are still serialized and the request bytes can be obtained on the response if .DisableDirectStreaming
is
set to true
on the request or globally
var connection = new InMemoryConnection(); var connectionPool = new SingleNodeConnectionPool(new Uri("http://localhost:9200")); var settings = new ConnectionSettings(connectionPool, connection); var client = new ElasticClient(settings);
Here we create a new ConnectionSettings
by using the overload that takes a IConnectionPool
and an IConnection
.
We pass it an InMemoryConnection
which, using the default parameterless constructor,
will return 200 for everything and never actually perform any IO.
Let’s see a more complex example
var response = new { took = 1, timed_out = false, _shards = new { total = 2, successful = 2, failed = 0 }, hits = new { total = 25, max_score = 1.0, hits = Enumerable.Range(1, 25).Select(i => (object)new { _index = "project", _type = "project", _id = $"Project {i}", _score = 1.0, _source = new { name = $"Project {i}" } }).ToArray() } }; var responseBytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(response)); var connection = new InMemoryConnection(responseBytes, 200); var connectionPool = new SingleNodeConnectionPool(new Uri("http://localhost:9200")); var settings = new ConnectionSettings(connectionPool, connection).DefaultIndex("project"); var client = new ElasticClient(settings); var searchResponse = client.Search<Project>(s => s.MatchAll());
We can now assert that the searchResponse
is valid and contains documents deserialized
from our fixed InMemoryConnection
response
searchResponse.ShouldBeValid(); searchResponse.Documents.Count.Should().Be(25);
Changing HttpConnection
editThere may be a need to change how the default HttpConnection
works, for example, to add an X509 certificate
to the request, change the maximum number of connections allowed to an endpoint, etc.
By deriving from HttpConnection
, it is possible to change the behaviour of the connection. The following
provides some examples
Kerberos Authentication
editFor a lot of use cases, deriving from HttpConnection
is a great way to customize the connection for your needs.
If you want to authenticate with Kerberos for example, creating a custom HttpConnection
as follows
allows you to set the right HTTP headers
use something like https://www.nuget.org/packages/Kerberos.NET/ to fill in the actual blanks of this implementation
public class KerberosConnection : HttpConnection { protected override HttpRequestMessage CreateRequestMessage(RequestData requestData) { var message = base.CreateRequestMessage(requestData); var header = string.Empty; message.Headers.Authorization = new AuthenticationHeaderValue("Negotiate", header); return message; } }