Skip to content

Commit de12213

Browse files
committed
MLT query: add support for artificial docs
Closes #1341
1 parent 65bed84 commit de12213

File tree

4 files changed

+82
-10
lines changed

4 files changed

+82
-10
lines changed

src/Nest/DSL/MultiGet/IMultiGetOperation.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ public interface IMultiGetOperation
2424

2525
[JsonProperty(PropertyName = "_source")]
2626
ISourceFilter Source { get; set; }
27+
28+
// Only used for the MLT query for specifying an artificial document.
29+
// TODO: For 2.0, we should consider decoupling IMultiGetOperation from
30+
// MoreLikeThisQuery and have a dedicatd MoreLikeThisDocument object.
31+
[JsonProperty(PropertyName = "doc")]
32+
object Document { get; set; }
2733

2834
Type ClrType { get; }
2935
}

src/Nest/DSL/MultiGet/MultiGetOperationDescriptor.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public MultiGetOperation(string id)
1515
this.Index = typeof(T);
1616
this.Type = typeof(T);
1717
}
18+
1819
public MultiGetOperation(long id) : this(id.ToString(CultureInfo.InvariantCulture)) {}
1920

2021
Type IMultiGetOperation.ClrType { get { return typeof(T); } }
@@ -30,6 +31,8 @@ public MultiGetOperation(long id) : this(id.ToString(CultureInfo.InvariantCultur
3031
public ISourceFilter Source { get; set; }
3132

3233
public string Routing { get; set; }
34+
35+
public object Document { get; set; }
3336
}
3437

3538
public class MultiGetOperationDescriptor<T> : IMultiGetOperation
@@ -43,6 +46,7 @@ public class MultiGetOperationDescriptor<T> : IMultiGetOperation
4346
string IMultiGetOperation.Routing { get; set; }
4447
ISourceFilter IMultiGetOperation.Source { get; set; }
4548
IList<PropertyPathMarker> IMultiGetOperation.Fields { get; set; }
49+
object IMultiGetOperation.Document { get; set; }
4650
Type IMultiGetOperation.ClrType { get { return typeof(T); } }
4751

4852
public MultiGetOperationDescriptor()
@@ -140,7 +144,7 @@ public MultiGetOperationDescriptor<T> Routing(string routing)
140144
/// </summary>
141145
public MultiGetOperationDescriptor<T> Fields(params Expression<Func<T, object>>[] expressions)
142146
{
143-
((IMultiGetOperation) this).Fields = expressions.Select(e => (PropertyPathMarker) e).ToList();
147+
Self.Fields = expressions.Select(e => (PropertyPathMarker) e).ToList();
144148
return this;
145149
}
146150

@@ -150,7 +154,16 @@ public MultiGetOperationDescriptor<T> Fields(params Expression<Func<T, object>>[
150154
/// </summary>
151155
public MultiGetOperationDescriptor<T> Fields(params string[] fields)
152156
{
153-
((IMultiGetOperation) this).Fields = fields.Select(f => (PropertyPathMarker) f).ToList();
157+
Self.Fields = fields.Select(f => (PropertyPathMarker) f).ToList();
158+
return this;
159+
}
160+
161+
// Only used for the MLT query for specifying an artificial document.
162+
// TODO: For 2.0, we should consider decoupling IMultiGetOperation from
163+
// MoreLikeThisQuery and have a dedicatd MoreLikeThisDocument object.
164+
public MultiGetOperationDescriptor<T> Document(T document)
165+
{
166+
Self.Document = document;
154167
return this;
155168
}
156169
}

src/Nest/DSL/Query/SubDescriptors/MoreLikeThisQueryDocumentsDescriptor.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,27 @@ public MoreLikeThisQueryDocumentsDescriptor<T> Get<TDocument>(string id, Func<Mu
5757
return this;
5858

5959
}
60+
61+
public MoreLikeThisQueryDocumentsDescriptor<T> Get<TDocument>(Func<MultiGetOperationDescriptor<TDocument>, MultiGetOperationDescriptor<TDocument>> getSelector)
62+
where TDocument : class
63+
{
64+
getSelector = getSelector ?? (s => s);
65+
var descriptor = getSelector(new MultiGetOperationDescriptor<TDocument>(_allowExplicitIndex));
66+
this.GetOperations.Add(descriptor);
67+
return this;
68+
}
69+
70+
public MoreLikeThisQueryDocumentsDescriptor<T> Document<TDocument>(TDocument document, string index = null, string type = null)
71+
where TDocument : class
72+
{
73+
var descriptor = new MultiGetOperationDescriptor<TDocument>(_allowExplicitIndex)
74+
.Document(document);
75+
if (!string.IsNullOrEmpty(index))
76+
descriptor.Index(index);
77+
if (!string.IsNullOrEmpty(type))
78+
descriptor.Type(type);
79+
this.GetOperations.Add(descriptor);
80+
return this;
81+
}
6082
}
6183
}

src/Tests/Nest.Tests.Unit/Search/Query/Singles/MoreLikeThisQueryJson.cs

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ namespace Nest.Tests.Unit.Search.Query.Singles
66
[TestFixture]
77
public class MoreLikeThisQueryJson
88
{
9+
public class MoreLikeThisTestDoc
10+
{
11+
public string Name { get; set; }
12+
public string Text { get; set; }
13+
}
14+
915
[Test]
1016
public void TestMoreLikeThisQuery()
1117
{
@@ -28,7 +34,7 @@ public void TestMoreLikeThisQuery()
2834
}}}";
2935
Assert.True(json.JsonEquals(expected), json);
3036
}
31-
37+
3238
[Test]
3339
public void MoreLikeThisWithIds()
3440
{
@@ -43,15 +49,18 @@ public void MoreLikeThisWithIds()
4349
}";
4450
Assert.True(json.JsonEquals(expected), json);
4551
}
46-
52+
4753
[Test]
4854
public void MoreLikeThisWithDocuments()
4955
{
5056
var s = new MoreLikeThisQueryDescriptor<ElasticsearchProject>()
5157
.OnFields(p => p.Name)
52-
.Documents(d=>d
53-
.Get(1, g=>g.Fields(p=>p.Product.Name).Routing("routing_value"))
54-
.Get<Person>("some-string-id", g=>g.Routing("routing_value").Type("people").Index("different_index"))
58+
.Documents(d => d
59+
.Get(1, g => g.Fields(p => p.Product.Name).Routing("routing_value"))
60+
.Get<Person>("some-string-id", g => g.Routing("routing_value").Type("people").Index("different_index"))
61+
.Get<MoreLikeThisTestDoc>(g => g.Document(new MoreLikeThisTestDoc { Name = "elasticsearch", Text = "foo" }))
62+
.Document<MoreLikeThisTestDoc>(new MoreLikeThisTestDoc { Name = "nest" })
63+
.Document<MoreLikeThisTestDoc>(new MoreLikeThisTestDoc { Name = "foo" }, "myindex", "mytype")
5564
);
5665
var json = TestElasticClient.Serialize(s);
5766

@@ -72,6 +81,28 @@ public void MoreLikeThisWithDocuments()
7281
_type: ""people"",
7382
_id: ""some-string-id"",
7483
_routing: ""routing_value""
84+
},
85+
{
86+
_index: ""nest_test_data"",
87+
_type: ""morelikethistestdoc"",
88+
doc: {
89+
name: ""elasticsearch"",
90+
text: ""foo""
91+
}
92+
},
93+
{
94+
_index: ""nest_test_data"",
95+
_type: ""morelikethistestdoc"",
96+
doc: {
97+
name: ""nest""
98+
}
99+
},
100+
{
101+
_index: ""myindex"",
102+
_type: ""mytype"",
103+
doc: {
104+
name: ""foo""
105+
}
75106
}]
76107
}";
77108
Assert.True(json.JsonEquals(expected), json);
@@ -82,9 +113,9 @@ public void MoreLikeThisWithDocumentsExplicit()
82113
{
83114
var s = new MoreLikeThisQueryDescriptor<ElasticsearchProject>()
84115
.OnFields(p => p.Name)
85-
.DocumentsExplicit(d=>d
86-
.Get(1, g=>g.Fields(p=>p.Product.Name).Routing("routing_value"))
87-
.Get<Person>("some-string-id", g=>g.Routing("routing_value").Type("people").Index("different_index"))
116+
.DocumentsExplicit(d => d
117+
.Get(1, g => g.Fields(p => p.Product.Name).Routing("routing_value"))
118+
.Get<Person>("some-string-id", g => g.Routing("routing_value").Type("people").Index("different_index"))
88119
);
89120
var json = TestElasticClient.Serialize(s);
90121

0 commit comments

Comments
 (0)