Skip to content

Commit 7d99e0e

Browse files
russcamMpdreamz
authored andcommitted
Documentation for Union type (#3059)
1 parent d6e598a commit 7d99e0e

File tree

9 files changed

+284
-24
lines changed

9 files changed

+284
-24
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
:ref_current: https://www.elastic.co/guide/en/elasticsearch/reference/6.1
2+
3+
:github: https://github.com/elastic/elasticsearch-net
4+
5+
:nuget: https://www.nuget.org/packages
6+
7+
////
8+
IMPORTANT NOTE
9+
==============
10+
This file has been generated from https://github.com/elastic/elasticsearch-net/tree/master/src/Tests/CommonOptions/Union/Union.doc.cs.
11+
If you wish to submit a PR for any spelling mistakes, typos or grammatical errors for this file,
12+
please modify the original csharp file found at the link and submit the PR with that change. Thanks!
13+
////
14+
15+
[[union]]
16+
=== Union type
17+
18+
Some API parameters within Elasticsearch can accept more than one JSON data structure, for example, source filtering on
19+
a search request can accept
20+
21+
* a `bool` value to disable `_source` retrieval
22+
23+
* a `string` value representing a wildcard pattern to control what parts of `_source` to return
24+
25+
* an array of `string` values representing multiple wildcard patterns to control what parts of `_source` to return
26+
27+
* an `object` with `includes` and `excludes` properties that each accept an array of wildcard patterns to control
28+
what parts of `_source` to include and exclude, respectively.
29+
30+
That's a lot of different flexibility! NEST includes a `Union<TFirst,TSecond>` type to make it easier to work with
31+
these kinds of parameters, forming the union of two types, `TFirst` and `TSecond`.
32+
33+
==== Implicit conversion
34+
35+
The `Union<TFirst,TSecond>` has implicit operators to convert from an instance of `TFirst` or `TSecond` to an
36+
instance of `Union<TFirst,TSecond>`. This is often the easiest way of construction an instance
37+
38+
[source,csharp]
39+
----
40+
Union<bool, ISourceFilter> sourceFilterFalse = false;
41+
42+
Union<bool, ISourceFilter> sourceFilterInterface = new SourceFilter
43+
{
44+
Includes = new [] { "foo.*" }
45+
};
46+
----
47+
48+
==== Constructor
49+
50+
Sometimes, the constructor of `Union<TFirst,TSecond>` may be required in cases where the compiler is
51+
unable to infer the correct implicit conversion
52+
53+
[source,csharp]
54+
----
55+
var sourceFilterTrue = new Union<bool, ISourceFilter>(true);
56+
57+
var sourceFilterInterface = new Union<bool, ISourceFilter>(new SourceFilter
58+
{
59+
Includes = new [] { "foo.*" }
60+
});
61+
----
62+
63+
==== Match
64+
65+
The `Match` method can be used to operate on the value encapsulated by the instance of `Union<TFirst,TSecond>`.
66+
Two delegates are passed; one to operate on a `TFirst` value and the other toe operate on a `TSecond` value.
67+
68+
[source,csharp]
69+
----
70+
sourceFilterTrue.Match(
71+
b => b.Should().BeTrue(),
72+
s => s.Should().BeNull());
73+
74+
sourceFilterInterface.Match(
75+
b => b.Should().BeFalse(),
76+
s => s.Should().NotBeNull());
77+
----
78+
79+
The delegates can also return a value
80+
81+
[source,csharp]
82+
----
83+
var serializedFilterTrue = sourceFilterTrue.Match(
84+
b => serializer.SerializeToString(b),
85+
s => null);
86+
87+
serializedFilterTrue.Should().Be("true");
88+
89+
var serializedFilterInterface = sourceFilterTrue.Match(
90+
b => null,
91+
s => serializer.SerializeToString(s));
92+
93+
serializedFilterInterface.Should().Be("{\"includes\":[\"foo.*\"]}");
94+
----
95+

docs/high-level.asciidoc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ As well as a number of types for working with
4949

5050
* <<date-math-expressions, Date Math>>
5151

52+
* <<union, Union types>>
53+
5254
And features such as
5355

5456
* <<mapping, Mapping CLR POCO types to JSON documents>>
@@ -258,9 +260,13 @@ NEST has a number of types for working with Elasticsearch conventions for
258260

259261
* <<date-math-expressions, Date Math expressions>>
260262

263+
* <<union, Union types>>
264+
261265
include::common-options/time-unit/time-units.asciidoc[]
262266

263267
include::common-options/distance-unit/distance-units.asciidoc[]
264268

265269
include::common-options/date-math/date-math-expressions.asciidoc[]
266270

271+
include::common-options/union/union.asciidoc[]
272+

src/Nest/Aggregations/Bucket/Terms/TermsExclude.cs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,36 @@
33

44
namespace Nest
55
{
6+
/// <summary>
7+
/// Filters which terms to exclude from the response
8+
/// </summary>
69
[JsonConverter(typeof(TermsExcludeJsonConverter))]
710
public class TermsExclude
811
{
12+
/// <summary>
13+
/// The regular expression pattern to determine terms to exclude from the response
14+
/// </summary>
915
[JsonIgnore]
1016
public string Pattern { get; set; }
1117

18+
/// <summary>
19+
/// Collection of terms to exclude from the response
20+
/// </summary>
1221
[JsonIgnore]
1322
public IEnumerable<string> Values { get; set; }
1423

15-
public TermsExclude(string pattern)
16-
{
17-
Pattern = pattern;
18-
}
24+
/// <summary>
25+
/// Creates an instance of <see cref="TermsExclude"/> that uses a regular expression pattern
26+
/// to determine the terms to exclude from the response
27+
/// </summary>
28+
/// <param name="pattern">The regular expression pattern</param>
29+
public TermsExclude(string pattern) => Pattern = pattern;
1930

20-
public TermsExclude(IEnumerable<string> values)
21-
{
22-
Values = values;
23-
}
31+
/// <summary>
32+
/// Creates an instance of <see cref="TermsExclude"/> that uses a collection of terms
33+
/// to exclude from the response
34+
/// </summary>
35+
/// <param name="values">The exact terms to exclude</param>
36+
public TermsExclude(IEnumerable<string> values) => Values = values;
2437
}
25-
}
38+
}

src/Nest/Aggregations/Bucket/Terms/TermsInclude.cs

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,21 @@
44

55
namespace Nest
66
{
7+
/// <summary>
8+
/// Filters which terms to include in the response
9+
/// </summary>
710
[JsonConverter(typeof(TermsIncludeJsonConverter))]
811
public class TermsInclude
912
{
13+
/// <summary>
14+
/// The regular expression pattern to determine terms to include in the response
15+
/// </summary>
1016
[JsonIgnore]
1117
public string Pattern { get; set; }
1218

19+
/// <summary>
20+
/// Collection of terms to include in the response
21+
/// </summary>
1322
[JsonIgnore]
1423
public IEnumerable<string> Values { get; set; }
1524

@@ -25,16 +34,26 @@ public class TermsInclude
2534
[JsonProperty("num_partitions")]
2635
public long? NumberOfPartitions { get; set; }
2736

28-
public TermsInclude(string pattern)
29-
{
30-
Pattern = pattern;
31-
}
37+
/// <summary>
38+
/// Creates an instance of <see cref="TermsInclude"/> that uses a regular expression pattern
39+
/// to determine the terms to include in the response
40+
/// </summary>
41+
/// <param name="pattern">The regular expression pattern</param>
42+
public TermsInclude(string pattern) => Pattern = pattern;
3243

33-
public TermsInclude(IEnumerable<string> values)
34-
{
35-
Values = values;
36-
}
44+
/// <summary>
45+
/// Creates an instance of <see cref="TermsInclude"/> that uses a collection of terms
46+
/// to include in the response
47+
/// </summary>
48+
/// <param name="values">The exact terms to include</param>
49+
public TermsInclude(IEnumerable<string> values) => Values = values;
3750

51+
/// <summary>
52+
/// Creates an instance of <see cref="TermsInclude"/> that partitions the terms into a number of
53+
/// partitions to receive in multiple requests. Used to process many unique terms
54+
/// </summary>
55+
/// <param name="partition">The 0-based partition number for this request</param>
56+
/// <param name="numberOfPartitions">The total number of partitions</param>
3857
public TermsInclude(long partition, long numberOfPartitions)
3958
{
4059
Partition = partition;

src/Nest/CommonAbstractions/Union/Union.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,36 @@
33

44
namespace Nest
55
{
6+
/// <summary>
7+
/// Represents the union of two types, <typeparamref name="TFirst"/> and <typeparamref name="TSecond"/>. Used
8+
/// in scenarios where an Elasticsearch API may accept more than one different input data structure.
9+
/// </summary>
10+
/// <typeparam name="TFirst">The first type</typeparam>
11+
/// <typeparam name="TSecond">The second type</typeparam>
612
[JsonConverter(typeof(UnionJsonConverter))]
713
public class Union<TFirst, TSecond>
814
{
915
internal readonly TFirst Item1;
1016
internal readonly TSecond Item2;
1117
internal readonly int _tag;
1218

19+
/// <summary>
20+
/// Creates an new instance of <see cref="Union{TFirst,TSecond}"/> that encapsulates <paramref name="item"/> value
21+
/// </summary>
22+
/// <param name="item">The value to encapsulate</param>
1323
public Union(TFirst item) { Item1 = item; _tag = 0; }
24+
25+
/// <summary>
26+
/// Creates an new instance of <see cref="Union{TFirst,TSecond}"/> that encapsulates <paramref name="item"/> value
27+
/// </summary>
28+
/// <param name="item">The value to encapsulate</param>
1429
public Union(TSecond item) { Item2 = item; _tag = 1; }
1530

31+
/// <summary>
32+
/// Runs an <see cref="Action{T}"/> delegate against the encapsulated value
33+
/// </summary>
34+
/// <param name="first">The delegate to run when this instance encapsulates an instance of <typeparamref name="TFirst"/></param>
35+
/// <param name="second">The delegate to run when this instance encapsulates an instance of <typeparamref name="TSecond"/></param>
1636
public void Match(Action<TFirst> first, Action<TSecond> second)
1737
{
1838
switch (_tag)
@@ -26,6 +46,12 @@ public void Match(Action<TFirst> first, Action<TSecond> second)
2646
default: throw new Exception($"Unrecognized tag value: {_tag}");
2747
}
2848
}
49+
50+
/// <summary>
51+
/// Runs a <see cref="Func{T,TResult}"/> delegate against the encapsulated value
52+
/// </summary>
53+
/// <param name="first">The delegate to run when this instance encapsulates an instance of <typeparamref name="TFirst"/></param>
54+
/// <param name="second">The delegate to run when this instance encapsulates an instance of <typeparamref name="TSecond"/></param>
2955
public T Match<T>(Func<TFirst, T> first, Func<TSecond, T> second)
3056
{
3157
switch (_tag)

src/Nest/CommonAbstractions/Union/UnionJsonConverter.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ internal class UnionJsonConverter : JsonConverter
1515

1616
public static UnionJsonConverterBase CreateConverter(Type t)
1717
{
18-
UnionJsonConverterBase conversion;
19-
if (KnownTypes.TryGetValue(t, out conversion))
18+
if (KnownTypes.TryGetValue(t, out var conversion))
2019
return conversion;
2120

2221
var genericArguments = t.GetTypeInfo().GenericTypeArguments;
@@ -55,9 +54,11 @@ public bool TryRead<T>(JsonReader reader, JsonSerializer serializer, out T v)
5554
v = serializer.Deserialize<T>(reader);
5655
return true;
5756
}
58-
catch {}
59-
v= default(T);
60-
return false;
57+
catch
58+
{
59+
v = default(T);
60+
return false;
61+
}
6162
}
6263

6364
public abstract void WriteJson(JsonWriter writer, object v, JsonSerializer serializer);

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ public class SourceFilterDescriptor<T> : DescriptorBase<SourceFilterDescriptor<T
2929

3030
Fields ISourceFilter.Excludes { get; set; }
3131

32-
3332
public SourceFilterDescriptor<T> Includes(Func<FieldsDescriptor<T>, IPromise<Fields>> fields) =>
3433
Assign(a => a.Includes = fields?.Invoke(new FieldsDescriptor<T>())?.Value);
3534

0 commit comments

Comments
 (0)