Skip to content

Commit b962321

Browse files
committed
Merge branch 'master' of github.com:elastic/elasticsearch-net
2 parents 28f4eb1 + b13e157 commit b962321

File tree

10 files changed

+262
-86
lines changed

10 files changed

+262
-86
lines changed

src/Benchmarking/project.lock.json

Lines changed: 36 additions & 36 deletions
Large diffs are not rendered by default.

src/Elasticsearch.Net/project.lock.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2025,7 +2025,7 @@
20252025
"System.Dynamic.Runtime/4.0.11-beta-23516": {
20262026
"type": "package",
20272027
"serviceable": true,
2028-
"sha512": "C2GXB20I5vMcO4wemZr5pEjwwEb6H6zVkwF12JSUhripKBIKgI0YKpfp9glyDSL903cYgIXAztMQDajwCR0PmA==",
2028+
"sha512": "ypkxS0e+yUw7F6JEwuB22u0qqruMeZFOmtcImh2efDHpTAuhF2FOqCDJ7f4qLf9yomVvB4kjkZ6xGunbIQryxQ==",
20292029
"files": [
20302030
"lib/DNXCore50/System.Dynamic.Runtime.dll",
20312031
"lib/MonoAndroid10/_._",

src/Nest/Aggregations/Bucket/DateHistogram/DateHistogramAggregation.cs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,25 @@ public interface IDateHistogramAggregation : IBucketAggregation
4848

4949
public class DateHistogramAggregation : BucketAggregationBase, IDateHistogramAggregation
5050
{
51+
private string _format;
5152
public Field Field { get; set; }
5253
public IScript Script { get; set; }
5354
public IDictionary<string, object> Params { get; set; }
5455
public Union<DateInterval, Time> Interval { get; set; }
55-
public string Format { get; set; }
56+
57+
public string Format
58+
{
59+
get
60+
{
61+
return !string.IsNullOrEmpty(_format) &&
62+
!_format.Contains("date_optional_time") &&
63+
ExtendedBounds != null
64+
? _format + "||date_optional_time"
65+
: _format;
66+
}
67+
set { _format = value; }
68+
}
69+
5670
public int? MinimumDocumentCount { get; set; }
5771
public string TimeZone { get; set; }
5872
public int? Factor { get; set; }
@@ -73,6 +87,7 @@ public class DateHistogramAggregationDescriptor<T>
7387
, IDateHistogramAggregation
7488
where T : class
7589
{
90+
private string _format;
7691
Field IDateHistogramAggregation.Field { get; set; }
7792

7893
IScript IDateHistogramAggregation.Script { get; set; }
@@ -81,7 +96,18 @@ public class DateHistogramAggregationDescriptor<T>
8196

8297
Union<DateInterval, Time> IDateHistogramAggregation.Interval { get; set; }
8398

84-
string IDateHistogramAggregation.Format { get; set; }
99+
string IDateHistogramAggregation.Format
100+
{
101+
get
102+
{
103+
return !string.IsNullOrEmpty(_format) &&
104+
!_format.Contains("date_optional_time") &&
105+
Self.ExtendedBounds != null
106+
? _format + "||date_optional_time"
107+
: _format;
108+
}
109+
set { _format = value; }
110+
}
85111

86112
int? IDateHistogramAggregation.MinimumDocumentCount { get; set; }
87113

src/Nest/Search/Search/SourceFiltering/SourceFilter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace Nest
55
{
6-
[JsonConverter(typeof(ReadAsTypeJsonConverter<SourceFilter>))]
6+
[JsonConverter(typeof(SourceFilterJsonConverter))]
77
public interface ISourceFilter
88
{
99
[JsonProperty("include")]
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Newtonsoft.Json;
4+
5+
namespace Nest
6+
{
7+
public class SourceFilterJsonConverter : JsonConverter
8+
{
9+
public override bool CanRead => true;
10+
public override bool CanWrite => false;
11+
public override bool CanConvert(Type objectType) => true;
12+
13+
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
14+
{
15+
}
16+
17+
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
18+
{
19+
if (reader.TokenType == JsonToken.Null) return null;
20+
21+
var filter = new SourceFilter();
22+
switch (reader.TokenType)
23+
{
24+
case JsonToken.Boolean:
25+
filter.Exclude = new[] { "*" };
26+
break;
27+
case JsonToken.String:
28+
filter.Include = new [] { (string)reader.Value };
29+
break;
30+
case JsonToken.StartArray:
31+
var include = new List<string>();
32+
while (reader.Read() && reader.TokenType != JsonToken.EndArray)
33+
include.Add((string)reader.Value);
34+
filter.Include = include.ToArray();
35+
break;
36+
default:
37+
serializer.Populate(reader, filter);
38+
break;
39+
}
40+
41+
return filter;
42+
}
43+
}
44+
}

src/Nest/project.lock.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2037,7 +2037,7 @@
20372037
"runtime.any.System.Linq.Expressions/4.0.11-beta-23516": {
20382038
"type": "package",
20392039
"serviceable": true,
2040-
"sha512": "P5nzo1Ye0GxB4BYdWian6Y427eTrhn1JS3jLWZq5bMWVn8hS/OIfyylASN0A/qqeLn4rGA0fOzmJSYqFSKvxgQ==",
2040+
"sha512": "4sPxQCjllMJ1uZNlwz/EataPyHSH+AqSDlOIPPqcy/88R2B+abfhPPC78rd7gvHp8KmMX4qbJF6lcCeDIQpmVg==",
20412041
"files": [
20422042
"lib/DNXCore50/System.Linq.Expressions.dll",
20432043
"lib/MonoAndroid10/_._",
@@ -2345,7 +2345,7 @@
23452345
"System.Dynamic.Runtime/4.0.11-beta-23516": {
23462346
"type": "package",
23472347
"serviceable": true,
2348-
"sha512": "C2GXB20I5vMcO4wemZr5pEjwwEb6H6zVkwF12JSUhripKBIKgI0YKpfp9glyDSL903cYgIXAztMQDajwCR0PmA==",
2348+
"sha512": "ypkxS0e+yUw7F6JEwuB22u0qqruMeZFOmtcImh2efDHpTAuhF2FOqCDJ7f4qLf9yomVvB4kjkZ6xGunbIQryxQ==",
23492349
"files": [
23502350
"lib/DNXCore50/System.Dynamic.Runtime.dll",
23512351
"lib/MonoAndroid10/_._",
@@ -2599,7 +2599,7 @@
25992599
"System.Linq.Expressions/4.0.11-beta-23516": {
26002600
"type": "package",
26012601
"serviceable": true,
2602-
"sha512": "FtKytB13HabzrSvrAgBgOOnG2uxJO4s7zvP5Sk0NS3bwbJUyb5AP1p4897UWnLiB6C95jI4nIkZps51sa9In8g==",
2602+
"sha512": "YEl5oyF5fifLbHHP099cvb/6f2r2h1QVHzoaoINPHOZtpNec+RfqvzETXcYDIdHT7l+bBAYsBuVUkBgfQEoYfQ==",
26032603
"files": [
26042604
"lib/MonoAndroid10/_._",
26052605
"lib/MonoTouch10/_._",

src/Tests/Aggregations/Bucket/DateHistogram/DateHistogramAggregationUsageTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ namespace Tests.Aggregations.Bucket.DateHistogram
1414
* The main difference is that the interval can be specified by date/time expressions.
1515
*
1616
* When specifying a format and extended_bounds, in order for Elasticsearch to be able to parse
17-
* the serialized DateTimes extended_bounds correctly, the date_optional_time format should
18-
* also be specified as an additional format.
17+
* the serialized DateTimes of extended_bounds correctly, the date_optional_time format is included
18+
* as part of the format value.
1919
*
2020
* Be sure to read the elasticsearch documentation {ref}/search-aggregations-bucket-datehistogram-aggregation.html[on this subject here]
2121
*/
@@ -72,7 +72,7 @@ public DateHistogramAggregationUsageTests(ReadOnlyCluster i, EndpointUsage usage
7272
.Field(p => p.StartedOn)
7373
.Interval(DateInterval.Month)
7474
.MinimumDocumentCount(2)
75-
.Format("yyyy-MM-dd'T'HH:mm:ss||date_optional_time")
75+
.Format("yyyy-MM-dd'T'HH:mm:ss")
7676
.ExtendedBounds(FixedDate.AddYears(-1), FixedDate.AddYears(1))
7777
.Order(HistogramOrder.CountAscending)
7878
.Missing(FixedDate)
@@ -96,7 +96,7 @@ public DateHistogramAggregationUsageTests(ReadOnlyCluster i, EndpointUsage usage
9696
Field = Field<Project>(p => p.StartedOn),
9797
Interval = DateInterval.Month,
9898
MinimumDocumentCount = 2,
99-
Format = "yyyy-MM-dd'T'HH:mm:ss||date_optional_time",
99+
Format = "yyyy-MM-dd'T'HH:mm:ss",
100100
ExtendedBounds = new ExtendedBounds<DateTime>
101101
{
102102
Minimum = FixedDate.AddYears(-1),

src/Tests/Framework/SerializationTestBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ private void Sort(JObject jObj)
104104
}
105105
}
106106

107-
private TObject Deserialize<TObject>(string json) =>
107+
protected TObject Deserialize<TObject>(string json) =>
108108
GetSerializer().Deserialize<TObject>(new MemoryStream(Encoding.UTF8.GetBytes(json)));
109109

110110
protected string Serialize<TObject>(TObject o)
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
using System;
2+
using Tests.Framework.Integration;
3+
using Tests.Framework.MockData;
4+
using Nest;
5+
using FluentAssertions;
6+
using Tests.Framework;
7+
using Newtonsoft.Json;
8+
using static Nest.Infer;
9+
10+
namespace Tests.Search.Request
11+
{
12+
/** Allows to control how the _source field is returned with every hit.
13+
* By default operations return the contents of the _source field unless
14+
* you have used the fields parameter or if the _source field is disabled.
15+
*/
16+
public class SourceFilteringUsageTests : SearchUsageTestBase
17+
{
18+
public SourceFilteringUsageTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(cluster, usage) { }
19+
20+
protected override object ExpectJson =>
21+
new
22+
{
23+
_source = new
24+
{
25+
include = new[] { "name", "startedOn" }
26+
}
27+
};
28+
29+
protected override Func<SearchDescriptor<Project>, ISearchRequest> Fluent => s => s
30+
.Source(so => so
31+
.Include(f => f
32+
.Fields(
33+
p => p.Name,
34+
p => p.StartedOn
35+
)
36+
)
37+
);
38+
39+
protected override SearchRequest<Project> Initializer =>
40+
new SearchRequest<Project>
41+
{
42+
Source = new SourceFilter
43+
{
44+
Include = Fields<Project>(p => p.Name, prop => prop.StartedOn)
45+
}
46+
};
47+
48+
protected override void ExpectResponse(ISearchResponse<Project> response)
49+
{
50+
response.IsValid.Should().BeTrue();
51+
52+
foreach (var document in response.Documents)
53+
{
54+
document.Name.Should().NotBeNull();
55+
document.StartedOn.Should().NotBe(default(DateTime));
56+
document.Description.Should().BeNull();
57+
}
58+
}
59+
}
60+
61+
public class SourceFilteringSerializationTests : SerializationTestBase
62+
{
63+
internal class WithSourceFilterProperty
64+
{
65+
[JsonProperty("_source")]
66+
public ISourceFilter SourceFilter { get; set; }
67+
}
68+
69+
[U]
70+
public void CanDeserializeBoolean()
71+
{
72+
var o = base.Deserialize<WithSourceFilterProperty>("{ \"_source\": false }");
73+
o.Should().NotBeNull();
74+
o.SourceFilter.Should().NotBeNull();
75+
o.SourceFilter.Exclude.Should().Contain("*");
76+
}
77+
78+
[U]
79+
public void CanDeserializeArray()
80+
{
81+
var o = base.Deserialize<WithSourceFilterProperty>("{ \"_source\": [\"obj.*\"] }");
82+
o.Should().NotBeNull();
83+
o.SourceFilter.Should().NotBeNull();
84+
o.SourceFilter.Include.Should().Contain("obj.*");
85+
}
86+
87+
[U]
88+
public void CanDeserializeString()
89+
{
90+
var o = base.Deserialize<WithSourceFilterProperty>("{ \"_source\": \"obj.*\" }");
91+
o.Should().NotBeNull();
92+
o.SourceFilter.Should().NotBeNull();
93+
o.SourceFilter.Include.Should().Contain("obj.*");
94+
}
95+
96+
[U]
97+
public void CanDeserializeObject()
98+
{
99+
var o = base.Deserialize<WithSourceFilterProperty>("{ \"_source\": { \"include\": [\"obj.*\"], \"exclude\": [\"foo.*\"] } }");
100+
o.Should().NotBeNull();
101+
o.SourceFilter.Should().NotBeNull();
102+
o.SourceFilter.Include.Should().Contain("obj.*");
103+
o.SourceFilter.Exclude.Should().Contain("foo.*");
104+
}
105+
}
106+
}

0 commit comments

Comments
 (0)