Skip to content

Commit 236a531

Browse files
committed
Update() now support partial updates (0.20), Bulk now supports Update (0.90.1) too
1 parent d3a3273 commit 236a531

22 files changed

+614
-133
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using System.Linq;
2+
using FluentAssertions;
3+
using NUnit.Framework;
4+
using Nest.Tests.MockData.Domain;
5+
6+
namespace Nest.Tests.Integration.Core.Bulk
7+
{
8+
public class BulkPercolateTests : BulkTests
9+
{
10+
[Test]
11+
public void BulkIndexWithPercolate()
12+
{
13+
// register up some percolator queries to test matching
14+
var query1 = "bulkindex-test-doc-1";
15+
16+
this._client.UnregisterPercolator<ElasticSearchProject>(query1);
17+
18+
var perc = this._client.RegisterPercolator<ElasticSearchProject>(p => p
19+
.Name(query1)
20+
.Query(q => q
21+
.Term(f => f.Country, "netherlands")
22+
)
23+
);
24+
25+
var descriptor = new BulkDescriptor();
26+
27+
// match against any doc
28+
descriptor.Index<ElasticSearchProject>(i => i
29+
.Object(new ElasticSearchProject { Id = 2, Country = "netherlands" })
30+
.Percolate("*") // match on any percolated docs
31+
);
32+
33+
// no percolate requested this time
34+
descriptor.Index<ElasticSearchProject>(i => i
35+
.Object(new ElasticSearchProject { Id = 3, Country = "netherlands" })
36+
);
37+
38+
var result = this._client.Bulk(descriptor);
39+
40+
result.Should().NotBeNull();
41+
result.IsValid.Should().BeTrue();
42+
43+
result.Items.Should().NotBeNull().And.NotBeEmpty().And.HaveCount(2).And.OnlyContain(r => r.OK);
44+
var indexResponses = result.Items.OfType<BulkIndexResponseItem>().ToList();
45+
46+
// tests on percolated responses
47+
indexResponses.Should().HaveCount(2);
48+
49+
indexResponses.First().Id.Should().BeEquivalentTo("2");
50+
indexResponses.First().Index.Should().BeEquivalentTo(ElasticsearchConfiguration.DefaultIndex);
51+
indexResponses.First().Type.Should().BeEquivalentTo(this.GetTypeNameFor<ElasticSearchProject>());
52+
indexResponses.First().Matches.Should().NotBeNull();
53+
54+
indexResponses.ElementAt(1).Id.Should().BeEquivalentTo("3");
55+
indexResponses.ElementAt(1).Index.Should().BeEquivalentTo(ElasticsearchConfiguration.DefaultIndex);
56+
indexResponses.ElementAt(1).Type.Should().BeEquivalentTo(this.GetTypeNameFor<ElasticSearchProject>());
57+
indexResponses.First().Matches.Should().BeNull();
58+
59+
// cleanup
60+
this._client.UnregisterPercolator<ElasticSearchProject>(query1);
61+
}
62+
}
63+
}

src/Nest.Tests.Integration/Core/Bulk/BulkTests.cs

Lines changed: 7 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,17 @@ namespace Nest.Tests.Integration.Core.Bulk
1010
[TestFixture]
1111
public class BulkTests : IntegrationTests
1212
{
13+
private readonly BulkPercolateTests _bulkPercolateTests;
14+
15+
1316
[Test]
1417
public void Bulk()
1518
{
16-
//Delete so we know the create does not throw an error.
17-
this._client.DeleteIndex(ElasticsearchConfiguration.DefaultIndex);
1819
var result = this._client.Bulk(b => b
1920
.Index<ElasticSearchProject>(i => i.Object(new ElasticSearchProject {Id = 2}))
20-
.Create<ElasticSearchProject>(i => i.Object(new ElasticSearchProject { Id = 3 }))
21+
.Create<ElasticSearchProject>(i => i.Object(new ElasticSearchProject { Id = 123123 }))
2122
.Delete<ElasticSearchProject>(i => i.Object(new ElasticSearchProject { Id = 4 }))
23+
2224
);
2325
result.Should().NotBeNull();
2426
result.IsValid.Should().BeTrue();
@@ -34,7 +36,7 @@ public void Bulk()
3436
deleteResponses.First().Type.Should().BeEquivalentTo(this.GetTypeNameFor<ElasticSearchProject>());
3537

3638
createResponses.Should().HaveCount(1);
37-
createResponses.First().Id.Should().BeEquivalentTo("3");
39+
createResponses.First().Id.Should().BeEquivalentTo("123123");
3840
createResponses.First().Index.Should().BeEquivalentTo(ElasticsearchConfiguration.DefaultIndex);
3941
createResponses.First().Type.Should().BeEquivalentTo(this.GetTypeNameFor<ElasticSearchProject>());
4042

@@ -116,10 +118,8 @@ public void BulkWithFixedIndexOveriddenIndividualy()
116118
[Test]
117119
public void BulkAlternativeWayOfWriting()
118120
{
119-
this._client.DeleteIndex(ElasticsearchConfiguration.DefaultIndex);
120-
121121
var descriptor = new BulkDescriptor();
122-
foreach (var i in Enumerable.Range(0, 1000))
122+
foreach (var i in Enumerable.Range(3000, 1000))
123123
descriptor.Index<ElasticSearchProject>(op => op.Object(new ElasticSearchProject {Id = i}));
124124

125125
var result = this._client.Bulk(descriptor);
@@ -129,59 +129,5 @@ public void BulkAlternativeWayOfWriting()
129129
result.Items.Should().NotBeNull().And.NotBeEmpty().And.HaveCount(1000).And.OnlyContain(r => r.OK);
130130

131131
}
132-
133-
[Test]
134-
public void BulkIndexWithPercolate()
135-
{
136-
// register up some percolator queries to test matching
137-
var query1 = "bulkindex-test-doc-1";
138-
139-
this._client.UnregisterPercolator<ElasticSearchProject>(query1);
140-
141-
var perc = this._client.RegisterPercolator<ElasticSearchProject>(p => p
142-
.Name(query1)
143-
.Query(q => q
144-
.Term(f => f.Country, "netherlands")
145-
)
146-
);
147-
148-
var descriptor = new BulkDescriptor();
149-
150-
// match against any doc
151-
descriptor.Index<ElasticSearchProject>(i => i
152-
.Object(new ElasticSearchProject { Id = 2, Country = "netherlands" })
153-
.Percolate("*") // match on any percolated docs
154-
);
155-
156-
// no percolate requested this time
157-
descriptor.Index<ElasticSearchProject>(i => i
158-
.Object(new ElasticSearchProject { Id = 3, Country = "netherlands" })
159-
);
160-
161-
var result = this._client.Bulk(descriptor);
162-
163-
result.Should().NotBeNull();
164-
result.IsValid.Should().BeTrue();
165-
166-
result.Items.Should().NotBeNull().And.NotBeEmpty().And.HaveCount(2).And.OnlyContain(r => r.OK);
167-
var indexResponses = result.Items.OfType<BulkIndexResponseItem>();
168-
169-
// tests on percolated responses
170-
indexResponses.Should().HaveCount(2);
171-
172-
indexResponses.First().Id.Should().BeEquivalentTo("2");
173-
indexResponses.First().Index.Should().BeEquivalentTo(ElasticsearchConfiguration.DefaultIndex);
174-
indexResponses.First().Type.Should().BeEquivalentTo(this.GetTypeNameFor<ElasticSearchProject>());
175-
indexResponses.First().Matches.Should().NotBeNull();
176-
177-
indexResponses.ElementAt(1).Id.Should().BeEquivalentTo("3");
178-
indexResponses.ElementAt(1).Index.Should().BeEquivalentTo(ElasticsearchConfiguration.DefaultIndex);
179-
indexResponses.ElementAt(1).Type.Should().BeEquivalentTo(this.GetTypeNameFor<ElasticSearchProject>());
180-
indexResponses.First().Matches.Should().BeNull();
181-
182-
// cleanup
183-
this._client.UnregisterPercolator<ElasticSearchProject>(query1);
184-
}
185-
186132
}
187133
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System.Linq;
2+
using FluentAssertions;
3+
using NUnit.Framework;
4+
using Nest.Tests.MockData.Domain;
5+
6+
namespace Nest.Tests.Integration.Core.Bulk
7+
{
8+
public class BulkUpdateTests : BulkTests
9+
{
10+
11+
[Test]
12+
public void BulkUpdateObject()
13+
{
14+
//Lets first insert some documents with id range 5000-6000
15+
var descriptor = new BulkDescriptor();
16+
foreach (var i in Enumerable.Range(5000, 1000))
17+
descriptor.Index<ElasticSearchProject>(op => op.Object(new ElasticSearchProject { Id = i }));
18+
19+
var result = this._client.Bulk(descriptor);
20+
result.Should().NotBeNull();
21+
result.IsValid.Should().BeTrue();
22+
23+
//Now lets update all of them giving them a name
24+
descriptor = new BulkDescriptor();
25+
foreach (var i in Enumerable.Range(5000, 1000))
26+
{
27+
int id = i;
28+
descriptor.Update<ElasticSearchProject, object>(op => op
29+
.Object(new ElasticSearchProject { Id = id })
30+
.Document(new { name = "SufixedName-" + id})
31+
.Refresh()
32+
);
33+
}
34+
35+
result = this._client.Bulk(descriptor);
36+
result.Should().NotBeNull();
37+
result.IsValid.Should().BeTrue();
38+
39+
var updatedObject = this._client.Get<ElasticSearchProject>(5000);
40+
Assert.NotNull(updatedObject);
41+
Assert.AreEqual(updatedObject.Name, "SufixedName-5000");
42+
43+
}
44+
}
45+
}

src/Nest.Tests.Integration/Nest.Tests.Integration.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@
5353
</ItemGroup>
5454
<ItemGroup>
5555
<Compile Include="Connection\Thrift\ThiftBugReportTests.cs" />
56+
<Compile Include="Core\Bulk\BulkPercolateTests.cs" />
5657
<Compile Include="Core\Bulk\BulkTests.cs" />
58+
<Compile Include="Core\Bulk\BulkUpdateTests.cs" />
5759
<Compile Include="Core\MultiSearch\MultiSearchTests.cs" />
5860
<Compile Include="ElasticsearchConfiguration.cs" />
5961
<Compile Include="Core\IndexTests.cs" />

src/Nest.Tests.Unit/BaseJsonTests.cs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Text;
5+
using System.Text.RegularExpressions;
56
using NUnit.Framework;
67
using Newtonsoft.Json;
78
using Newtonsoft.Json.Linq;
@@ -39,8 +40,14 @@ protected void deb(string s)
3940
//with this i can print variables as i type...
4041
//Lazy programmers for the win!
4142
throw new Exception(s);
42-
}
43-
43+
}
44+
protected void JsonEqualsCamelCase(object o, MethodBase method, string fileName = null)
45+
{
46+
//Todo find a way to have a single code path for serializations
47+
//Serialize vs SerializeCamelCase smells a bit.
48+
var json = _client.SerializeCamelCase(o);
49+
this.JsonEquals(json, method, fileName);
50+
}
4451
protected void JsonEquals(object o, MethodBase method, string fileName = null)
4552
{
4653
var json = _client.Serialize(o);
@@ -50,12 +57,9 @@ protected void JsonEquals(string json, MethodBase method, string fileName = null
5057
{
5158
var file = this.GetFileFromMethod(method, fileName);
5259

53-
54-
5560
var expected = File.ReadAllText(file);
5661
Assert.True(json.JsonEquals(expected), this.PrettyPrint(json));
5762
}
58-
5963

6064
protected void JsonNotEquals(object o, MethodBase method, string fileName = null)
6165
{
@@ -69,6 +73,20 @@ protected void JsonNotEquals(string json, MethodBase method, string fileName = n
6973
var expected = File.ReadAllText(file);
7074
Assert.False(json.JsonEquals(expected), this.PrettyPrint(json));
7175
}
76+
protected void BulkJsonEquals(string json, MethodBase method, string fileName = null)
77+
{
78+
var file = this.GetFileFromMethod(method, fileName);
79+
var expected = File.ReadAllText(file);
80+
var expectedJsonLines = Regex.Split(expected, @"\r?\n", RegexOptions.None).Where(s=>!s.IsNullOrEmpty());
81+
var jsonLines = Regex.Split(json, @"\r?\n", RegexOptions.None).Where(s => !s.IsNullOrEmpty());
82+
Assert.IsNotEmpty(expectedJsonLines, json);
83+
Assert.IsNotEmpty(jsonLines, json);
84+
Assert.AreEqual(jsonLines.Count(), expectedJsonLines.Count(), json);
85+
foreach (var line in expectedJsonLines.Zip(jsonLines, (e,j) => new { Expected = e, Json = j}))
86+
{
87+
Assert.True(line.Json.JsonEquals(line.Expected), line.Json);
88+
}
89+
}
7290

7391
private string PrettyPrint(string json)
7492
{
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{ "index" : {"_index":"nest_test_data","_type":"elasticsearchprojects","_id":"2"} }
2+
{"id":2,"loc":0,"startedOn":"0001-01-01T00:00:00","longValue":0,"floatValue":0.0,"doubleValue":0.0,"boolValue":false,"locScriptField":0,"stupidIntIWantAsLong":0}
3+
{ "create" : {"_index":"nest_test_data","_type":"elasticsearchprojects","_id":"3"} }
4+
{"id":3,"loc":0,"startedOn":"0001-01-01T00:00:00","longValue":0,"floatValue":0.0,"doubleValue":0.0,"boolValue":false,"locScriptField":0,"stupidIntIWantAsLong":0}
5+
{ "delete" : {"_index":"nest_test_data","_type":"elasticsearchprojects","_id":"4"} }
6+
{ "update" : {"_index":"nest_test_data","_type":"elasticsearchprojects","_id":"3"} }
7+
{ "doc" : { "name" : "NEST" } }

src/Nest.Tests.Unit/Core/Bulk/BulkTests.cs

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,40 @@
22
using FluentAssertions;
33
using NUnit.Framework;
44
using Nest.Tests.MockData.Domain;
5+
using System.Reflection;
56

67
namespace Nest.Tests.Unit.Core.Bulk
78
{
89
[TestFixture]
910
public class BulkTests : BaseJsonTests
1011
{
1112
[Test]
12-
public void BulkNonFixed()
13+
public void BulkOperations()
1314
{
1415
var result = this._client.Bulk(b => b
1516
.Index<ElasticSearchProject>(i => i.Object(new ElasticSearchProject {Id = 2}))
1617
.Create<ElasticSearchProject>(i => i.Object(new ElasticSearchProject { Id = 3 }))
1718
.Delete<ElasticSearchProject>(i => i.Object(new ElasticSearchProject { Id = 4 }))
19+
.Update<ElasticSearchProject, object>(i => i
20+
.Object(new ElasticSearchProject { Id = 3 })
21+
.Document(new { name = "NEST"})
22+
)
1823
);
1924
var status = result.ConnectionStatus;
20-
var uri = new Uri(result.ConnectionStatus.RequestUrl);
21-
uri.AbsolutePath.Should().Be("/_bulk");
25+
this.BulkJsonEquals(status.Request, MethodInfo.GetCurrentMethod());
2226
}
2327
[Test]
24-
public void BulkFixedIndex()
25-
{
26-
var result = this._client.Bulk(b => b
27-
.FixedPath("myindex")
28-
.Index<ElasticSearchProject>(i => i.Object(new ElasticSearchProject { Id = 2 }))
29-
.Create<ElasticSearchProject>(i => i.Object(new ElasticSearchProject { Id = 3 }))
30-
.Delete<ElasticSearchProject>(i => i.Object(new ElasticSearchProject { Id = 4 }))
31-
);
32-
var status = result.ConnectionStatus;
33-
var uri = new Uri(result.ConnectionStatus.RequestUrl);
34-
uri.AbsolutePath.Should().Be("/myindex/_bulk");
35-
}
36-
[Test]
37-
public void BulkFixedIndexAndType()
28+
public void BulkUpdateDetails()
3829
{
3930
var result = this._client.Bulk(b => b
40-
.FixedPath("myindex", "mytype")
41-
.Index<ElasticSearchProject>(i => i.Object(new ElasticSearchProject { Id = 2 }))
42-
.Create<ElasticSearchProject>(i => i.Object(new ElasticSearchProject { Id = 3 }))
43-
.Delete<ElasticSearchProject>(i => i.Object(new ElasticSearchProject { Id = 4 }))
31+
.Update<ElasticSearchProject, object>(i => i
32+
.Object(new ElasticSearchProject { Id = 3 })
33+
.Document(new { name = "NEST" })
34+
.RetriesOnConflict(4)
35+
)
4436
);
4537
var status = result.ConnectionStatus;
46-
var uri = new Uri(result.ConnectionStatus.RequestUrl);
47-
uri.AbsolutePath.Should().Be("/myindex/mytype/_bulk");
38+
this.BulkJsonEquals(status.Request, MethodInfo.GetCurrentMethod());
4839
}
4940
}
5041
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{ "update" : {"_index":"nest_test_data","_type":"elasticsearchprojects","_id":"3", "_retry_on_conflict" : 4} }
2+
{"doc":{"name":"NEST"}}

0 commit comments

Comments
 (0)