Skip to content

Commit 01202a0

Browse files
committed
Do not set _id and routing automatically (#3805)
This commit updates LikeDocument so that _id and routing are not 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. Fixes #3977
1 parent 1900aea commit 01202a0

File tree

2 files changed

+50
-27
lines changed

2 files changed

+50
-27
lines changed

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

Lines changed: 43 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,55 +3,80 @@
33

44
namespace Nest
55
{
6+
7+
/// <summary>
8+
/// An indexed or artificial document with which to find likeness
9+
/// </summary>
610
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
711
[ContractJsonConverter(typeof(ReadAsTypeJsonConverter<LikeDocument<object>>))]
812
public interface ILikeDocument
913
{
14+
/// <summary>
15+
/// A document to find other documents like
16+
/// </summary>
1017
[JsonProperty("doc")]
1118
[JsonConverter(typeof(SourceConverter))]
1219
object Document { get; set; }
1320

21+
/// <summary>
22+
/// The fields to use for likeness
23+
/// </summary>
1424
[JsonProperty("fields")]
1525
Fields Fields { get; set; }
1626

27+
/// <summary>
28+
/// The id of an indexed document to find other documents like
29+
/// </summary>
1730
[JsonProperty("_id")]
1831
Id Id { get; set; }
1932

33+
/// <summary>
34+
/// The index of an indexed document to find other documents like
35+
/// </summary>
2036
[JsonProperty("_index")]
2137
IndexName Index { get; set; }
2238

39+
/// <summary>
40+
/// A different analyzer than the one defined for the target fields
41+
/// </summary>
2342
[JsonProperty("per_field_analyzer")]
2443
IPerFieldAnalyzer PerFieldAnalyzer { get; set; }
2544

45+
/// <summary>
46+
/// The routing value of an indexed document to find other documents like
47+
/// </summary>
2648
[JsonProperty("_routing")]
2749
Routing Routing { get; set; }
2850

2951
[JsonProperty("_type")]
3052
TypeName Type { get; set; }
3153
}
3254

55+
/// <inheritdoc />
3356
public abstract class LikeDocumentBase : ILikeDocument
3457
{
35-
private Routing _routing;
36-
58+
/// <inheritdoc />
3759
public object Document { get; set; }
3860

61+
/// <inheritdoc />
3962
public Fields Fields { get; set; }
4063

64+
/// <inheritdoc />
4165
public Id Id { get; set; }
66+
67+
/// <inheritdoc />
4268
public IndexName Index { get; set; }
4369

70+
/// <inheritdoc />
4471
public IPerFieldAnalyzer PerFieldAnalyzer { get; set; }
4572

46-
public Routing Routing
47-
{
48-
get => _routing ?? (Document == null ? null : new Routing(Document));
49-
set => _routing = value;
50-
}
51-
5273
public TypeName Type { get; set; }
74+
75+
/// <inheritdoc />
76+
public Routing Routing { get; set; }
5377
}
5478

79+
/// <inheritdoc cref="ILikeDocument" />
5580
public class LikeDocument<TDocument> : LikeDocumentBase
5681
where TDocument : class
5782
{
@@ -66,17 +91,16 @@ public LikeDocument(Id id)
6691

6792
public LikeDocument(TDocument document)
6893
{
69-
Id = Id.From(document);
7094
Document = document;
7195
Index = typeof(TDocument);
7296
Type = typeof(TDocument);
7397
}
7498
}
7599

100+
/// <inheritdoc cref="ILikeDocument" />
76101
public class LikeDocumentDescriptor<TDocument> : DescriptorBase<LikeDocumentDescriptor<TDocument>, ILikeDocument>, ILikeDocument
77102
where TDocument : class
78103
{
79-
private Routing _routing;
80104

81105
public LikeDocumentDescriptor()
82106
{
@@ -85,39 +109,36 @@ public LikeDocumentDescriptor()
85109
}
86110

87111
object ILikeDocument.Document { get; set; }
88-
89112
Fields ILikeDocument.Fields { get; set; }
90113
Id ILikeDocument.Id { get; set; }
91114
IndexName ILikeDocument.Index { get; set; }
92115
IPerFieldAnalyzer ILikeDocument.PerFieldAnalyzer { get; set; }
93-
94-
Routing ILikeDocument.Routing
95-
{
96-
get => _routing ?? (Self.Document == null ? null : new Routing(Self.Document));
97-
set => _routing = value;
98-
}
116+
Routing ILikeDocument.Routing { get; set; }
99117

100118
TypeName ILikeDocument.Type { get; set; }
101119

120+
/// <inheritdoc cref="ILikeDocument.Index" />
102121
public LikeDocumentDescriptor<TDocument> Index(IndexName index) => Assign(index, (a, v) => a.Index = v);
103122

104123
public LikeDocumentDescriptor<TDocument> Type(TypeName type) => Assign(type, (a, v) => a.Type = v);
105124

125+
/// <inheritdoc cref="ILikeDocument.Id" />
106126
public LikeDocumentDescriptor<TDocument> Id(Id id) => Assign(id, (a, v) => a.Id = v);
107127

128+
/// <inheritdoc cref="ILikeDocument.Routing" />
108129
public LikeDocumentDescriptor<TDocument> Routing(Routing routing) => Assign(routing, (a, v) => a.Routing = v);
109130

131+
/// <inheritdoc cref="ILikeDocument.Fields" />
110132
public LikeDocumentDescriptor<TDocument> Fields(Func<FieldsDescriptor<TDocument>, IPromise<Fields>> fields) =>
111133
Assign(fields, (a, v) => a.Fields = v?.Invoke(new FieldsDescriptor<TDocument>())?.Value);
112134

135+
/// <inheritdoc cref="ILikeDocument.Fields" />
113136
public LikeDocumentDescriptor<TDocument> Fields(Fields fields) => Assign(fields, (a, v) => a.Fields = v);
114137

115-
public LikeDocumentDescriptor<TDocument> Document(TDocument document) => Assign(document, (a, v) =>
116-
{
117-
a.Id = Infer.Id(v);
118-
a.Document = v;
119-
});
138+
/// <inheritdoc cref="ILikeDocument.Document" />
139+
public LikeDocumentDescriptor<TDocument> Document(TDocument document) => Assign(document, (a, v) => a.Document = v);
120140

141+
/// <inheritdoc cref="ILikeDocument.PerFieldAnalyzer" />
121142
public LikeDocumentDescriptor<TDocument> PerFieldAnalyzer(
122143
Func<PerFieldAnalyzerDescriptor<TDocument>, IPromise<IPerFieldAnalyzer>> analyzerSelector
123144
) =>

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public MoreLikeThisFullDocumentQueryUsageTests(ReadOnlyCluster i, EndpointUsage
1616
{
1717
Like = new List<Like>
1818
{
19-
new LikeDocument<Project>(Project.Instance),
19+
new LikeDocument<Project>(Project.Instance) { Routing = Project.Instance.Name },
2020
"some long text"
2121
}
2222
};
@@ -31,9 +31,8 @@ public MoreLikeThisFullDocumentQueryUsageTests(ReadOnlyCluster i, EndpointUsage
3131
{
3232
_index = "project",
3333
_type = "doc",
34-
_id = Project.Instance.Name,
35-
_routing = Project.Instance.Name,
36-
doc = Project.InstanceAnonymous
34+
doc = Project.InstanceAnonymous,
35+
_routing = Project.Instance.Name
3736
},
3837
"some long text"
3938
}
@@ -43,7 +42,10 @@ public MoreLikeThisFullDocumentQueryUsageTests(ReadOnlyCluster i, EndpointUsage
4342
protected override QueryContainer QueryFluent(QueryContainerDescriptor<Project> q) => q
4443
.MoreLikeThis(sn => sn
4544
.Like(l => l
46-
.Document(d => d.Document(Project.Instance))
45+
.Document(d => d
46+
.Document(Project.Instance)
47+
.Routing(Project.Instance.Name)
48+
)
4749
.Text("some long text")
4850
)
4951
);

0 commit comments

Comments
 (0)