Skip to content

Conversation

@Mpdreamz
Copy link
Member

@Mpdreamz Mpdreamz commented Jan 3, 2018

This PR strongly types the routing parameter to Routing and introduces a new feature called auto routing.

This feature will automatically append routing=key if we see the Poco is using a JoinField in which case we will send the parent id provided there. If the join field itself is the root and only defines the root relation name NEST will send the id of the root object. This plays nicely when _routing is required on the mapping. This will hopefully aid people as they move to single types and the new way to do parent and child mappings. Also finished the documentation section for parent and child mappings 6.x style so that we can link to it from the 6.0 GA release blog post.

You can also configure a default routing lookup property on ConnectionSettings through InferMappingFor<>(). This takes precedence over NEST looking for a JoinField property.

These defaults kick in for request and objects (LikeDocument. BulkOperation, MultiTermVectorOperation, PercolateQuery) that have a routing parameter AND a .NET document to infer on. For requests this is automated from the code generator.

And lastly an explicitly set Routing(x) or Routing = x by the user takes precedence over all, and if x == null it will nullify the routing.

Important to note: For POCO's that have neither a JoinField or a mapping set up for routing on the ConnectionSettings the routing parameter is not send automatically so existing code should not be affected.

See docs:

https://github.com/elastic/elasticsearch-net/blob/01b7731dab069e8967d3b3baccda0efa092f800d/src/Tests/ClientConcepts/HighLevel/Mapping/ParentChildJoins.doc.cs

https://github.com/elastic/elasticsearch-net/blob/01b7731dab069e8967d3b3baccda0efa092f800d/src/Tests/ClientConcepts/HighLevel/Inference/RoutingInference.doc.cs

@Raw(string.Join("\r\n\t\t", desc.Select(d=> "/// " + d)))
///</summary></text>
}</text><text>@if (!string.IsNullOrWhiteSpace(kv.Value.Obsolete))
{<text> [Obsolete("Scheduled to be removed in 7.0, @kv.Value.Obsolete")]</text>}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@foreach (CsharpMethod method in Model.CsharpMethodsWithQueryStringInfo)
{
if (!method.Url.Params.Any(p => p.Key.Contains("field") || p.Key.Contains("source_")))
if (!method.Url.Params.Any(p => p.Key.Contains("field") || p.Key.Contains("source_") || p.Key.Contains("routing")))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These chained conditions always scare me as I have to read to work out what they mean.
Much prefer:

var nameOfCompositeCondition = ............................;
if (nameOfCompositeCondition) { ............ }

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

}</text><text>@if(!string.IsNullOrWhiteSpace(kv.Value.DeprecatedInFavorOf))
{
<text>[Obsolete("Scheduled to be removed in 5.0, use @kv.Value.DeprecatedInFavorOf instead")]</text>
<text>[Obsolete("Scheduled to be removed in 7.0, use @kv.Value.DeprecatedInFavorOf instead")]</text>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Contributor

@codebrain codebrain left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some changes.
This review has taken quite a bit of time!

}

/**
* ==== Inferring from a type
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The real power of the Routingis in the inference rules (the default inferred routing for an object will be null)

/**
* ==== JoinField
*
* If your POCO has a `JoinField` property however NEST will automatically infer the parentid as the routing value.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If your class has a property of type JoinField, NEST will automatically infer the parentid as the routing value.

Expect("8080").WhenInferringRoutingOn(dto);

/**
* here we link this instance as the root (parent) of the relation. Nest is smart enough to infer that the default routing
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we link this instance as the root (parent) of the relation. NEST infers that the default routing for this instance should be the Id of the document itself.

/**
* ==== Precedence of ConnectionSettings
*
* The routing property configured on `ConnectionSettings` however always takes precedence.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove 'however'

}
[U] public void DuplicateJoinField()
{
/** a property with more than one JoinField is not allowed */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A class cannot contain more than one property of type JoinField, an exception is thrown in this case...

Action resolve = () => Expect("8080").WhenInferringRoutingOn(dto);
resolve.ShouldThrow<ArgumentException>().WithMessage("BadDTO has more than one JoinField property");

/** unless you configure NEST to look elsewhere */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... unless you configure the ConnectionSettings to use an alternate property:

* ==== Default mapping for String properties
* ==== Parent And Child mapping
*
* In the following example we setup our client and give our types prefered index and type names. What's new is that we can
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Delete 'What's new' and use 'In NEST 6.x'

*/
* ==== Indexing parents or children
*
* Now that we have our join field mapping set up on the index we can proceed to index parents and children documents.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use phrase 'parent and child documents'

@Mpdreamz Mpdreamz merged commit 9558d7c into master Jan 4, 2018
@Mpdreamz Mpdreamz deleted the feature/auto-routing branch January 4, 2018 10:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants