Skip to content

Commit c5db2ba

Browse files
committed
MultiGetOperation can now be deserialised with default serialiser
Added Unit tests for MoreLikeThisQueryJson.cs resolving #1415
1 parent b3a4109 commit c5db2ba

File tree

1 file changed

+50
-26
lines changed

1 file changed

+50
-26
lines changed

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

Lines changed: 50 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using NUnit.Framework;
1+
using System;
2+
using NUnit.Framework;
23
using Nest.Tests.MockData.Domain;
34

45
namespace Nest.Tests.Unit.Search.Query.Singles
@@ -15,39 +16,58 @@ public class MoreLikeThisTestDoc
1516
[Test]
1617
public void TestMoreLikeThisQuery()
1718
{
18-
var s = new SearchDescriptor<ElasticsearchProject>()
19-
.From(0)
20-
.Size(10)
21-
.Query(q => q
22-
.MoreLikeThis(fz => fz
23-
.Name("named_query")
24-
.OnFields(f => f.Name)
25-
.LikeText("elasticsearcc")
26-
)
27-
);
19+
Func<MoreLikeThisQueryDocumentsDescriptor<ElasticsearchProject>, MoreLikeThisQueryDocumentsDescriptor<ElasticsearchProject>> documentsSelector = d => d.Get("SOME_ID", op => op.Index("my_index").Type("my_type"));
20+
Action<MoreLikeThisQueryDescriptor<ElasticsearchProject>> selector = fz => fz
21+
.Name("named_query")
22+
.OnFields(f => f.Name, f => f.Content)
23+
.LikeText("elasticsearcc")
24+
.Documents(documentsSelector)
25+
.MinTermFrequency(1)
26+
.MaxQueryTerms(12);
27+
var s = new SearchDescriptor<ElasticsearchProject>()
28+
.From(0)
29+
.Size(10)
30+
.Query(q => q.MoreLikeThis(selector));
31+
2832
var json = TestElasticClient.Serialize(s);
29-
var expected = @"{ from: 0, size: 10, query :
33+
const string expected = @"{ from: 0, size: 10, query :
3034
{ mlt: {
3135
_name: ""named_query"",
32-
fields : [""name"" ],
33-
like_text : ""elasticsearcc""
34-
}}}";
35-
Assert.True(json.JsonEquals(expected), json);
36+
fields : [""name"", ""content"" ],
37+
like_text : ""elasticsearcc"" ,
38+
""docs"": [
39+
{
40+
""_index"": ""my_index"",
41+
""_type"": ""my_type"",
42+
""_id"": ""SOME_ID""
43+
}
44+
],
45+
""min_term_freq"": 1,
46+
""max_query_terms"": 12
47+
}}}";
48+
Verify<SearchDescriptor<dynamic>>(json, expected);
3649
}
3750

38-
[Test]
51+
private static void Verify<T>(string json, string expected) where T: class
52+
{
53+
Assert.True(json.JsonEquals(expected), json);
54+
var returnedJson = TestElasticClient.Serialize(TestElasticClient.Deserialize<T>(json));
55+
Assert.True(json.JsonEquals(returnedJson), json);
56+
}
57+
58+
[Test]
3959
public void MoreLikeThisWithIds()
4060
{
4161
var s = new MoreLikeThisQueryDescriptor<ElasticsearchProject>()
4262
.Ids(new long[] { 1, 2, 3, 4 })
4363
.OnFields(p => p.Name);
4464
var json = TestElasticClient.Serialize(s);
4565

46-
var expected = @"{
66+
const string expected = @"{
4767
fields: [ ""name""],
4868
ids: [ ""1"", ""2"", ""3"", ""4""],
4969
}";
50-
Assert.True(json.JsonEquals(expected), json);
70+
Verify<MoreLikeThisQueryDescriptor<object>>(json, expected);
5171
}
5272

5373
[Test]
@@ -69,7 +89,7 @@ public void MoreLikeThisWithDocuments()
6989
);
7090
var json = TestElasticClient.Serialize(s);
7191

72-
var expected = @"{
92+
const string expected = @"{
7393
fields: [ ""name""],
7494
docs: [
7595
{
@@ -113,7 +133,8 @@ public void MoreLikeThisWithDocuments()
113133
}
114134
}]
115135
}";
116-
Assert.True(json.JsonEquals(expected), json);
136+
137+
Verify<MoreLikeThisQueryDescriptor<object>>(json, expected);
117138
}
118139

119140
[Test]
@@ -122,13 +143,13 @@ public void MoreLikeThisWithDocumentsExplicit()
122143
var s = new MoreLikeThisQueryDescriptor<ElasticsearchProject>()
123144
.OnFields(p => p.Name)
124145
.DocumentsExplicit(d => d
125-
.Get(1, g => g.Fields(p => p.Product.Name).Routing("routing_value"))
146+
.Get(1, g => g.Fields(p => p.Product.Name).Routing("routing_value"))
126147
.Get<Person>("some-string-id", g => g.Routing("routing_value").Type("people").Index("different_index"))
127148
);
128149
var json = TestElasticClient.Serialize(s);
129150

130151
//NEST should default to not sending index but when specified explicitly it should not force it null
131-
var expected = @"{
152+
const string expected = @"{
132153
fields: [ ""name""],
133154
docs: [
134155
{
@@ -146,7 +167,9 @@ public void MoreLikeThisWithDocumentsExplicit()
146167
_routing: ""routing_value""
147168
}]
148169
}";
149-
Assert.True(json.JsonEquals(expected), json);
170+
Assert.True(json.JsonEquals(expected), json);
171+
//Nice if we get this working
172+
//Verify<MoreLikeThisQueryDescriptor<object>>(json, expected);
150173
}
151174

152175
[Test]
@@ -175,7 +198,7 @@ public void TestMoreLikeThisAllQuery()
175198
)
176199
);
177200
var json = TestElasticClient.Serialize(s);
178-
var expected = @"{ from: 0, size: 10, query :
201+
const string expected = @"{ from: 0, size: 10, query :
179202
{ mlt: {
180203
fields : [""name"" ],
181204
like_text : ""elasticsearcc"",
@@ -195,7 +218,8 @@ public void TestMoreLikeThisAllQuery()
195218
boost: 1.1,
196219
analyzer: ""my_analyzer""
197220
}}}";
198-
Assert.True(json.JsonEquals(expected), json);
221+
222+
Verify<SearchDescriptor<ElasticsearchProject>>(json, expected);
199223
}
200224
}
201225
}

0 commit comments

Comments
 (0)