|
| 1 | +:ref_current: https://www.elastic.co/guide/en/elasticsearch/reference/master |
| 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/ClientConcepts/HighLevel/Inference/RoutingInference.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 | +[[routing-inference]] |
| 16 | +=== Routing inference |
| 17 | + |
| 18 | +==== Implicit conversion |
| 19 | + |
| 20 | +You can always create a routing explicitly by relying on the implicit conversion from the following types |
| 21 | + |
| 22 | +* `Int32` |
| 23 | + |
| 24 | +* `Int64` |
| 25 | + |
| 26 | +* `String` |
| 27 | + |
| 28 | +* `Guid` |
| 29 | + |
| 30 | +Methods and Properties that take an `Routing` can be passed any of these types and it will be implicitly |
| 31 | +converted to an instance of `Routing` |
| 32 | + |
| 33 | +[source,csharp] |
| 34 | +---- |
| 35 | +Routing routingFromInt = 1; |
| 36 | +Routing routingFromLong = 2L; |
| 37 | +Routing routingFromString = "hello-world"; |
| 38 | +Routing routingFromGuid = new Guid("D70BD3CF-4E38-46F3-91CA-FCBEF29B148E"); |
| 39 | +
|
| 40 | +Expect(1).WhenSerializing(routingFromInt); |
| 41 | +Expect(2).WhenSerializing(routingFromLong); |
| 42 | +Expect("hello-world").WhenSerializing(routingFromString); |
| 43 | +Expect("d70bd3cf-4e38-46f3-91ca-fcbef29b148e").WhenSerializing(routingFromGuid); |
| 44 | +---- |
| 45 | + |
| 46 | +==== Inferring from a type |
| 47 | + |
| 48 | +The real power of the `Routing` is in the inference rules (the default inferred routing for an object will be null). |
| 49 | +Lets look at an example of this given the following POCO: |
| 50 | + |
| 51 | +[source,csharp] |
| 52 | +---- |
| 53 | +class MyDTO |
| 54 | +{ |
| 55 | + public Guid Routing { get; set; } |
| 56 | + public string Name { get; set; } |
| 57 | + public string OtherName { get; set; } |
| 58 | +} |
| 59 | +---- |
| 60 | + |
| 61 | +By default NEST will try to find a property called `Routing` on the class using reflection |
| 62 | +and create a cached delegate based on the property getter |
| 63 | + |
| 64 | +[source,csharp] |
| 65 | +---- |
| 66 | +var dto = new MyDTO |
| 67 | +{ |
| 68 | + Routing = new Guid("D70BD3CF-4E38-46F3-91CA-FCBEF29B148E"), |
| 69 | + Name = "x", |
| 70 | + OtherName = "y" |
| 71 | +}; |
| 72 | +Expect(null).WhenInferringRoutingOn(dto); |
| 73 | +---- |
| 74 | + |
| 75 | +Using connection settings, you can specify a property that NEST should use to infer Routing for the document. |
| 76 | +Here we instruct NEST to infer the Routing for `MyDTO` based on its `Name` property |
| 77 | + |
| 78 | +[source,csharp] |
| 79 | +---- |
| 80 | +WithConnectionSettings(x => x |
| 81 | + .InferMappingFor<MyDTO>(m => m |
| 82 | + .RoutingProperty(p => p.Name) |
| 83 | + ) |
| 84 | +).Expect("x").WhenInferringRoutingOn(dto); |
| 85 | +---- |
| 86 | + |
| 87 | +IMPORTANT: Inference rules are cached __per__ `ConnectionSettings` instance. |
| 88 | + |
| 89 | +Because the cache is per `ConnectionSettings` instance, we can create another `ConnectionSettings` instance |
| 90 | +with different inference rules |
| 91 | + |
| 92 | +[source,csharp] |
| 93 | +---- |
| 94 | +WithConnectionSettings(x => x |
| 95 | + .InferMappingFor<MyDTO>(m => m |
| 96 | + .RoutingProperty(p => p.OtherName) |
| 97 | + ) |
| 98 | +).Expect("y").WhenInferringRoutingOn(dto); |
| 99 | +---- |
| 100 | + |
| 101 | +==== JoinField |
| 102 | + |
| 103 | +If your class has a property of type JoinField, NEST will automatically infer the parentid as the routing value. |
| 104 | + |
| 105 | +The name of this property can be anything. Be sure the read the <<parent-child-relationships, section on Parent/Child relationships>> to get a complete |
| 106 | +walkthrough on using Parent Child joins with NEST. |
| 107 | + |
| 108 | +[source,csharp] |
| 109 | +---- |
| 110 | +class MyOtherDTO |
| 111 | +{ |
| 112 | + public JoinField SomeJoinField { get; set; } |
| 113 | + public Guid Id { get; set; } |
| 114 | + public string Name { get; set; } |
| 115 | + public string OtherName { get; set; } |
| 116 | +} |
| 117 | +---- |
| 118 | + |
| 119 | +here we link this instance of `MyOtherDTO` with its parent id `"8080"` |
| 120 | + |
| 121 | +[source,csharp] |
| 122 | +---- |
| 123 | +var dto = new MyOtherDTO |
| 124 | +{ |
| 125 | + SomeJoinField = JoinField.Link<MyOtherDTO>("8080"), |
| 126 | + Id = new Guid("D70BD3CF-4E38-46F3-91CA-FCBEF29B148E"), |
| 127 | + Name = "x", |
| 128 | + OtherName = "y" |
| 129 | +}; |
| 130 | +Expect("8080").WhenInferringRoutingOn(dto); |
| 131 | +---- |
| 132 | + |
| 133 | +Here we link this instance as the root (parent) of the relation. NEST infers that the default routing for this instance |
| 134 | +should be the Id of the document itself. |
| 135 | + |
| 136 | +[source,csharp] |
| 137 | +---- |
| 138 | +dto = new MyOtherDTO |
| 139 | +{ |
| 140 | + SomeJoinField = JoinField.Root<MyOtherDTO>(), |
| 141 | + Id = new Guid("D70BD3CF-4E38-46F3-91CA-FCBEF29B148E"), |
| 142 | + Name = "x", |
| 143 | + OtherName = "y" |
| 144 | +}; |
| 145 | +Expect("d70bd3cf-4e38-46f3-91ca-fcbef29b148e").WhenInferringRoutingOn(dto); |
| 146 | +---- |
| 147 | + |
| 148 | +==== Precedence of ConnectionSettings |
| 149 | + |
| 150 | +The routing property configured on `ConnectionSettings` always takes precedence. |
| 151 | + |
| 152 | +[source,csharp] |
| 153 | +---- |
| 154 | +WithConnectionSettings(x => x |
| 155 | + .InferMappingFor<MyOtherDTO>(m => m |
| 156 | + .RoutingProperty(p => p.OtherName) |
| 157 | + ) |
| 158 | +).Expect("y").WhenInferringRoutingOn(dto); |
| 159 | +
|
| 160 | +class BadDTO |
| 161 | + { |
| 162 | +public JoinField SomeJoinField { get; set; } |
| 163 | +public JoinField AnotherJoinField { get; set; } |
| 164 | +public string ParentName { get; set; } |
| 165 | + } |
| 166 | +---- |
| 167 | + |
| 168 | +A class cannot contain more than one property of type JoinField, an exception is thrown in this case |
| 169 | + |
| 170 | +[source,csharp] |
| 171 | +---- |
| 172 | +var dto = new BadDTO |
| 173 | +{ |
| 174 | + SomeJoinField = JoinField.Link<MyOtherDTO>("8080"), |
| 175 | + AnotherJoinField = JoinField.Link<MyOtherDTO>("8081"), |
| 176 | + ParentName = "my-parent" |
| 177 | +}; |
| 178 | +Action resolve = () => Expect("8080").WhenInferringRoutingOn(dto); |
| 179 | +resolve.ShouldThrow<ArgumentException>().WithMessage("BadDTO has more than one JoinField property"); |
| 180 | +---- |
| 181 | + |
| 182 | +unless you configure the ConnectionSettings to use an alternate property: |
| 183 | + |
| 184 | +[source,csharp] |
| 185 | +---- |
| 186 | +WithConnectionSettings(x => x |
| 187 | + .InferMappingFor<BadDTO>(m => m |
| 188 | + .RoutingProperty(p => p.ParentName) |
| 189 | + ) |
| 190 | +).Expect("my-parent").WhenInferringRoutingOn(dto); |
| 191 | +---- |
| 192 | + |
0 commit comments