Skip to content

Commit 2de46d2

Browse files
committed
Merge pull request #768 from elasticsearch/feature/http-compression
fix #693 can now instruct the HTTP client to accept gzip, deflated respo...
2 parents e2978fd + 387ff68 commit 2de46d2

File tree

6 files changed

+28
-2
lines changed

6 files changed

+28
-2
lines changed

src/Elasticsearch.Net/Connection/Configuration/ConnectionConfiguration.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ public class ConnectionConfiguration<T> : IConnectionConfigurationValues, IHideO
106106
private TimeSpan? _sniffLifeSpan;
107107
TimeSpan? IConnectionConfigurationValues.SniffInformationLifeSpan { get{ return _sniffLifeSpan; } }
108108

109+
private bool _compressionEnabled;
110+
bool IConnectionConfigurationValues.EnableCompressedResponses { get{ return _compressionEnabled; } }
111+
109112
private bool _traceEnabled;
110113
bool IConnectionConfigurationValues.TraceEnabled { get{ return _traceEnabled; } }
111114

@@ -159,6 +162,16 @@ public T SniffLifeSpan(TimeSpan sniffTimeSpan)
159162
return (T)this;
160163
}
161164

165+
/// <summary>
166+
/// Enable compressed responses from elasticsearch (NOTE that that nodes need to be configured to allow this)
167+
/// http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/modules-http.html
168+
/// </summary>
169+
public T EnableCompressedResponses(bool enabled = true)
170+
{
171+
this._compressionEnabled = enabled;
172+
return (T) this;
173+
}
174+
162175
/// <summary>
163176
/// Enable Trace signals to the IConnection that it should put debug information on the Trace.
164177
/// </summary>

src/Elasticsearch.Net/Connection/Configuration/IConnectionConfiguration.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ public interface IConnectionConfiguration<out T> : IHideObjectMembers
1313
where T : IConnectionConfiguration<T>
1414
{
1515

16+
/// <summary>
17+
/// Enable compressed responses from elasticsearch (NOTE that that nodes need to be configured to allow this)
18+
/// http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/modules-http.html
19+
/// </summary>
20+
T EnableCompressedResponses(bool enabled = true);
21+
1622

1723
/// <summary>
1824
/// Enable Trace signals to the IConnection that it should put debug information on the Trace.

src/Elasticsearch.Net/Connection/Configuration/IConnectionConfigurationValues.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public interface IConnectionConfigurationValues
1616
int? MaxDeadTimeout { get; }
1717
int? MaxRetries { get; }
1818
bool DisablePings { get; }
19+
bool EnableCompressedResponses { get; }
1920

2021
string ProxyAddress { get; }
2122
string ProxyUsername { get; }

src/Elasticsearch.Net/Connection/HttpConnection.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.IO;
4+
using System.IO.Compression;
45
using System.Linq;
56
using System.Net;
67
using System.Runtime.InteropServices;
@@ -170,9 +171,13 @@ protected virtual HttpWebRequest CreateWebRequest(Uri uri, string method, byte[]
170171
//var url = this._CreateUriString(path);
171172

172173
var myReq = (HttpWebRequest)WebRequest.Create(uri);
173-
174174
myReq.Accept = "application/json";
175175
myReq.ContentType = "application/json";
176+
if (this.ConnectionSettings.EnableCompressedResponses)
177+
{
178+
myReq.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
179+
myReq.Headers.Add("Accept-Encoding", "gzip,deflate");
180+
}
176181
if (requestSpecificConfig != null && !string.IsNullOrWhiteSpace(requestSpecificConfig.ContentType))
177182
{
178183
myReq.Accept = requestSpecificConfig.ContentType;

src/Tests/Nest.Tests.Integration/ElasticsearchConfiguration.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public static ConnectionSettings Settings(int? port = null, Uri hostOverride = n
3939
.ExposeRawResponse();
4040
}
4141

42-
public static readonly ElasticClient Client = new ElasticClient(Settings());
42+
public static readonly ElasticClient Client = new ElasticClient(Settings().EnableCompressedResponses());
4343
public static readonly ElasticClient ClientNoRawResponse = new ElasticClient(Settings().ExposeRawResponse(false));
4444
public static readonly ElasticClient ClientThatTrows = new ElasticClient(Settings().ThrowOnElasticsearchServerExceptions());
4545
public static readonly ElasticClient ThriftClient = new ElasticClient(Settings(9500), new ThriftConnection(Settings(9500)));

src/Tests/Nest.Tests.Integration/Reproduce/Reproduce325Tests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public void FluentMappingReturnsResults()
4444
})
4545
.AddMapping<TechnicalProduct>(m => MapTechnicalProduct(m, indexName)));
4646

47+
var index = this._client.GetIndexSettings(i=>i.Index(indexName));
4748
}
4849

4950

0 commit comments

Comments
 (0)