Skip to content

Commit 1e8bb3c

Browse files
authored
Do not set routing automatically (#3805)
This commit updates LikeDocument so that routing is not automatically set when a document input is used. Routing is required in the MoreLikeThisFullDocumentQueryUsageTests because the Project type mapping requires routing, but this should not be the default.
1 parent 70f87f3 commit 1e8bb3c

File tree

2 files changed

+48
-20
lines changed

2 files changed

+48
-20
lines changed

src/Nest/QueryDsl/Specialized/MoreLikeThis/Like/LikeDocument.cs

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,51 +4,74 @@
44

55
namespace Nest
66
{
7+
/// <summary>
8+
/// An indexed or artificial document with which to find likeness
9+
/// </summary>
710
[InterfaceDataContract]
811
[ReadAs(typeof(LikeDocument<object>))]
912
public interface ILikeDocument
1013
{
14+
/// <summary>
15+
/// A document to find other documents like
16+
/// </summary>
1117
[DataMember(Name = "doc")]
1218
[JsonFormatter(typeof(SourceFormatter<object>))]
1319
object Document { get; set; }
1420

21+
/// <summary>
22+
/// The fields to use for likeness
23+
/// </summary>
1524
[DataMember(Name = "fields")]
1625
Fields Fields { get; set; }
1726

27+
/// <summary>
28+
/// The id of an indexed document to find other documents like
29+
/// </summary>
1830
[DataMember(Name = "_id")]
1931
Id Id { get; set; }
2032

33+
/// <summary>
34+
/// The index of an indexed document to find other documents like
35+
/// </summary>
2136
[DataMember(Name = "_index")]
2237
IndexName Index { get; set; }
2338

39+
/// <summary>
40+
/// A different analyzer than the one defined for the target fields
41+
/// </summary>
2442
[DataMember(Name = "per_field_analyzer")]
2543
IPerFieldAnalyzer PerFieldAnalyzer { get; set; }
2644

45+
/// <summary>
46+
/// The routing value of an indexed document to find other documents like
47+
/// </summary>
2748
[DataMember(Name = "routing")]
2849
Routing Routing { get; set; }
2950
}
3051

52+
/// <inheritdoc />
3153
public abstract class LikeDocumentBase : ILikeDocument
3254
{
33-
[IgnoreDataMember]
34-
private Routing _routing;
35-
55+
/// <inheritdoc />
3656
public object Document { get; set; }
3757

58+
/// <inheritdoc />
3859
public Fields Fields { get; set; }
3960

61+
/// <inheritdoc />
4062
public Id Id { get; set; }
63+
64+
/// <inheritdoc />
4165
public IndexName Index { get; set; }
4266

67+
/// <inheritdoc />
4368
public IPerFieldAnalyzer PerFieldAnalyzer { get; set; }
4469

45-
public Routing Routing
46-
{
47-
get => _routing ?? (Document == null ? null : new Routing(Document));
48-
set => _routing = value;
49-
}
70+
/// <inheritdoc />
71+
public Routing Routing { get; set; }
5072
}
5173

74+
/// <inheritdoc cref="ILikeDocument" />
5275
public class LikeDocument<TDocument> : LikeDocumentBase
5376
where TDocument : class
5477
{
@@ -67,38 +90,39 @@ public LikeDocument(TDocument document)
6790
}
6891
}
6992

93+
/// <inheritdoc cref="ILikeDocument" />
7094
public class LikeDocumentDescriptor<TDocument> : DescriptorBase<LikeDocumentDescriptor<TDocument>, ILikeDocument>, ILikeDocument
7195
where TDocument : class
7296
{
73-
private Routing _routing;
74-
7597
public LikeDocumentDescriptor() => Self.Index = typeof(TDocument);
7698

7799
object ILikeDocument.Document { get; set; }
78100
Fields ILikeDocument.Fields { get; set; }
79101
Id ILikeDocument.Id { get; set; }
80102
IndexName ILikeDocument.Index { get; set; }
81103
IPerFieldAnalyzer ILikeDocument.PerFieldAnalyzer { get; set; }
104+
Routing ILikeDocument.Routing { get; set; }
82105

83-
Routing ILikeDocument.Routing
84-
{
85-
get => _routing ?? (Self.Document == null ? null : new Routing(Self.Document));
86-
set => _routing = value;
87-
}
88-
106+
/// <inheritdoc cref="ILikeDocument.Index" />
89107
public LikeDocumentDescriptor<TDocument> Index(IndexName index) => Assign(index, (a, v) => a.Index = v);
90108

109+
/// <inheritdoc cref="ILikeDocument.Id" />
91110
public LikeDocumentDescriptor<TDocument> Id(Id id) => Assign(id, (a, v) => a.Id = v);
92111

112+
/// <inheritdoc cref="ILikeDocument.Routing" />
93113
public LikeDocumentDescriptor<TDocument> Routing(Routing routing) => Assign(routing, (a, v) => a.Routing = v);
94114

115+
/// <inheritdoc cref="ILikeDocument.Fields" />
95116
public LikeDocumentDescriptor<TDocument> Fields(Func<FieldsDescriptor<TDocument>, IPromise<Fields>> fields) =>
96117
Assign(fields, (a, v) => a.Fields = v?.Invoke(new FieldsDescriptor<TDocument>())?.Value);
97118

119+
/// <inheritdoc cref="ILikeDocument.Fields" />
98120
public LikeDocumentDescriptor<TDocument> Fields(Fields fields) => Assign(fields, (a, v) => a.Fields = v);
99121

100-
public LikeDocumentDescriptor<TDocument> Document(TDocument document) => Assign(document, (a, v) => a.Document = v);
122+
/// <inheritdoc cref="ILikeDocument.Document" />
123+
public LikeDocumentDescriptor<TDocument> Document(TDocument document) => Assign(document, (a, v) => a.Document = v);
101124

125+
/// <inheritdoc cref="ILikeDocument.PerFieldAnalyzer" />
102126
public LikeDocumentDescriptor<TDocument> PerFieldAnalyzer(
103127
Func<PerFieldAnalyzerDescriptor<TDocument>, IPromise<IPerFieldAnalyzer>> analyzerSelector
104128
) =>

src/Tests/Tests/QueryDsl/Specialized/MoreLikeThis/MoreLikeThisFullDocumentQueryUsageTests.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public MoreLikeThisFullDocumentQueryUsageTests(ReadOnlyCluster i, EndpointUsage
1414
{
1515
Like = new List<Like>
1616
{
17-
new LikeDocument<Project>(Project.Instance),
17+
new LikeDocument<Project>(Project.Instance) { Routing = Project.Instance.Name },
1818
"some long text"
1919
}
2020
};
@@ -28,7 +28,8 @@ public MoreLikeThisFullDocumentQueryUsageTests(ReadOnlyCluster i, EndpointUsage
2828
new
2929
{
3030
_index = "project",
31-
doc = Project.InstanceAnonymous
31+
doc = Project.InstanceAnonymous,
32+
routing = Project.Instance.Name
3233
},
3334
"some long text"
3435
}
@@ -38,7 +39,10 @@ public MoreLikeThisFullDocumentQueryUsageTests(ReadOnlyCluster i, EndpointUsage
3839
protected override QueryContainer QueryFluent(QueryContainerDescriptor<Project> q) => q
3940
.MoreLikeThis(sn => sn
4041
.Like(l => l
41-
.Document(d => d.Document(Project.Instance))
42+
.Document(d => d
43+
.Document(Project.Instance)
44+
.Routing(Project.Instance.Name)
45+
)
4246
.Text("some long text")
4347
)
4448
);

0 commit comments

Comments
 (0)