|
| 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 | + |
0 commit comments